1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-10 17:42:29 +03:00

Fix misuse of StrNCpy to copy and add null to non-null-terminated data.

Does not work since it fetches one byte beyond the source data, and when
the phase of the moon is wrong, the source data is smack up against the
end of backend memory and you get SIGSEGV.  Don't laugh, this is a fix
for an actual user bug report.
This commit is contained in:
Tom Lane
2000-07-07 21:12:53 +00:00
parent de85dd1d51
commit 65da0d66b4
9 changed files with 56 additions and 43 deletions

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/libpq/be-fsstubs.c,v 1.48 2000/07/03 23:09:37 wieck Exp $
* $Header: /cvsroot/pgsql/src/backend/libpq/be-fsstubs.c,v 1.49 2000/07/07 21:12:53 tgl Exp $
*
* NOTES
* This should be moved to a more appropriate place. It is here
@@ -379,26 +379,23 @@ lo_import(PG_FUNCTION_ARGS)
/*
* open the file to be read in
*/
nbytes = VARSIZE(filename) - VARHDRSZ + 1;
if (nbytes > FNAME_BUFSIZE)
nbytes = FNAME_BUFSIZE;
StrNCpy(fnamebuf, VARDATA(filename), nbytes);
nbytes = VARSIZE(filename) - VARHDRSZ;
if (nbytes >= FNAME_BUFSIZE)
nbytes = FNAME_BUFSIZE-1;
memcpy(fnamebuf, VARDATA(filename), nbytes);
fnamebuf[nbytes] = '\0';
fd = PathNameOpenFile(fnamebuf, O_RDONLY | PG_BINARY, 0666);
if (fd < 0)
{ /* error */
elog(ERROR, "lo_import: can't open unix file \"%s\": %m",
fnamebuf);
}
/*
* create an inversion "object"
*/
lobj = inv_create(INV_READ | INV_WRITE);
if (lobj == NULL)
{
elog(ERROR, "lo_import: can't create inv object for \"%s\"",
fnamebuf);
}
/*
* the oid for the large object is just the oid of the relation
@@ -461,18 +458,17 @@ lo_export(PG_FUNCTION_ARGS)
* 022. This code used to drop it all the way to 0, but creating
* world-writable export files doesn't seem wise.
*/
nbytes = VARSIZE(filename) - VARHDRSZ + 1;
if (nbytes > FNAME_BUFSIZE)
nbytes = FNAME_BUFSIZE;
StrNCpy(fnamebuf, VARDATA(filename), nbytes);
nbytes = VARSIZE(filename) - VARHDRSZ;
if (nbytes >= FNAME_BUFSIZE)
nbytes = FNAME_BUFSIZE-1;
memcpy(fnamebuf, VARDATA(filename), nbytes);
fnamebuf[nbytes] = '\0';
oumask = umask((mode_t) 0022);
fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
umask(oumask);
if (fd < 0)
{ /* error */
elog(ERROR, "lo_export: can't open unix file \"%s\": %m",
fnamebuf);
}
/*
* read in from the Unix file and write to the inversion file