snprintf question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruno van Dooren

    #1

    snprintf question

    Hi,

    this might be a strange question, but why is snprintf not guaranteed to nul
    terminate a string?
    i thought the whole point of those sn functions was to prevent these errors.

    sure, they prevent buffer overwrites, but callers can still screw up bad if
    they don't always make sure for themselves that the 0 is there before they
    let someone else read the contents.

    now you still have the problem that you have to make that check everywhere,
    instead of not having to bother.

    kind regards,
    Bruno.


  • William DePalo [MVP VC++]

    #2
    Re: snprintf question

    "Bruno van Dooren" <bruno_nos_pam_ van_dooren@hotm ail.com> wrote in message
    news:eKcJxSWuFH A.3500@TK2MSFTN GP09.phx.gbl...[color=blue]
    > this might be a strange question, but why is snprintf not guaranteed to
    > nul terminate a string?[/color]

    History, mostly.

    As I see it, there are two ways to go when the string being written exceeds
    the space avaialble to it.

    1) truncate and null terminate
    2) just truncate

    C has always had the mindset that the programmer knows what he is doing or
    should know. So, it chooses option 2.
    [color=blue]
    > i thought the whole point of those sn functions was to prevent these
    > errors.[/color]

    You may be thinking of the new "safe" string functions

    http://msdn.microsoft.com/library/de...re03102004.asp

    [color=blue]
    > sure, they prevent buffer overwrites, but callers can still screw up bad
    > if they don't always make sure for themselves that the 0 is there before
    > they let someone else read the contents.[/color]

    Yup. The docs do mention in their security note that

    snprintf(s, ...)

    should be followed by

    s[n - 1] = 0;

    where n = the number of characters in s.
    [color=blue]
    > now you still have the problem that you have to make that check
    > everywhere, instead of not having to bother.[/color]

    Yup. Sadly, such is life. Since C "strings" are just arrays, and since
    "strings" are passed by simple pointer, called functions have no
    authoritative source for string length.

    Regards,
    Will


    Comment

    • Ronald Laeremans [MSFT]

      #3
      Re: snprintf question

      Bruno van Dooren wrote:[color=blue]
      > Hi,
      >
      > this might be a strange question, but why is snprintf not guaranteed to nul
      > terminate a string?
      > i thought the whole point of those sn functions was to prevent these errors.
      >
      > sure, they prevent buffer overwrites, but callers can still screw up bad if
      > they don't always make sure for themselves that the 0 is there before they
      > let someone else read the contents.
      >
      > now you still have the problem that you have to make that check everywhere,
      > instead of not having to bother.
      >
      > kind regards,
      > Bruno.
      >
      >[/color]

      Hi Bruno,

      Take a look at the following link:
      http://msdn.microsoft.com/library/de...re03102004.asp

      Ronald Laeremans
      Visual C++ team

      Comment

      Working...