Content-Length: 6767 | pFad | http://github.com/Unidata/netcdf-c/pull/2633.patch
6797ADD2
From 5ae3655436393b31baac8fb8e0588dcfea01d0b4 Mon Sep 17 00:00:00 2001
From: Ward Fisher
Date: Tue, 21 Feb 2023 15:02:14 -0700
Subject: [PATCH 1/6] Fixing issues uncovered by compiling with
'-fsanitize=undefined' and running nc_test/nc_test, in support of
https://github.com/Unidata/netcdf-c/issues/1983, amongst others. The cast as
was being used was undefined behavior, and had to be worked around in a style
approximating C++'s 'reinterpret_cast'
---
nc_test/util.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/nc_test/util.c b/nc_test/util.c
index 2687ab0980..0cc4eaf91e 100644
--- a/nc_test/util.c
+++ b/nc_test/util.c
@@ -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));
}
@@ -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));
}
From df7f6ec5719a5ce89f4de129420c1d8c5c1f8161 Mon Sep 17 00:00:00 2001
From: Ward Fisher
Date: Tue, 21 Feb 2023 16:54:51 -0700
Subject: [PATCH 2/6] Correct undefined variable error.
---
nc_test/util.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/nc_test/util.c b/nc_test/util.c
index 0cc4eaf91e..3febb7e064 100644
--- a/nc_test/util.c
+++ b/nc_test/util.c
@@ -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;
From 776282348389f41b2fb323588af452a4f9c97b87 Mon Sep 17 00:00:00 2001
From: Ward Fisher
Date: Tue, 21 Feb 2023 16:58:28 -0700
Subject: [PATCH 3/6] Correct another uninitialized issue.
---
nc_test/util.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/nc_test/util.c b/nc_test/util.c
index 3febb7e064..9e46d88fe3 100644
--- a/nc_test/util.c
+++ b/nc_test/util.c
@@ -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 */
From b7635f061fc044072626f460c43d1c0155033fcd Mon Sep 17 00:00:00 2001
From: Ward Fisher
Date: Fri, 24 Feb 2023 11:27:36 -0700
Subject: [PATCH 4/6] Fixed an issue where memcpy was potentially passed a null
pointer.
---
libsrc/var.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/libsrc/var.c b/libsrc/var.c
index 00f3748826..f09bbe27bd 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;
From 0c45c396073348cd5d2ae0ff04b836f5bb38286a Mon Sep 17 00:00:00 2001
From: Ward Fisher
Date: Fri, 24 Feb 2023 11:48:00 -0700
Subject: [PATCH 5/6] More issues returned by sanitizer, related to attempts to
assign MAX_UNSIGNED_CHAR (255) to a variable of type char.
---
nc_test/util.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/nc_test/util.c b/nc_test/util.c
index 9e46d88fe3..b873f87bfe 100644
--- a/nc_test/util.c
+++ b/nc_test/util.c
@@ -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);
@@ -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,7 +1073,7 @@ 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) {
+ IF ((unsigned char)text[k] != (unsigned char)expect) {
error("nc_get_att_text: unexpected value");
} else {
nok++;
From 1d908e29ac3d8076cc585e5c7e8dd79ebdf9c626 Mon Sep 17 00:00:00 2001
From: Ward Fisher
Date: Fri, 24 Feb 2023 13:37:28 -0700
Subject: [PATCH 6/6] Explicit cast to unsigned char.
---
nc_test/util.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/nc_test/util.c b/nc_test/util.c
index b873f87bfe..4399763754 100644
--- a/nc_test/util.c
+++ b/nc_test/util.c
@@ -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);
@@ -1074,7 +1074,8 @@ check_atts(int ncid)
for (k = 0; k < ATT_LEN(i,j); k++) {
expect = hash(xtype, -1, &k);
IF ((unsigned char)text[k] != (unsigned char)expect) {
- error("nc_get_att_text: unexpected value");
+ error("Var %s [%lu] value read %hhd not that expected %g ",
+ var_name[i], j, text, expect);
} else {
nok++;
}
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/Unidata/netcdf-c/pull/2633.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy