Skip to content

Commit

Permalink
Updated patch with changes from Unidata/netcdf-c#2633. These patches …
Browse files Browse the repository at this point in the history
…relate to undefined behavior in nc_test, and (unsigned char) values being assigned to variables of type (char)
  • Loading branch information
WardF committed Feb 24, 2023
1 parent 77f976d commit 73e8048
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion recipe/patches/fix-undefined-behavior.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
diff --git a/libsrc/var.c b/libsrc/var.c
index 00f37488..f09bbe27 100644
--- a/libsrc/var.c
+++ b/libsrc/var.c
@@ -175,10 +175,12 @@ dup_NC_var(const NC_var *rvarp)
return NULL;
}

- (void) memcpy(varp->shape, rvarp->shape,
- rvarp->ndims * sizeof(size_t));
- (void) memcpy(varp->dsizes, rvarp->dsizes,
- rvarp->ndims * sizeof(off_t));
+ if(rvarp->shape != NULL)
+ (void) memcpy(varp->shape, rvarp->shape,
+ rvarp->ndims * sizeof(size_t));
+ if(rvarp->dsizes != NULL)
+ (void) memcpy(varp->dsizes, rvarp->dsizes,
+ rvarp->ndims * sizeof(off_t));
varp->xsz = rvarp->xsz;
varp->len = rvarp->len;
varp->begin = rvarp->begin;
diff --git a/nc_test/util.c b/nc_test/util.c
index 2687ab098..9e46d88fe 100644
index 2687ab09..43997637 100644
--- a/nc_test/util.c
+++ b/nc_test/util.c
@@ -170,8 +170,8 @@ equal(const double x,
Expand All @@ -24,6 +45,15 @@ index 2687ab098..9e46d88fe 100644
return ABS(x2-y2) <= epsilon * MAX( ABS(x2), ABS(y2));
}

@@ -343,7 +343,7 @@ int dbl2nc ( const double d, const nc_type xtype, void *p)
* reporting it as a range error.
*/
if ( r < X_CHAR_MIN || r > X_CHAR_MAX ) return 2;
- *((signed char*) p) = (signed char)r;
+ *((unsigned char*) p) = (unsigned char)r;
break;
case NC_BYTE:
r = floor(0.5+d);
@@ -413,8 +413,8 @@ int dbl2nc ( const double d, const nc_type xtype, void *p)
double
hash( const nc_type xtype, const int rank, const size_t *index )
Expand All @@ -35,6 +65,15 @@ index 2687ab098..9e46d88fe 100644
int d; /* index of dimension */

/* If vector then elements 0 & 1 are min & max. Elements 2 & 3 are */
@@ -841,7 +841,7 @@ put_atts(int ncid)
for (j = 0; j < NATTS(i); j++) {
if (ATT_TYPE(i,j) == NC_CHAR) {
for (k = 0; k < ATT_LEN(i,j); k++) {
- catt[k] = (char) hash(ATT_TYPE(i,j), -1, &k);
+ catt[k] = (unsigned char) hash(ATT_TYPE(i,j), -1, &k);
}
err = nc_put_att_text(ncid, i, ATT_NAME(i,j),
ATT_LEN(i,j), catt);
@@ -969,7 +969,7 @@ check_dims(int ncid)
void
check_vars(int ncid)
Expand All @@ -44,3 +83,24 @@ index 2687ab098..9e46d88fe 100644
char text, name[NC_MAX_NAME];
int i, err; /* status */
size_t j;
@@ -1006,7 +1006,7 @@ check_vars(int ncid)
err = nc_get_var1_text(ncid, i, index, &text);
IF (err)
error("nc_get_var1_text: %s", nc_strerror(err));
- IF (text != (char)expect) {
+ IF ((unsigned char)text != (unsigned char)expect) {
error("Var %s [%lu] value read %hhd not that expected %g ",
var_name[i], j, text, expect);
print_n_size_t(var_rank[i], index);
@@ -1073,8 +1073,9 @@ check_atts(int ncid)
error("nc_get_att_text: %s", nc_strerror(err));
for (k = 0; k < ATT_LEN(i,j); k++) {
expect = hash(xtype, -1, &k);
- IF (text[k] != (char)expect) {
- error("nc_get_att_text: unexpected value");
+ IF ((unsigned char)text[k] != (unsigned char)expect) {
+ error("Var %s [%lu] value read %hhd not that expected %g ",
+ var_name[i], j, text, expect);
} else {
nok++;
}

0 comments on commit 73e8048

Please sign in to comment.