[GRASS-dev] bug in vector network analysis node costs

Markus Metz markus.metz.giswork at gmail.com
Tue Jun 11 02:27:39 PDT 2013

On Mon, Jun 10, 2013 at 10:29 PM, Štěpán Turek <stepan.turek at seznam.cz> wrote:
> Hi all,
> Probably I found bug in nodes cost in vector network analysis (tested in G7,
> probably it affects all branches). Problem is in lib/vector/Vlib/net.c:
> int cost;
> ...
> (* dglInt32_t)(dglInt32_t) & cost
> int cost is cast into dglInt32_t pointer. DGLib dglInt32_t is long.  I am
> relatively new to C so I do not understand the way it is casted and I can
> miss something. I am reading it as cast address of  cost int to  dglInt32_t
> and then to pointer to dglInt32_t. It does not make sense to me but it
> compiles :-)
> The problem is when you have on your system different size of long and int,
> it is probably also the reason why it was not noticed so far (on 32 bit
> usually sizeof(int) == sizeof(long)). Because it seems that during this cast
> there are added bytes to be same size as long. However these bytes have
> random values and therefore results costs do not make sense.

I think the reason why this has not been noticed yet is that not many
users are working with node costs. 64 bit systems with sizeof(long) >
sizeof(int) are commonly used for quite a few years by now.

The type name dglInt32_t is confusing because it is not necessarily a
32bit integer, it is an integer with at least 32 bits.

> If this change is made everything seems ok:
> dglInt32_t dgl_cost;
> ...
> dgl_cost = cost;
> dglNodeSet_Attr(gr, dglGetNode(gr, (dglInt32_t) cat),
> & dgl_cost);
> Now there is:
> dglNodeSet_Attr(gr, dglGetNode(gr, (dglInt32_t) cat),
> (* dglInt32_t)(dglInt32_t) & cost);
> I have attached also patch.

Makes sense. I have applied the patch to all branches in r56680-2.

Markus M

More information about the grass-dev mailing list