Sponsor FloSync Free digital signage & video wall software for Mac and Windows Download Free

Re: sprintf() bug?

Info-Mac discussion from 1984 - 2002.
Locked
User avatar
Info-Mac
Administrator
Posts: 13716
Joined: December 21st, 1988, 11:00 am

Re: sprintf() bug?

Post by Info-Mac »

Xref: utzoo comp.sys.mac:12555 comp.sys.mac.digest:118 comp.sys.mac.hypercard:355
Newsgroups: comp.sys.mac,comp.sys.mac.digest,comp.sys.mac.hypercard
Path: utzoo!utgpu!jarvis.csri.toronto.edu!ai.toronto.edu!dudek
From: [email protected] (Gregory Dudek)
Subject: Re: sprintf() bug?
Message-ID:
Keywords: C,sprintf,strings
Reply-To: [email protected] (Gregory Dudek)
Organization: Department of Computer Science, University of Toronto
References:
Date: Tue, 16-Feb-88 17:28:25 EST

Just to correct some apparent misinformation that has been propagated
on this subjec...

In article [email protected] (David Palmer) writes:
>In article [email protected] (Gray Watson) writes:
>>> sprintf(tempstring, "%s %f %s %f", "\pthe square of ", x, "is", (x *x));
>>> DrawString(tempstring);

[ etc, etc ]
>>
>
>The string produced by "\pthe square of " is NOT null terminated (it is
>a Pascal string, and so the length is specified by a byte at the beginning,
>...
>True, but the '\p' in "\pHello Dolly" is just a directive to tell the
>compiler to count the characters for itself, and the string is stored in
>memory as "\013Hello Dolly", with no null termination.
>

Aztec C produces a C-string *WITH* a null terminator when it sees:
"\Pfoo". In other words it's a legal Pascal string but has an EXTRA
character (null) that Pascal routines won't notice, thus making it a
legal C-string also. This seems like a very sensible idea since C
routines invoked on it won't blow up & things like strcpy still work.
I don't know what other complers do, but I would be seem reasonable if they
did the same.
Of course, this doesn't solve the original poster's problem. He should
use something like:
sprintf(tmp,"%s %d ect",...); DrawString(CtoPascal(tmp));
where CtoPascal is appropriately defined (most compilers include
a library routine that does this).

As for glue routines, Aztec C uses them only for *some* ROM traps; most
are defined as straight ROM entries.

Greg Dudek
--
Dept. of Computer Science (vision group) University of Toronto
Reasonable mailers: [email protected]
Other UUCP: {uunet,ihnp4,decvax,linus,pyramid,
dalcs,watmath,garfield,ubc-vision,calgary}!utai!dudek
ARPA: user%[email protected]
Locked