vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Sven Dehmlow wrote: > On Mon, 16 May 2005, Han Boetes wrote: > > Han Boetes wrote: > > > Theo de Raadt wrote: > > > > snprintf does not return size_t > > > > > > sizeof returns size_t > > > > > > Alright... here is how I see it: > > > > Lets asume for simplicity sake that an int has a range from -128 > > to 127 and size_t has a range from 0 to 255. > > > > Now there are various ways to do this as I have seen: > > > > size_t ret; > > char foo[100]; > > > > ... > > ret = snprintf(foo, sizeof(foo), "%s", input); > > if ((int)ret == -1 || ret > = sizeof(foo)) > > ... > > > > input size: what happens: > > -------------------------------------------- > > illegal input snprintf returns -1, (int)ret == -1 works ok > > Han, what went wrong in your childhood? Comparing an unsigned > type with a signed value is absolutely evil! It may work, but it > is really bad style. It's a dirty hack, I'm not sure if the > result is even defined by the standard. OK, let me repeat the goal of my message: What is the proper way to check if snprintf did the right thing. Once I know that I can move along. Pointing out, with a lot of noise, I made an error doesn't make you look better, nor does it convince me you're trustable. > > RETURN VALUES > > The printf(), fprintf(), sprintf(), vprintf(), vfprintf(), vsprintf(), > > and vasprintf() functions return the number of characters printed (not > > including the trailing `\0' used to end output to strings). > > > > The snprintf() and vsnprintf() functions return the number of characters > > that would have been output if the size were unlimited (again, not > > including the final `\0'). If an output or encoding error occurs, a val- > > ue of -1 is returned instead. > > > > > > The right final sentence would be: > > > > If an input, output or encoding error occurs, a value of -1 is > > returned instead. > > And what if someone introduces finer grained error-results in > snprintf()? Your code would be immediately broken ... That's not even hypothetical. # Han |