home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / bsd / 11715 < prev    next >
Encoding:
Text File  |  1993-01-23  |  2.0 KB  |  87 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: NAME_MAX off by 1
  5. Message-ID: <1993Jan22.182148.5188@runx.oz.au>
  6. Organization: RUNX Un*x Timeshare.  Sydney, Australia.
  7. Date: Fri, 22 Jan 93 18:21:48 GMT
  8. Lines: 77
  9.  
  10. File names of length NAME_MAX do not work in 386BSD-0.1.  Names of length
  11. NAME_MAX - 1 are OK.  Reducing NAME_MAX by 1 to 254 would not help - it
  12. would invalidate the documentation and reduce the actual maximum by 1.
  13.  
  14. This program demonstrates the bug:
  15.  
  16. ---
  17. NAME_MAX-test.c
  18. ---
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <limits.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26.  
  27. int errors;
  28.  
  29. void test(size_t component_length, int expect_error);
  30.  
  31. int main(void)
  32. {
  33.     test(NAME_MAX, 1);
  34.     test(NAME_MAX + 1, 0);
  35.     exit(errors ? 1 : 0);
  36. }
  37.  
  38. void test(size_t component_length, int expect_error)
  39. {
  40.     int fd;
  41.     char *name;
  42.  
  43.     name = malloc(5 + component_length);
  44.     if (name == NULL)
  45.     {
  46.     perror("malloc");
  47.     abort();
  48.     }
  49.     sprintf(name, "/tmp/%0*d", (int) component_length, 0);
  50.     fd = creat(name, 0666);
  51.     if (fd >= 0) {
  52.     (void) unlink(name);
  53.     fprintf(stderr, "%s: length %d didn't cause an error\n",
  54.         expect_error ? "  OK" : "oops", (int) component_length);
  55.     if (expect_error)
  56.         ++errors;
  57.     }
  58.     else {
  59.     fprintf(stderr, "%s: length %d caused error `%s'\n",
  60.         !expect_error ? "  OK" : "oops", (int) component_length,
  61.         strerror(errno));
  62.     if (!expect_error)
  63.         ++errors;
  64.     }
  65. }
  66. ---
  67.  
  68. This might be a fix:
  69.  
  70. *** vfs_lookup.c~    Wed Dec 25 09:24:09 1991
  71. --- vfs_lookup.c    Tue Jan 19 21:25:50 1993
  72. ***************
  73. *** 275,279 ****
  74.           ndp->ni_hash += (unsigned char)*cp;
  75.       ndp->ni_namelen = cp - ndp->ni_ptr;
  76. !     if (ndp->ni_namelen >= NAME_MAX) {
  77.           error = ENAMETOOLONG;
  78.           goto bad;
  79. --- 275,279 ----
  80.           ndp->ni_hash += (unsigned char)*cp;
  81.       ndp->ni_namelen = cp - ndp->ni_ptr;
  82. !     if (ndp->ni_namelen > NAME_MAX) {
  83.           error = ENAMETOOLONG;
  84.           goto bad;
  85. -- 
  86. Bruce Evans  (bde@runx.oz.au)
  87.