Re: "NUL" device is missing

From: Chuck Chopp (ChuckChopp_at_rtfmcsi.com)
Date: 05/14/04


Date: Fri, 14 May 2004 07:16:22 -0400

KM wrote:

> What VC version you are building against?
> The reason I am asking is that the issue has been fixed in C standard of 99
> year. Only VC 2003 has peaked up the fix.
> The fix was to pass NULL to _snprintf to count the number of characters
> required for a specified formating string. Basically, you can make a call
> like: _snprintf(NULL,0,"%...",...) and it will return you the number.
>
> You can easy track this down exploring CRT source of VC. In VC 6.0
> (sprintf.h, output.c, stdio.h) you may see that the string formating routine
> subcalls do not check for NULL. In VC 2003 CRT, they do (output.c, write_xxx
> functions called from _output). This may be a cure for you. Also, there is
> another useful wrapper (don't know if it is ANSI compatible) -
> _scprintf(char* format,...) - this will return the same number of characters
> you need.

I'm stuck using Visual C/C++ v6.0 right now w/o the option to move on up to
the Studio 2003 compiler. However, I will keep this in mind for future use.
  I spent quite a bit of time last night reviewing the source code for the
CRT and had reached the point where everything comes down to a call to the
_output() function that does all of the dirty work. If it is one of the
string printf() types of functions, then a dummy _iob [a.k.a. "FILE"
structure] is worked up where it lookes like a valid file handle but really
points to the string buffer. I can see the counted vs. uncounted versions
set a member in the structure to limit define the output buffer size.
Still, it is the _output() function that does all of the work and I haven't
gone through and reviewe the source code for _output to see if it tests
properly for NULL. I think that in VC6 it doesnn't as I've had errors occur
when passing in a NULL pointer to sprintf(). I'll look at it all in more
detail today.

It is encouraging to hear than an updated ANSI C standard does finally
address this problem, though. I've got VS2003 .NET on hand and I will
review the CRT source code from it, too, to see what the differences in the
implementation look like. Perhaps I can cook up my own flavor of _output()
to use with VC6 in my own code to back-port that functionality and thus
remove the dependency on the NUL device.

-- 
Chuck Chopp
ChuckChopp (at) rtfmcsi (dot) com http://www.rtfmcsi.com
RTFM Consulting Services Inc.     864 801 2795 voice & voicemail
103 Autumn Hill Road              864 801 2774 fax
Greer, SC  29651
Do not send me unsolicited commercial email.


Relevant Pages

  • Re: Why is it dangerous?
    ... learn that C had such weak string handling, ... Exploiting getson an auto buffer ... that omitted any standard on what could or could not be input. ... the pedants, on whose code contains more *actual* bugs. ...
    (comp.lang.c)
  • Re: Anyone object to the following change in libc?
    ... sscanfinput string. ... ISO C standard" refers to "c89", which is the version of the C ... non-sensical format specifier entirely, since any characters that ... conformance test suite for ISO C complaining about the ...
    (freebsd-current)
  • Re: A C++ Whishlist
    ... > people from inclusion in a standard. ... > creating their own string class. ... >>don't want an ever increasing size of exception specification on each ...
    (comp.lang.cpp)
  • Re: How to make Forth interesting?
    ... Standard Forth doesn't give you all the tools to do that. ... thought here is to set up some new wordlists whose hash function is the ... John Passaniti says if you have a language that's ... it might be useful to have more string stuff in Forth. ...
    (comp.lang.forth)
  • Re: string comparison
    ... Null termination is part of the definition of a string. ... of the Standard, or did you mean horribly designed? ... then why trust that the assembler will generate the ... Do you check the machine code to see if the ...
    (comp.lang.c)