After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 640555 - Can't run foreach{} on a null iterable variable
Can't run foreach{} on a null iterable variable
Status: RESOLVED NOTABUG
Product: libgee
Classification: Platform
Component: general
0.6.x
Other Linux
: Normal normal
: ---
Assigned To: libgee-maint
libgee-maint
Depends on:
Blocks:
 
 
Reported: 2011-01-25 17:19 UTC by Philip Withnall
Modified: 2011-01-26 22:26 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Philip Withnall 2011-01-25 17:19:52 UTC
If I have a GeeCollection which is null and try to iterate over it using foreach{}, libgee crashes. It would be nice if it gracefully treated null collections as empty.

i.e. The following Vala:

Gee.Collection<Individual> foo = null;

foreach (var i in foo) {
  // Stuff
}

Gives (approximately) the following C:

_tmp1_ = gee_iterable_iterator ((GeeIterable*) foo);

and the gee_iterable_iterator() call crashes:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6271a4a in gee_iterable_iterator (self=0x0) at iterable.c:77
77		return GEE_ITERABLE_GET_INTERFACE (self)->iterator (self);
Comment 1 Maciej (Matthew) Piechotka 2011-01-25 20:22:26 UTC
(In reply to comment #0)
> If I have a GeeCollection which is null and try to iterate over it using
> foreach{}, libgee crashes. It would be nice if it gracefully treated null
> collections as empty.
> 
> i.e. The following Vala:
> 
> Gee.Collection<Individual> foo = null;
> 

Gee.Collection<Individual> foo = Collection.empty ();

> foreach (var i in foo) {
>   // Stuff
> }
> 
> Gives (approximately) the following C:
> 
> _tmp1_ = gee_iterable_iterator ((GeeIterable*) foo);
> 
> and the gee_iterable_iterator() call crashes:
> 
> Program received signal SIGSEGV, Segmentation fault.
> 0x00007ffff6271a4a in gee_iterable_iterator (self=0x0) at iterable.c:77
> 77        return GEE_ITERABLE_GET_INTERFACE (self)->iterator (self);

It is not libgee problem - it is vala as foreach is syntax sugar. Internally it tries to call ((Gee.Collection)null).iterator () which results in segfault.

Vala does not support calling any methods on nulls not mentioning the interfaces and I don't think it will ever will (and hardly any other language do). In any case there is workaround with the .empty() which you may find useful depending on problem. 

If you still think that is a bug please repopen it againt vala but I don't think it will be considered.
Comment 2 Philip Withnall 2011-01-26 22:26:26 UTC
(In reply to comment #1)
> (In reply to comment #0)
> > If I have a GeeCollection which is null and try to iterate over it using
> > foreach{}, libgee crashes. It would be nice if it gracefully treated null
> > collections as empty.
> > 
> > i.e. The following Vala:
> > 
> > Gee.Collection<Individual> foo = null;
> > 
> 
> Gee.Collection<Individual> foo = Collection.empty ();
> 
> > foreach (var i in foo) {
> >   // Stuff
> > }
> > 
> > Gives (approximately) the following C:
> > 
> > _tmp1_ = gee_iterable_iterator ((GeeIterable*) foo);
> > 
> > and the gee_iterable_iterator() call crashes:
> > 
> > Program received signal SIGSEGV, Segmentation fault.
> > 0x00007ffff6271a4a in gee_iterable_iterator (self=0x0) at iterable.c:77
> > 77        return GEE_ITERABLE_GET_INTERFACE (self)->iterator (self);
> 
> It is not libgee problem - it is vala as foreach is syntax sugar. Internally it
> tries to call ((Gee.Collection)null).iterator () which results in segfault.
> 
> Vala does not support calling any methods on nulls not mentioning the
> interfaces and I don't think it will ever will (and hardly any other language
> do). In any case there is workaround with the .empty() which you may find
> useful depending on problem. 
> 
> If you still think that is a bug please repopen it againt vala but I don't
> think it will be considered.

Fair enough, that makes sense. Unfortunately, .empty() won't work for us, so we'll just continue to wrap our foreach blocks in if statements. Thanks.