add some more error checks into _fileExistsInDirectory function to report "too long name" error

Started by Mahendra Singh Thalorover 1 year ago2 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t51420
psql -h localhost -U postgres

Built from patchset v1 (message #1), July 27, 2026 at 09:34 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t51420_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t51420_1 && git checkout t51420_1

Patchset v1 (message #1) is on t51420_1

Jump to latest
#1Mahendra Singh Thalor
mahi6run@gmail.com

Hi,
In another thread[1]/messages/by-id/202504110938.4kx73ylnv6p4@alvherre.pgsql, Álvaro gave some feedback for _fileExistsInDirectory
function for "too long name" error.
Basically, in _fileExistsInDirectory function, we pass dirname and filename
but we were checking only the combined length of these two names.

Here, I am attaching a patch which will check lengths of dirname and
filename separately and will report errors if the name is too long.

I added a check in some other parts also to report an error for "too long
name".

Please review the attached patch and let me know feedback.

[1]: /messages/by-id/202504110938.4kx73ylnv6p4@alvherre.pgsql
/messages/by-id/202504110938.4kx73ylnv6p4@alvherre.pgsql

--
Thanks and Regards
Mahendra Singh Thalor
EnterpriseDB: http://www.enterprisedb.com

Attachments:

t51420_1
v01-add-some-more-error-checks-into-_fileExistsInDirecto.patchapplication/octet-stream; name=v01-add-some-more-error-checks-into-_fileExistsInDirecto.patchDownload+53-13
#2Daniel Gustafsson
daniel@yesql.se
In reply to: Mahendra Singh Thalor (#1)
Re: add some more error checks into _fileExistsInDirectory function to report "too long name" error

On 11 Apr 2025, at 14:26, Mahendra Singh Thalor <mahi6run@gmail.com> wrote:

Hi,
In another thread[1], Álvaro gave some feedback for _fileExistsInDirectory function for "too long name" error.
Basically, in _fileExistsInDirectory function, we pass dirname and filename but we were checking only the combined length of these two names.

My interpretation of the original problem in the other thread is that the
errormessage isn't applicable for a generic function as it only mention
directory, not that checking the combination is inherently wrong.

Here, I am attaching a patch which will check lengths of dirname and filename separately and will report errors if the name is too long.

Since we only care about the combination of directory and filename, do we
really gain much by using separate checks? A proposed filename exceeding
MAXPGPATH should be pretty rare in production I'd hope.

+   if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH)
+       pg_fatal("combined name of directory:\"%s\" and file:\"%s\" is too long", filename, dir);

snprintf() will return a negative value in case of an error so if we really
want to clamp down on path generation we should probably check that as well.

--
Daniel Gustafsson