Re: Redundant validity check in stock code.

From: Daniel A. Koepke (dkoepke@circlemud.org)
Date: 07/26/02


On Fri, 26 Jul 2002, Thomas Arp wrote:

> That's what I get for believing the website (2.0.6), and not
> double-checking.

To be completely clear, C99 conformant libraries will always return a
non-negative result.  Pre-standard versions may return -1.  The manuals do
an awfully poor job of conveying this meaning in a clear, concise fashion,
so it's worth repeating.

For portability, given,

  char buf[constant size];
  int len = 0;
  int nb;

you should use the idiom:

  nb = snprintf(buf + len, ...);

  if (nb + len > sizeof(buf) || nb < 0) {
    .
    . /* failed */
    .
  }

  len += nb;

-dak

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT