home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.sys.sun.misc:4149 comp.lang.c:13382
- 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
- From: kpv@ulysses.att.com (Phong Vo)
- Newsgroups: comp.sys.sun.misc,comp.lang.c
- Subject: strcpy bug on SUNs
- Keywords: strcpy, mmap, core dump
- Message-ID: <17202@ulysses.att.com>
- Date: 9 Sep 92 14:25:20 GMT
- Sender: netnews@ulysses.att.com
- Followup-To: poster
- Lines: 37
-
- Below is a bug with strcpy() on SUN4 that Glenn Fowler and I came across
- recently. It has to do with strcpy(to,from) reading beyond the last
- byte of "from" when it is not aligned but "to" is aligned. I am
- cross-posting this to comp.lang.c since this bug is relevant to the recent
- discussion there on optimizing strcpy() using word copy. It shows why
- such optimizations must be thought out very carefully.
-
- ---cut here, compile and run-----------------------------------
- #include <sys/types.h>
- #include <fcntl.h>
- #include <sys/mman.h>
-
- main()
- {
- char* map;
- char buf[8192];
- int i;
-
- /* create a file with 8191 a's and a \0 */
- for(i = 0; i < sizeof(buf)-1; ++i)
- buf[i] = 'a';
- buf[i] = 0;
- i = creat("xxx",0644);
- write(i,buf,sizeof(buf));
- close(i);
-
- /* map the file in */
- i = open("xxx",O_RDONLY);
- unlink("xxx");
- map = mmap((caddr_t)0,8192,PROT_READ,MAP_PRIVATE,i,0L);
-
- if(map && map != (char*)(-1) )
- strcpy(buf,map+1); /* this dumps core */
- }
-
- Phong Vo, kpv@ulysses.att.com
- AT&T Bell Labs, 600 Mountain Ave, Murray Hill, NJ07974
-