Fix an off-by-one error in `perror()`.
authorDan Cross <crossd@gmail.com>
Wed, 25 May 2016 16:09:45 +0000 (12:09 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Fri, 27 May 2016 23:38:07 +0000 (19:38 -0400)
The `perror` function returns a printable string corresonding
to the current error.  Ours had an off-by-one bug due to an
incorrect use of `strncpy`.

Change-Id: I54a4314c723aa69480b4ef77da39973a4bbc58a2
Signed-off-by: Dan Cross <crossd@gmail.com>
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
tools/compilers/gcc-glibc/glibc-2.19-akaros/sysdeps/akaros/perror.c

index 3e23a75..16ad10d 100644 (file)
@@ -57,10 +57,11 @@ void
 perror (const char *s)
 {
   int errnum = errno;
-  char errstr_save[MAX_ERRSTR_LEN];
+  char errstr_save[MAX_ERRSTR_LEN + 1];
   FILE *fp;
   int fd = -1;
   strncpy(errstr_save, errstr(), MAX_ERRSTR_LEN);
+  errstr_save[MAX_ERRSTR_LEN] = '\0';
 
   /* The standard says that 'perror' must not change the orientation
      of the stream.  What is supposed to happen when the stream isn't