[GRASS-dev] Re: [GRASS GIS] #718: r.li forgets mask/illegal filename
GRASS GIS
trac at osgeo.org
Tue Aug 18 09:25:31 EDT 2009
#718: r.li forgets mask/illegal filename
------------------------+---------------------------------------------------
Reporter: kyngchaos | Owner: grass-dev at lists.osgeo.org
Type: defect | Status: new
Priority: normal | Milestone: 6.4.0
Component: Raster | Version: 6.4.0 RCs
Resolution: | Keywords: r.li, smp
Platform: All | Cpu: All
------------------------+---------------------------------------------------
Comment (by glynn):
Replying to [comment:19 hamish]:
> I still can't find any joy with gdb so resorting to sticking printf()s
everywhere. With that I can see that it is falling over in
r.li.daemon/list.c's insertNode() on the first malloc() on the second time
that function is called.
> source:grass/trunk/raster/r.li/r.li.daemon/list.c at 38777#L32
> unfortunately I'm guessing that just means that the memory is already
corrupt by the time it gets there. :(
That's what it looks like.
Oh, look at insertNode() in list.c:
{{{
void insertNode(list l, msg mess)
{
node new;
new = malloc(sizeof(node));
new->m = malloc(sizeof(msg));
}}}
Even without reading what follows, the above should smell fishy. Don't
malloc() calls usually look like "TYPE *x = malloc(N * sizeof(TYPE))"?
I.e. shouldn't the type of the return value be a pointer to whatever type
appears in the sizeof?
Note: list.h says:
{{{
typedef struct nodoLista *node;
}}}
So "node" is a typedef for a pointer to the structure, so the malloc() is
only allocating enough memory for the pointer, not the structure.
This is just one reason why sane programmers very rarely use typedefs, and
almost never for pointer types.
[They may sometimes be used to hide implementation details in public
interfaces, e.g. "typedef void *HANDLE", but the actual type would
normally be used in the implementation.]
For the sake of anyone who has to clear up any other mistakes in r.li
(given the overall quality, I doubt that this is the only one), I would
ask whoever fixes this to finish the job and eliminate the typedefs
altogether. If something is a pointer to a structure, it should look like
"struct foo *p", not "foo p".
Oh, and bonus points for changing the type names to English, like the
other 99.99% of the GRASS codebase.
--
Ticket URL: <https://trac.osgeo.org/grass/ticket/718#comment:21>
GRASS GIS <http://grass.osgeo.org>
More information about the grass-dev
mailing list