Opened 5 years ago

Closed 3 years ago

#2438 closed defect (fixed)

Vect_get_line_type cannot handle line out of range

Reported by: artegion Owned by: grass-dev@…
Priority: normal Milestone: 7.0.5
Component: LibVector Version: unspecified
Keywords: Cc:
CPU: Unspecified Platform: Unspecified

Description

calling Vect_get_line_type with line out of range (i.e. line=0) Vect_line_alive returns -1 but Vect_get_line_type ignores it (not -1 is False just like not 1) -> access violation reading ....

>>> from grass.lib.vector import  Vect_line_alive
>>> from grass.lib.vector import  Vect_get_line_type
>>> from grass.pygrass vector import VectorTopo
>>> a= VectorTopo('test')
>>> a.open()
>>> Vect_line_alive(a.c_mapinfo, 1)
1
>>> Vect_get_line_type(a.c_mapinfo, 1)
1
>>> Vect_line_alive(a.c_mapinfo, 0)
-1
>>> not Vect_line_alive(a.c_mapinfo, 0)
False
>>> Vect_get_line_type(a.c_mapinfo, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
WindowsError: exception: access violation reading 0x736C0014
>>>

Change History (6)

comment:1 in reply to:  description ; Changed 5 years ago by mmetz

Resolution: invalid
Status: newclosed

Replying to artegion:

calling Vect_get_line_type with line out of range (i.e. line=0) Vect_line_alive returns -1 but Vect_get_line_type ignores it (not -1 is False just like not 1) -> access violation reading ....

This is a programmer's error. 'Vect_get_line_type()' assumes that the programmer first tested if the provided line id is indeed valid. You did call 'Vect_line_alive()' first, you can only proceed if the line is alive. Fix your code.

comment:2 in reply to:  1 ; Changed 5 years ago by artegion

Resolution: invalid
Status: closedreopened

Replying to mmetz:

Replying to artegion:

calling Vect_get_line_type with line out of range (i.e. line=0) Vect_line_alive returns -1 but Vect_get_line_type ignores it (not -1 is False just like not 1) -> access violation reading ....

This is a programmer's error. 'Vect_get_line_type()' assumes that the programmer first tested if the provided line id is indeed valid. You did call 'Vect_line_alive()' first, you can only proceed if the line is alive. Fix your code.

For sure calling 'Vect_get_line_type()' with a wrong line id is a programmer error but 'Vect_get_line_type()' already call 'Vect_line_alive()'.

  258 int Vect_get_line_type(const struct Map_info *Map, int line)
  259 {
  260     check_level(Map);
  261     
  262     if (!Vect_line_alive(Map, line))
  263         return 0;
  264         
  265     return (Map->plus.Line[line]->type);
  266 }

Testing if (!Vect_line_alive(Map, line)) is wrong because Vect_line_alive returns

1 feature alive

0 feature is dead

-1 on error

a small change ( if (Vect_line_alive(Map, line!=1)) may improve code robustness.

comment:3 in reply to:  2 Changed 5 years ago by artegion

sorry: if (Vect_line_alive(Map, line)!=1)

comment:4 in reply to:  2 Changed 5 years ago by mmetz

Milestone: 6.4.57.0.0

Replying to artegion:

Replying to mmetz:

Replying to artegion:

calling Vect_get_line_type with line out of range (i.e. line=0) Vect_line_alive returns -1 but Vect_get_line_type ignores it (not -1 is False just like not 1) -> access violation reading ....

This is a programmer's error. 'Vect_get_line_type()' assumes that the programmer first tested if the provided line id is indeed valid. You did call 'Vect_line_alive()' first, you can only proceed if the line is alive. Fix your code.

For sure calling 'Vect_get_line_type()' with a wrong line id is a programmer error but 'Vect_get_line_type()' already call 'Vect_line_alive()'.

Testing if (!Vect_line_alive(Map, line)) is wrong because Vect_line_alive returns

1 feature alive

0 feature is dead

-1 on error

a small change ( if (Vect_line_alive(Map, line!=1)) may improve code robustness.

The problem comes from r55582 which introduced return -1 on error for the Vect_*_alive functions. Unfortunately, the existing calls to Vect_*_alive() were not adjusted. I have restored the behaviour of the Vect_*_alive() functions in r62370,1, no more need to change Vect_get_line_type(). Note that this applies only to G7, not to G6.

comment:5 Changed 3 years ago by martinl

Milestone: 7.0.07.0.5

comment:6 Changed 3 years ago by martinl

Resolution: fixed
Status: reopenedclosed
Note: See TracTickets for help on using tickets.