These kids today…

Recently a newbie asked a question about formatting numbers into strings in C, and then I received an offline mail about my answer (this message was from some other chucklehead, not the original newbie, who seemed to appreciate my answer)…. The idiot was trying to tell me to only use snprintf “where it was necessary” (and use sprintf elsewhere), while I was arguing that using all the time was good practice and “safer” (if programmers were so good at seeing their own buffer overflows all the time Bugtraq wouldn’t be so necessary)… This joker simply wouldn’t even entertain the thought that the negligible performance penalty was worth it…

To me, while I recognize that sometimes “you know” that your buffer is large enough, but it seems to me that as soon as “you know” this is true, someone will come along and modify your code just enough to make it untrue… To me, good practice means doing your best to make sure your code is maintainable through the annals of time… Of course, I am probably pretty anal about this (since I seem to be the only person who advocates putting braces around one-line if blocks)…

Bitch off…

Update: I am glad to see so many other people do one-line conditionals the “right” way… =)

18 thoughts on “These kids today…

    1. “The proposed replacement functions, strlcpy() and strlcat(), address these problems by presenting an API designed for safe string copies (see Figure 1 for function prototypes.”

      Of course it’s not part of MSVC… Besides, if you really wanted to use them, it’s really not much work to write your own implementation, even if you did it in x86 asm for the speed.

        1. Actually, the newbie was running BeOS, but I do much of my work at work under Windows…. Unfortunately, though, this work is using the mozilla framework, and NSPR doesn’t have analogues of strl[cpy,cat]…

          Windows, itself, though, has the functions StrCpyN as an analogue of strlcpy, but as far as I can tell there is no simple analogue of strlcat… What I would do to get around this in windows, though, would be to use snprintf(…,”%s%s”,…); in it’s substitute…

          (note: snprintf is actually _snprintf in win32)

          1. but as far as I can tell there is no simple analogue of strlcat…

            Of course, someone reading the instructions would just choose Win32’s strncat functions.

            =)

    1. Re: One-line if blocks…

      I remember reading that article a while back, either you sent it to me or to noise… I am not sure what I think of it, in fact i have a difficult time figuring out whether it is satire or someone’s actual practice… =)

    1. The first few pages of O’Reilly’s “Practical C++ Programming” had a quote that has stuck with me for many years:

      C++ Order of Operations:

      1) Multiplication and Division before Addition and Subtraction
      2) Put Parentheses around everything else

      I think that sums it up pretty nicely… =)

Leave a Reply