Skip to content

Commit

Permalink
Merge pull request #2633 from WardF/gh1983.wif
Browse files Browse the repository at this point in the history
A small, but meaningful start, to addressing undefined behavior
  • Loading branch information
WardF authored Feb 24, 2023
2 parents 0034e31 + 1d908e2 commit 17b4cb3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
10 changes: 6 additions & 4 deletions libsrc/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 13 additions & 12 deletions nc_test/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ equal(const double x,
/* because in-memory data type char can be signed or unsigned,
* type cast the value from external NC_CHAR before the comparison
*/
char x2 = (char) x;
char y2 = (char) y;
char x2 = *(char *)&x;
char y2 = *(char *)&y;
return ABS(x2-y2) <= epsilon * MAX( ABS(x2), ABS(y2));
}

Expand All @@ -194,8 +194,8 @@ equal2(const double x,
/* because in-memory data type char can be signed or unsigned,
* type cast the value from external NC_CHAR before the comparison
*/
char x2 = (char) x;
char y2 = (char) y;
char x2 = *(char *)&x;
char y2 = *(char *)&y;
return ABS(x2-y2) <= epsilon * MAX( ABS(x2), ABS(y2));
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 )
{
double base;
double result;
double base = 0;
double result = 0;
int d; /* index of dimension */

/* If vector then elements 0 & 1 are min & max. Elements 2 & 3 are */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -969,7 +969,7 @@ check_dims(int ncid)
void
check_vars(int ncid)
{
size_t index[MAX_RANK];
size_t index[MAX_RANK] = {0};
char text, name[NC_MAX_NAME];
int i, err; /* status */
size_t j;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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++;
}
Expand Down

0 comments on commit 17b4cb3

Please sign in to comment.