Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!sun-barr!oliveb!tymix!tardis!jms From: jms@tardis.Tymnet.COM (Joe Smith) Newsgroups: comp.sys.amiga.tech Subject: Re: strins() Summary: infinite loop using sprintf Keywords: Lattice C Manx Aztec C strins() sprintf Message-ID: <254@tardis.Tymnet.COM> Date: 14 Jun 89 00:07:01 GMT References: <0929.AA0929@caleb> <109394@sun.Eng.Sun.COM> <10285@watcgl.waterloo.edu> Reply-To: jms@tardis.Tymnet.COM (Joe Smith) Organization: McDonnell Douglas Field Service Co, San Jose CA Lines: 22 [I predict there will be over 25 followups to Geo's article.] In article <10285@watcgl.waterloo.edu> gswan@watcgl.waterloo.edu (Geo Swan) writes: >Let me suggest that our original poster consider using the following macro: >#define strins( s1, s2 ) sprintf( s1, "%s%s", s2, s1 ) Can you guarantee that the above macro will work when s2 is longer than s1? Example: Let's say s1 is a pointer to an array of 80 bytes, and only the first two bytes have any characters, the rest are null. Let's say s2 is a pointer to a string of 4 bytes. As sprintf copies s2 into s1, it overwrites the original string. If sprintf copies until len(s1), then it will return a pointer to an 8 byte string - s2 concatenated with s2. If sprintf copies until it gets to the null byte, it will never find it - s1 will get an infinite number of copies of s2. Or guru. It's OK to use sprintf as long as the destination does not include any of the sources. -- Joe Smith (408)922-6220 | SMTP: JMS@F74.TYMNET.COM or jms@tymix.tymnet.com McDonnell Douglas FSCO | UUCP: ...!{ames,pyramid}!oliveb!tymix!tardis!jms PO Box 49019, MS-D21 | PDP-10 support: My car's license plate is "POPJ P," San Jose, CA 95161-9019 | narrator.device: "I didn't say that, my Amiga did!"