[GRASS5] How to get monitor background color?
glynn.clements at virgin.net
Fri Oct 26 17:06:47 EDT 2001
Markus Neteler wrote:
> > Instead, you should first ensure that colorname is pointing to some
> > allocated memory large enough to hold the resulting color. Something
> > like:
> > char colorname[MAX_COLOR_LEN];
> > I have no idea what value might be appropriate for MAX_COLOR_LEN.
> > (And if there isn't a well-defined value for this somewhere, then
> > D_get_erase_color should be updated to accept a maximum length as an
> > input parameter or to allocate memory for the result itself).
> Thanks for the hints, Carl,
> however, I am still a bit stuck here. Perhaps our display gurus
> have a recommendation?
There isn't a maximum length; D_get_erase_color() returns whatever was
passed to D_get_erase_color().
Certainly, Carl is right that functions which accept pointers to
"output" arrays (ones into which results are written) should also
accept a size argument. But then how to deal with overflows?
Truncating the result to fit prevents crashes, but may cause more
subtle errors. G_fatal_error() isn't particularly friendly. Having the
caller enlarge the buffer then try again is the right approach, but
probably won't happen. Better to use G_store() and return a pointer;
that way, the worst case is a memory leak.
Also, I suspect that this is far from the only place in GRASS where
this occurs. GRASS is littered with fixed-size buffers; in most cases,
it's just assumed that the buffer will be large enough.
Glynn Clements <glynn.clements at virgin.net>
More information about the grass-dev