home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sys / sun / misc / 4149 < prev    next >
Encoding:
Text File  |  1992-09-09  |  1.5 KB  |  50 lines

  1. Xref: sparky comp.sys.sun.misc:4149 comp.lang.c:13382
  2. Path: sparky!uunet!spool.mu.edu!caen!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!ulysses!ulysses.att.com!kpv
  3. From: kpv@ulysses.att.com (Phong Vo)
  4. Newsgroups: comp.sys.sun.misc,comp.lang.c
  5. Subject: strcpy bug on SUNs
  6. Keywords: strcpy, mmap, core dump
  7. Message-ID: <17202@ulysses.att.com>
  8. Date: 9 Sep 92 14:25:20 GMT
  9. Sender: netnews@ulysses.att.com
  10. Followup-To: poster
  11. Lines: 37
  12.  
  13. Below is a bug with strcpy() on SUN4 that Glenn Fowler and I came across
  14. recently. It has to do with strcpy(to,from) reading beyond the last
  15. byte of "from" when it is not aligned but "to" is aligned. I am
  16. cross-posting this to comp.lang.c since this bug is relevant to the recent
  17. discussion there on optimizing strcpy() using word copy. It shows why
  18. such optimizations must be thought out very carefully.
  19.  
  20. ---cut here, compile and run-----------------------------------
  21. #include    <sys/types.h>
  22. #include    <fcntl.h>
  23. #include    <sys/mman.h>
  24.  
  25. main()
  26. {
  27.     char*    map;
  28.     char    buf[8192];
  29.     int    i;
  30.  
  31.     /* create a file with 8191 a's and a \0 */
  32.     for(i = 0; i < sizeof(buf)-1; ++i)
  33.         buf[i] = 'a';
  34.     buf[i] = 0;
  35.     i = creat("xxx",0644);
  36.     write(i,buf,sizeof(buf));
  37.     close(i);
  38.  
  39.     /* map the file in */
  40.     i = open("xxx",O_RDONLY);
  41.     unlink("xxx");
  42.     map = mmap((caddr_t)0,8192,PROT_READ,MAP_PRIVATE,i,0L);
  43.  
  44.     if(map && map != (char*)(-1) )
  45.         strcpy(buf,map+1);    /* this dumps core */
  46. }
  47.   
  48. Phong Vo, kpv@ulysses.att.com
  49. AT&T Bell Labs, 600 Mountain Ave, Murray Hill, NJ07974
  50.