[Nut-upsdev] zero-prepended values - why oh why?

Arnaud Quette aquette.dev at gmail.com
Thu Mar 2 09:58:53 UTC 2006


2006/2/28, Peter Selinger <selinger at mathstat.dal.ca>:
> ...
>
> I apologize if I am about to state the obvious, but when changing %f
> to %d, you will get garbage output unless you also insert an (int)
> typecast (except in the few cases where the compiler might do this
> automatically).  The reason this is relevant is that certain drivers,
> such as newhidups and mge-shut, keep the format strings in a big
> lookup table, and the driver makes certain assumptions about the types
> of the variables being formatted. I was a bit surprised when I saw
> changes such as the following in Arnaud's latest commit:
>
> -       { "battery.runtime", 0, 0, "UPS.PowerSummary.RunTimeToEmpty", "%05d", NULL, SHUT_FLAG_OK, NULL },
> +       { "battery.runtime", 0, 0, "UPS.PowerSummary.RunTimeToEmpty", "%.0f", NULL, SHUT_FLAG_OK, NULL },
>
> This doesn't seem like the kind of thing that will work. Note how this
> is being used (item->fmt is the format string from the above table,
> item->hid2info is the last table entry, which is NULL in this
> example):
>
> mge-shut.c:206:
>             if (item->hid2info != NULL)
>               {
>                nutvalue = hu_find_infoval(item->hid2info, (long)hData.Value);
>                if (nutvalue != NULL)
>                  dstate_setinfo(item->type, "%s", nutvalue);
>                else
>                  dstate_setinfo(item->type, item->fmt, hData.Value);
>               }
>             else
>               dstate_setinfo(item->type, item->fmt, hData.Value);
>
> Also observe that hData.Value is of type "long" (hidtypes.h:102).
> Applying dstate_setinfo with format "%.0f" to a value of type "long"
> should produce garbage, as there is no way the compiler can figure out
> to do a typecast in this situation.
>
> Arnaud, did you test this, and did it work? I think it shouldn't.
> ...

another good example which shows that I should not commit from home
(no lab anymore), late in the evening, being tired and having my mind
away on some other things.
so, no, it was not tested, and looking back, I've made a big b...shit...

I gotta check all that when I got a bit of time.
Peter: thanks for pointing that out

Arnaud



More information about the Nut-upsdev mailing list