Re: Crashing g_string_sprintf when printing invalid floats. gtk+-1.2.9 on Linux



Chris Wareham wrote:
Eric M. Monsler wrote:


Inspecting errno when?

 >

tmp_float = (float)strtod("6.0e-44",NULL);

/* inspect errno and tmp_float here - see strtod(3) */

if(errno == ERANGE) {
    g_print("bad argument to strtod()\n");
    exit(1);
} else if(tmp_float == FLT_MIN || tmp_float == FLT_MAX) {
    g_print("float out of range\n");
    exit(1);
}

g_string_sprintf(pGStr,"%6.2f\n",tmp_float);

Even better would be to skip the cast, make tmp_float
into tmp_double, and just check errno. This is because
FLT_MIN and FLT_MAX may not be portable beyond machines
with gcc on them.


And of course you're already fucked if the return from
strtod is greater than FLT_MAX or less than FLT_MIN.
So the "else if" part of the code above is redundant.

Chris

--
chris wareham iosystems co uk (work)
cwareham btinternet com (home)




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]