bugGNU Octave - Bugs: bug #34967, Bad behavior of structures with an...


bug #34967: Bad behavior of structures with an empty cell

Submitted by:  Henrik Alsing Friberg <hfriberg>
Submitted on:  Fri 02 Dec 2011 10:30:48 AM UTC  
Category:  Interpreter Severity:  2 - Minor
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  Closed
Release:  dev Operating System:  Any

Add a New Comment (Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


( Jump to the original submission)

Tue 04 Jun 2019 03:14:16 PM UTC, comment #8: 

Well, only 7.5 years later, the last of the bugs in this report has been fixed.  See changeset (https://hg.savannah.gnu.org/hgweb/octave/rev/1bad33112465).

Marking as fixed and closing report.

Rik <rik5>
Project Administrator
Sat 01 Jun 2019 03:59:14 PM UTC, comment #7: 

The remaining incompatibility with Matlab is this

s = struct ('name', {});

This should produce an error because the fieldname foobar is unknown.

Rik <rik5>
Project Administrator
Sat 19 Nov 2016 07:41:41 PM UTC, comment #6: 

This behavior is still present in Octave 4.2.0.

Hartmut <hardy>
Sat 16 May 2015 07:02:21 PM UTC, comment #5: 

I think we would want to aim for Matlab compatibility here, so I had someone test for me.

>> s = struct('name', {});

This is a zero-length struct array with one field "name".

Non-existent fields always return an error, whether the size of the struct array is zero or not, so for us this should look like:

>> size(s)
ans =
   0   0
>> s.foo
error: structure has no member 'foo'

Valid field names should return an empty cs-list when the struct array size is zero, so using it alone fails with an error but concatenation in an array should work.

Mike Miller <mtmiller>
Project Administrator
Mon 27 Jan 2014 02:27:10 AM UTC, comment #4: 

The following commands still produce odd results in the current development version of Octave (4.1.0+):

octave:1> val = struct("name", {});
octave:2> val.foo
octave:3> val.bar
octave:4> [1, 2, val.name, 4, 5]
ans = [](0x0)

I'm assuming what we want is the same behavior as if val were a non-empty struct array:

octave:1> val.name = {};
octave:2> val.foo
error: structure has no member 'foo'
octave:3> val.bar
error: structure has no member 'bar'
octave:4> [1, 2, val.name, 4, 5]
ans =
  [1,1] = 1
  [1,2] = 2
  [1,3] = 4
  [1,4] = 5

Or should array concatenation produce an error as in the assignment case, as below?

octave:4> x = val.name
error: value on right hand side of assignment is undefined
octave:5> [1, 2, val.name, 4, 5]
error: value on right hand side of assignment is undefined

Mike Miller <mtmiller>
Project Administrator
Thu 13 Dec 2012 03:32:22 PM UTC, comment #3: 

The struct command is for creating struct arrays, not scalar structs. When the value arguments of struct() are cell arrays, they get unpacked into cs-lists defining each value in the struct array. The equivalent of

s.a = {}

is therefore

s = struct("a", {{}})

Jordi GutiƩrrez Hermoso <jordigh>
Project Administrator
Fri 02 Dec 2011 11:44:49 AM UTC, comment #2: 

Hi John,

My expectation is that the following two constructions behaves the same:

val = struct("name", {});


val = struct();
val.name = {};

However, the latter construction does not have the problems I have pointed out.
For example:

  • length(val)  is 1.
  • ans = val.name  is defined.

Thanks for the quick response..

Henrik Alsing Friberg <hfriberg>
Fri 02 Dec 2011 11:27:02 AM UTC, comment #1: 

The expression

struct ("name", {})

creates a 0x0 structure array with a field called "name".  It doesn't have any elements, so it's size is 0x0.  So your first example is the result I would expect.

Your second example shows a bug.  The behavior is different in the current development sources, but there is still a bug there.

The problem in your third example is also still present in the current development sources.  The 5th and 6th command lines should give errors about nonexistent structure fields.  The 7th command line is correct behavior however, because val.name produces a comma-separated list, but it is empty because the structure array is empty, so there is nothing to assign.

I don't think your fourth example shows a bug in Octave because the cell array of values that arg0.contents(p1) returns is empty.  Then you are trying to extract the first value which does not exist, so you are indexing outside the bounds of the cell array and that behavior is undefined.

John W. Eaton <jwe>
Project Administrator
Fri 02 Dec 2011 10:30:48 AM UTC, original submission:  

The following is an example of a structure that behaves badly when it has an empty cell within.

val = struct("name", {});

This submission will be a list of examples of this behavior, ending with one that causes a crash. The problem in all cases is that this particular structure does not behave as other structures.

Example 1
The structure has more fieldnames than fields:

octave:2> length(fieldnames(val))
ans =  1
octave:3> length(val)
ans = 0

Example 2
Interpretation terminates once the empty cell is encountered.

octave:4> [1, 2, val.name, 4, 5]
ans =
   1   2

Example 3
Any member can be extracted from the structure without errors, but the result is undefined even for 'val.name' which should be an empty cell.

octave:5> val.blabla
octave:6> val.qwerty
octave:7> ans = val.name
error: value on right hand side of assignment is undefined

Normally you would get these errors

octave:5> ans = struct();
octave:6> ans.blabla
error: structure has no member `blabla'

Example 4
From within an OCT-file, the underlying octave_value of this structure can not be extracted and causes a segmentation fault with core dump.

Octave_map::const_iterator p1 = arg0.seek( "name" );
octave_value tmp = arg0.contents( p1 ) (0);

This crash can be avoided by checking the size of

arg0.contents( p1 ).length()

... but the Octave Manual does not mention this, and I doubt many of us have implemented that check.

Henrik Alsing Friberg <hfriberg>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

   bug dependencies.


Carbon-Copy List
  • -email is unavailable- added by hardy (Posted a comment)
  • -email is unavailable- added by rik5 (Updated the item)
  • -email is unavailable- added by jordigh (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by hfriberg (Submitted the item)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.




    Follow 12 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-06-04 rik5 StatusConfirmed => Fixed
        Open/ClosedOpen => Closed
    2019-02-27 mtmiller Carbon-CopyRemoved 80942 => -
    2015-05-16 mtmiller Priority5 - Normal => 3 - Low
        Item GroupIncorrect Result => Matlab Compatibility
        StatusNeed Info => Confirmed
        Operating SystemGNU/Linux => Any
    2014-01-28 mtmiller Dependencies- => bugs #36003 is dependent
    2014-01-27 mtmiller Severity3 - Normal => 2 - Minor
        StatusNone => Need Info
        Release3.4.2 => dev
    2013-10-10 rik5 Item GroupSegfault, Bus Error, etc. => Incorrect Result

    Back to the top

    Powered by Savane 3.5