* path.cc (readlink): Check if buffer length is positive. Truncate output to

buffer length.  Don't terminate buffer with '\0'.
This commit is contained in:
Christopher Faylor 2000-09-04 17:52:42 +00:00
parent 9c136d7ea6
commit f76325499a
14 changed files with 33 additions and 27 deletions

View File

@ -1,3 +1,9 @@
2000-09-03 Egor Duda <deo@logos-m.ru>
* path.cc (readlink): Check if buffer length is positive.
Truncate output to buffer length. Don't terminate buffer
with '\0'.
Sun Sep 3 00:38:40 2000 Christopher Faylor <cgf@cygnus.com> Sun Sep 3 00:38:40 2000 Christopher Faylor <cgf@cygnus.com>
* environ.cc (environ_init): Don't free the new environment table after * environ.cc (environ_init): Don't free the new environment table after

View File

@ -2436,6 +2436,13 @@ int
readlink (const char *path, char *buf, int buflen) readlink (const char *path, char *buf, int buflen)
{ {
extern suffix_info stat_suffixes[]; extern suffix_info stat_suffixes[];
if (buflen < 0)
{
set_errno (ENAMETOOLONG);
return -1;
}
path_conv pathbuf (path, PC_SYM_CONTENTS, stat_suffixes); path_conv pathbuf (path, PC_SYM_CONTENTS, stat_suffixes);
if (pathbuf.error) if (pathbuf.error)
@ -2452,14 +2459,8 @@ readlink (const char *path, char *buf, int buflen)
return -1; return -1;
} }
int len = strlen (pathbuf.get_win32 ()); int len = max (buflen, (int) strlen (pathbuf.get_win32 ()));
if (len > (buflen - 1))
{
set_errno (ENAMETOOLONG);
return -1;
}
memcpy (buf, pathbuf.get_win32 (), len); memcpy (buf, pathbuf.get_win32 (), len);
buf[len] = '\0';
/* errno set by symlink.check if error */ /* errno set by symlink.check if error */
return len; return len;

View File

@ -601,8 +601,7 @@ _link (const char *a, const char *b)
&dwBytesWritten, &dwBytesWritten,
TRUE, // abort TRUE, // abort
FALSE, // don't process security FALSE, // don't process security
&lpContext &lpContext);
);
} }
else else
syscall_printf ("cannot write streamId, %E"); syscall_printf ("cannot write streamId, %E");