Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A small, but meaningful start, to addressing undefined behavior #2633

Merged
merged 6 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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