commit b52a84631c9f551bf940f0318304a946ece0b33d
parent dd724383897196d0dc36b603bdfa529ce0d69c09
Author: Remy Noulin <loader2x@gmail.com>
Date: Sun, 29 Aug 2021 18:56:24 +0000
fix bug in compact function for smallArray and smallJson, allocate an empty sArray when the result is an empty array
release/json/libsheepyCSmallArrayCuTest.c | 13 +++++++++++++
release/json/libsheepyCSmallArrayTest.c | 13 +++++++++++++
release/json/libsheepyCSmallArrayTestMem.c | 13 +++++++++++++
release/json/libsheepyCSmallJsonCuTest.c | 13 +++++++++++++
release/json/libsheepyCSmallJsonTest.c | 13 +++++++++++++
release/json/libsheepyCSmallJsonTestMem.c | 13 +++++++++++++
release/libsheepy.h | 2 +-
src/json/libsheepyCSmallArray.c | 15 ++++++++++++++-
src/json/libsheepyCSmallJson.c | 14 ++++++++++++++
src/libsheepy.h | 2 +-
10 files changed, 108 insertions(+), 3 deletions(-)
Diffstat:
10 files changed, 108 insertions(+), 3 deletions(-)
diff --git a/release/json/libsheepyCSmallArrayCuTest.c b/release/json/libsheepyCSmallArrayCuTest.c
@@ -17355,6 +17355,19 @@ void compactSmallArrayT(CuTest *tc UNUSED) {
char *s = toStringO(r);
ck_assert_str_eq(s, "[true,\"<data container>\",{\"a\":1},2.000000e+00,5,\"asd\",[1],[0x61,0x73,0x64,0x00]]");
free(s);
+ // array with blank elements, becomes empty after compact
+ // self->a should not be null
+ // self->a should an empty sArray to avoid issues with the setP function
+ self->f->free(self);
+ self->f->pushS(self, " ");
+ self->f->pushS(self, "");
+ r = compactO(self);
+ ck_assert_ptr_ne(r, NULL);
+ ck_assert_ptr_ne(r->a, NULL);
+ ck_assert_int_eq(lenO(r), 0);
+ s = toStringO(r);
+ ck_assert_str_eq(s, "[]");
+ free(s);
// empty array
emptyO(self);
r = compactO(self);
diff --git a/release/json/libsheepyCSmallArrayTest.c b/release/json/libsheepyCSmallArrayTest.c
@@ -17344,6 +17344,19 @@ START_TEST(compactSmallArrayT)
char *s = toStringO(r);
ck_assert_str_eq(s, "[true,\"<data container>\",{\"a\":1},2.000000e+00,5,\"asd\",[1],[0x61,0x73,0x64,0x00]]");
free(s);
+ // array with blank elements becomes empty after compact
+ // self->a should not be null
+ // self->a should an empty sArray to avoid issues with the setP function
+ self->f->free(self);
+ self->f->pushS(self, " ");
+ self->f->pushS(self, "");
+ r = compactO(self);
+ ck_assert_ptr_ne(r, NULL);
+ ck_assert_ptr_ne(r->a, NULL);
+ ck_assert_int_eq(lenO(r), 0);
+ s = toStringO(r);
+ ck_assert_str_eq(s, "[]");
+ free(s);
// empty array
emptyO(self);
r = compactO(self);
diff --git a/release/json/libsheepyCSmallArrayTestMem.c b/release/json/libsheepyCSmallArrayTestMem.c
@@ -17353,6 +17353,19 @@ void compactSmallArrayT(void) {
char *s = toStringO(r);
ck_assert_str_eq(s, "[true,\"<data container>\",{\"a\":1},2.000000e+00,5,\"asd\",[1],[0x61,0x73,0x64,0x00]]");
free(s);
+ // array with blank elements, becomes empty after compact
+ // self->a should not be null
+ // self->a should an empty sArray to avoid issues with the setP function
+ self->f->free(self);
+ self->f->pushS(self, " ");
+ self->f->pushS(self, "");
+ r = compactO(self);
+ ck_assert_ptr_ne(r, NULL);
+ ck_assert_ptr_ne(r->a, NULL);
+ ck_assert_int_eq(lenO(r), 0);
+ s = toStringO(r);
+ ck_assert_str_eq(s, "[]");
+ free(s);
// empty array
emptyO(self);
r = compactO(self);
diff --git a/release/json/libsheepyCSmallJsonCuTest.c b/release/json/libsheepyCSmallJsonCuTest.c
@@ -27699,6 +27699,19 @@ void compactSmallJsonT(CuTest *tc UNUSED) {
char *s = toStringO(r);
ck_assert_str_eq(s, "[true,\"<data container>\",{\"a\":1},2.000000e+00,5,\"asd\",[1],[0x61,0x73,0x64,0x00]]");
free(s);
+ // array with blank elements becomes empty after compact
+ // self->topA should not be null
+ // self->topA should an empty sArray to avoid issues with the setP function
+ self->f->free(self);
+ self->f->pushS(self, " ");
+ self->f->pushS(self, "");
+ r = compactO(self);
+ ck_assert_ptr_ne(r, NULL);
+ ck_assert_ptr_ne(r->topA, NULL);
+ ck_assert_int_eq(lenO(r), 0);
+ s = toStringO(r);
+ ck_assert_str_eq(s, "[]");
+ free(s);
// empty array
emptyO(self);
r = compactO(self);
diff --git a/release/json/libsheepyCSmallJsonTest.c b/release/json/libsheepyCSmallJsonTest.c
@@ -27688,6 +27688,19 @@ START_TEST(compactSmallJsonT)
char *s = toStringO(r);
ck_assert_str_eq(s, "[true,\"<data container>\",{\"a\":1},2.000000e+00,5,\"asd\",[1],[0x61,0x73,0x64,0x00]]");
free(s);
+ // array with blank elements becomes empty after compact
+ // self->topA should not be null
+ // self->topA should an empty sArray to avoid issues with the setP function
+ self->f->free(self);
+ self->f->pushS(self, " ");
+ self->f->pushS(self, "");
+ r = compactO(self);
+ ck_assert_ptr_ne(r, NULL);
+ ck_assert_ptr_ne(r->topA, NULL);
+ ck_assert_int_eq(lenO(r), 0);
+ s = toStringO(r);
+ ck_assert_str_eq(s, "[]");
+ free(s);
// empty array
emptyO(self);
r = compactO(self);
diff --git a/release/json/libsheepyCSmallJsonTestMem.c b/release/json/libsheepyCSmallJsonTestMem.c
@@ -27697,6 +27697,19 @@ void compactSmallJsonT(void) {
char *s = toStringO(r);
ck_assert_str_eq(s, "[true,\"<data container>\",{\"a\":1},2.000000e+00,5,\"asd\",[1],[0x61,0x73,0x64,0x00]]");
free(s);
+ // array with blank elements becomes empty after compact
+ // self->topA should not be null
+ // self->topA should an empty sArray to avoid issues with the setP function
+ self->f->free(self);
+ self->f->pushS(self, " ");
+ self->f->pushS(self, "");
+ r = compactO(self);
+ ck_assert_ptr_ne(r, NULL);
+ ck_assert_ptr_ne(r->topA, NULL);
+ ck_assert_int_eq(lenO(r), 0);
+ s = toStringO(r);
+ ck_assert_str_eq(s, "[]");
+ free(s);
// empty array
emptyO(self);
r = compactO(self);
diff --git a/release/libsheepy.h b/release/libsheepy.h
@@ -98,7 +98,7 @@
// version accoring to the version package: Release.Major.minor.patch
// https://noulin.net/version/file/README.md.html
-#define LIBSHEEPY_VERSION "2.2.6"
+#define LIBSHEEPY_VERSION "2.2.6.1"
#ifndef SH_PREFIX
#define SH_PREFIX(NAME) NAME
diff --git a/src/json/libsheepyCSmallArray.c b/src/json/libsheepyCSmallArray.c
@@ -10944,7 +10944,20 @@ internal smallArrayt* compactSmallArray(smallArrayt *self) {
}
free(self->a);
- self->a = r->a;
+ if ((r->a)) {
+ self->a = r->a;
+ }
+ else {
+ // r->a is null and has no elements, allocate an empty sArray
+ // to make setP work correctly after a call to compact.
+ // For example:
+ // smallArrayt *a = getG(dict, rtSmallArrayt, "key");
+ // compactG(a);
+ // setNFreePG(dict, "key", a);
+ // if self->a is null then setNFreePG returns an error and the "key" array pointer
+ // is not updated causing memory errors
+ self->a = allocSArray();;
+ }
finishO(r);
return(self);
}
diff --git a/src/json/libsheepyCSmallJson.c b/src/json/libsheepyCSmallJson.c
@@ -18582,6 +18582,20 @@ internal smallJsont* compactSmallJson(smallJsont *self) {
free(self->topA);
self->topA = r->topA;
+ if ((r->topA)) {
+ self->topA = r->topA;
+ }
+ else {
+ // r->topA is null and has no elements, allocate an empty sArray
+ // to make setP work correctly after a call to compact.
+ // For example:
+ // smallJsont *a = getG(dict, rtSmallJsont, "key");
+ // compactG(a);
+ // setNFreePG(dict, "key", a);
+ // if self->topA is null then setNFreePG returns an error and the "key" array pointer
+ // is not updated causing memory errors
+ self->topA = allocSArray();;
+ }
finishO(r);
return(self);
}
diff --git a/src/libsheepy.h b/src/libsheepy.h
@@ -98,7 +98,7 @@
// version accoring to the version package: Release.Major.minor.patch
// https://noulin.net/version/file/README.md.html
-#define LIBSHEEPY_VERSION "2.2.6"
+#define LIBSHEEPY_VERSION "2.2.6.1"
#ifndef SH_PREFIX
#define SH_PREFIX(NAME) NAME