Repository: SINTEF/dlite (language: python)

Issue:
Fix new warnings from gcc 12.2.1
gcc 12.2.1 issues two new warnings:

A `format-truncation` warnings in `mu_assert_string_eq()`. To silence it, we have to check the return value of `snprintf()` even though it is not used. Not sure that it improves the code... but it is required for successfully compiling dlite with gcc 12.2.1 since all warnings are turned into errors.

The second fix is a real improvement for handling the return value of `realloc()` correctly.


Discussion / hints:

Fix patch:
diff --git a/src/dlite-entity.c b/src/dlite-entity.c
index 1d9a1b6ad..190bb79ad 100644
--- a/src/dlite-entity.c
+++ b/src/dlite-entity.c
@@ -1730,14 +1730,17 @@ int dlite_instance_set_dimension_sizes(DLiteInstance *inst, const int *dims)
     if (newmembs == oldmembs[n]) {
       continue;
     } else if (newmembs > 0) {
-      void *q;
+      void **q;
       if (newmembs < oldmembs[n])
         for (i=newmembs; i < oldmembs[n]; i++)
           dlite_type_clear((char *)(*ptr) + i*p->size, p->type, p->size);
-      if (!(*ptr = realloc((q = *ptr), newsize))) {
-        if (q) free(q);
+
+      if (!(q = realloc(*ptr, newsize))) {
+        free(*ptr);
         return err(1, "error reallocating '%s' to size %d", p->name, newsize);
       }
+      *ptr = q;
+
       if (newmembs > oldmembs[n])
         memset((char *)(*ptr) + oldsize, 0, newsize - oldsize);
     } else if (*ptr) {


Test patch:
diff --git a/src/tests/minunit/minunit.h b/src/tests/minunit/minunit.h
index 49fda003f..eeed8f980 100644
--- a/src/tests/minunit/minunit.h
+++ b/src/tests/minunit/minunit.h
@@ -223,6 +223,7 @@ static void (*minunit_teardown)(void) = NULL;
 #define mu_assert_string_eq(expected, result) MU__SAFE_BLOCK(\
 	const char *minunit_tmp_e = expected;\
 	const char *minunit_tmp_r = result;\
+        int len;\
 	minunit_assert++;\
 	if (!minunit_tmp_e) {\
 		minunit_tmp_e = "<null pointer>";\
@@ -231,8 +232,8 @@ static void (*minunit_teardown)(void) = NULL;
 		minunit_tmp_r = "<null pointer>";\
 	}\
 	if(strcmp(minunit_tmp_e, minunit_tmp_r)) {\
-		snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: '%s' expected but was '%s'", __func__, __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r);\
-		minunit_status = 1;\
+                len = snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: '%s' expected but was '%s'", __func__, __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r);\
+		minunit_status = (len >= 0) ? 1 : 2;\
 		return;\
 	} else {\
 		printf(".");\