changeset 23345:0b6810085ed3

Altering class member of object array modifies other class members (bug #46660). * libinterp/octave-value/ov-classdef.cc(cdef_object_array::subsasgn): avoid the aforementioned problem. * test/module.mk: Add test directory for bug #46660. * test/bug-46660/module.mk: Add files for test for bug #46660. * test/bug-46660/bug-46660.tst: New test script for bug #46660. * test/bug-46660/class_bug46660.m: New test class for bug #46660. Pushed by Kai T. Ohlhus <k.ohlhus@gmail.com>.
author Lachlan Andrew <lachlanbis@gmail.com>
date Wed, 05 Apr 2017 13:26:45 +0200
parents 7dc148266dca
children 957d95286f52
files libinterp/octave-value/ov-classdef.cc test/bug-46660/bug-46660.tst test/bug-46660/class_bug46660.m test/bug-46660/module.mk test/module.mk
diffstat 5 files changed, 52 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-classdef.cc	Wed Apr 05 10:08:26 2017 +0200
+++ b/libinterp/octave-value/ov-classdef.cc	Wed Apr 05 13:26:45 2017 +0200
@@ -1611,12 +1611,19 @@
         }
       else
         {
-          const octave_value_list& ival = idx.front ();
+          const octave_value_list& ivl = idx.front ();
+
+          // Fill in trailing singleton dimensions so that
+          // array.index doesn't create a new blank entry (bug #46660).
+          const octave_idx_type one = static_cast<octave_idx_type> (1);
+          const octave_value_list& ival = ivl.length () >= 2
+                                            ? ivl : ((array.dims ()(0) == 1)
+                                                      ? ovl (one, ivl(0))
+                                                      : ovl (ivl(0), one));
 
           bool is_scalar = true;
 
-          Array<idx_vector> iv (dim_vector (1, std::max (ival.length (),
-            static_cast<octave_idx_type> (2))));
+          Array<idx_vector> iv (dim_vector (1, ival.length ()));
 
           for (int i = 0; i < ival.length (); i++)
             {
@@ -1639,11 +1646,6 @@
                        "array.");
             }
 
-          // Fill in trailing singleton dimensions so that
-          // array.index doesn't create a new blank entry (bug #46660).
-          for (int i = ival.length (); i < 2; i++)
-            iv(i) = static_cast<octave_idx_type> (1);
-
           Array<cdef_object> a = array.index (iv, true);
 
           if (a.numel () != 1)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-46660/bug-46660.tst	Wed Apr 05 13:26:45 2017 +0200
@@ -0,0 +1,29 @@
+## Copyright (C) 2017 Lachlan Andrew
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or
+## (at your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+%!test
+%! a(1) = class_bug46660 ();
+%! a(2) = class_bug46660 ();
+%! a(2).x = 123;
+%! assert (a(2).x, 123);
+%! assert (isempty (a(2).y));
+%!
+%! # writing into a(2).y may not alter a(2).x
+%! a(2).y = 321;
+%! assert (a(2).x, 123);
+%! assert (a(2).y, 321);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-46660/class_bug46660.m	Wed Apr 05 13:26:45 2017 +0200
@@ -0,0 +1,6 @@
+classdef class_bug46660
+  properties
+    x
+    y
+  endproperties
+endclassdef
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-46660/module.mk	Wed Apr 05 13:26:45 2017 +0200
@@ -0,0 +1,6 @@
+
+bug_46660_TEST_FILES = \
+  test/bug-46660/class_bug46660.m \
+  test/bug-46660/bug-46660.tst
+
+TEST_FILES += $(bug_46660_TEST_FILES)
--- a/test/module.mk	Wed Apr 05 10:08:26 2017 +0200
+++ b/test/module.mk	Wed Apr 05 13:26:45 2017 +0200
@@ -49,6 +49,7 @@
 include test/bug-38236/module.mk
 include test/bug-38691/module.mk
 include test/bug-44940/module.mk
+include test/bug-46660/module.mk
 include test/bug-50014/module.mk
 include test/bug-50035/module.mk
 include test/class-concat/module.mk