From 0fbcc3a0c7ef4835c77c5d8b87cc8732cc3f90df Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 30 Jul 2021 09:16:27 -0700 Subject: Fixed uninitializsed bug in Bu::Array::setSize. When setCapacity was used before setSize some elements were not being properly initialized in the array. --- src/stable/array.h | 33 +++++++++++++++++++++++++++------ src/unit/array.unit | 8 ++++++++ src/unit/string.unit | 6 ++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/stable/array.h b/src/stable/array.h index ecffc6a..b089bda 100644 --- a/src/stable/array.h +++ b/src/stable/array.h @@ -297,7 +297,7 @@ namespace Bu return core->iSize; } - void setSize( long iNewLen, const value &initTo=value() ) + void setSize( long iNewLen ) { if( core->iSize == iNewLen ) return; @@ -306,19 +306,40 @@ namespace Bu if( iNewLen > core->iCapacity ) { core->setCapacity( iNewLen ); + } + if( iNewLen > core->iSize ) + { for( int j = core->iSize; j < iNewLen; j++ ) { core->va.construct( - &core->pData[j], - initTo + &core->pData[j] ); } - core->iSize = iNewLen; } - else + core->iSize = iNewLen; + } + + void setSize( long iNewLen, const value &initTo ) + { + if( core->iSize == iNewLen ) + return; + + _hardCopy(); + if( iNewLen > core->iCapacity ) + { + core->setCapacity( iNewLen ); + } + if( iNewLen > core->iSize ) { - core->iSize = iNewLen; + for( int j = core->iSize; j < iNewLen; j++ ) + { + core->va.construct( + &core->pData[j], + initTo + ); + } } + core->iSize = iNewLen; } /** diff --git a/src/unit/array.unit b/src/unit/array.unit index 83401c1..e9f2bdf 100644 --- a/src/unit/array.unit +++ b/src/unit/array.unit @@ -101,4 +101,12 @@ suite Array unitTest( aInts[j] == j ); } } + + test setSize + { + Bu::Array aStr( 3 ); + aStr.setSize( 3 ); + aStr[1] = "Hello"; + aStr[0] = aStr[1].clone(); + } } diff --git a/src/unit/string.unit b/src/unit/string.unit index 1f4ca7e..6242eff 100644 --- a/src/unit/string.unit +++ b/src/unit/string.unit @@ -586,6 +586,12 @@ suite String { unitTest( Bu::String("0x%{1}00").arg( 75, Bu::Fmt::hex() ).end() == "0x4b00" ); } + + test sharedEmpty1 + { + Bu::String a; + Bu::String b = a.clone(); + } } // 03F09CA4F58AC8CA0E80F0D9D409D0A60700A192270004BC3A99E91D0001034F544603362E35013103313130019CA4F58AC8CA0E0002830800002C4200008AC200EBF7D9D4090127BB010000E3 // -- cgit v1.2.3