home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / bsd / 11728 < prev    next >
Encoding:
Text File  |  1993-01-23  |  2.0 KB  |  97 lines

  1. Newsgroups: comp.unix.bsd
  2. Path: sparky!uunet!munnari.oz.au!metro!ipso!runxtsa!bde
  3. From: bde@runx.oz.au (Bruce Evans)
  4. Subject: [386BSD] Bug + fix: getcwd off by one
  5. Message-ID: <1993Jan22.185211.5564@runx.oz.au>
  6. Organization: RUNX Un*x Timeshare.  Sydney, Australia.
  7. Date: Fri, 22 Jan 93 18:52:11 GMT
  8. Lines: 87
  9.  
  10. getcwd() has two off-by-one bugs in 386BSD-0.1:
  11.  
  12. 1. getcwd(buf, size) fails when the size is just large enough.
  13. 2. getcwd(buf + 1, 1) incorrectly succeeds when the current directory
  14.    is "/".  buf[0] and buf[2] are clobbered.
  15.  
  16. This program demonstrates the bug:
  17.  
  18. ---
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23.  
  24. int main(void)
  25. {
  26.     char buf[5];
  27.     int errors;
  28.  
  29.     errors = 0;
  30.     if (chdir("/tmp") != 0) {
  31.     perror("chdir");
  32.     abort();
  33.     }
  34.     if (getcwd(buf, 5) == NULL) {
  35.     perror("oops, getcwd failed for buffer size = size required");
  36.     ++errors;
  37.     }
  38.     if (chdir("/") != 0) {
  39.     perror("chdir");
  40.     abort();
  41.     }
  42.     buf[0] = 0;
  43.     buf[2] = 1;
  44.     if (getcwd(buf + 1, 1) != NULL) {
  45.     fprintf(stderr,
  46.         "oops, getcwd succeeded for buffer size = one too small\n");
  47.     ++errors;
  48.     }
  49.     if (buf[0] != 0) {
  50.     fprintf(stderr,
  51.         "oops, getcwd scribbled on memory before start of buffer\n");
  52.     ++errors;
  53.     }
  54.     if (buf[2] != 1) {
  55.     fprintf(stderr,
  56.         "oops, getcwd scribbled on memory after end of buffer\n");
  57.     ++errors;
  58.     }
  59.     exit(errors == 0 ? 0 : 1);
  60. }
  61. ---
  62.  
  63. This might be a fix:
  64.  
  65. ---
  66. *** /usr/src/lib/libc/gen/getcwd.c~    Wed May  1 10:35:50 1991
  67. --- /usr/src/lib/libc/gen/getcwd.c    Tue Jan 19 22:22:38 1993
  68. ***************
  69. *** 74,78 ****
  70.       if (pt) {
  71.           ptsize = 0;
  72. !         if (!size) {
  73.               errno = EINVAL;
  74.               return((char *)NULL);
  75. --- 74,78 ----
  76.       if (pt) {
  77.           ptsize = 0;
  78. !         if (size < 2) {
  79.               errno = EINVAL;
  80.               return((char *)NULL);
  81. ***************
  82. *** 186,190 ****
  83.            * leading slash.
  84.            */
  85. !         if (bpt - pt <= dp->d_namlen + (first ? 1 : 2)) {
  86.               size_t len, off;
  87.   
  88. --- 186,190 ----
  89.            * leading slash.
  90.            */
  91. !         if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
  92.               size_t len, off;
  93.   
  94. ---
  95. -- 
  96. Bruce Evans  (bde@runx.oz.au)
  97.