Fixed various UBSan warnings about working with NULL pointers

Any pointer arithmetic with NULL pointers is technically UB, even if you don't end up dereferencing the pointer.
This commit is contained in:
Sean McBride 2023-11-14 22:28:01 -05:00
parent 99f22954c5
commit dc1b8b9c4b
6 changed files with 90 additions and 62 deletions

View File

@ -230,15 +230,18 @@ dup_NC_attrarrayV(NC_attrarray *ncap, const NC_attrarray *ref)
ncap->nelems = 0;
{
NC_attr **app = ncap->value;
const NC_attr **drpp = (const NC_attr **)ref->value;
NC_attr *const *const end = &app[ref->nelems];
for( /*NADA*/; app < end; drpp++, app++, ncap->nelems++)
if (app)
{
*app = dup_NC_attr(*drpp);
if(*app == NULL)
const NC_attr **drpp = (const NC_attr **)ref->value;
NC_attr *const *const end = &app[ref->nelems];
for( /*NADA*/; app < end; drpp++, app++, ncap->nelems++)
{
status = NC_ENOMEM;
break;
*app = dup_NC_attr(*drpp);
if(*app == NULL)
{
status = NC_ENOMEM;
break;
}
}
}
}

View File

@ -228,15 +228,18 @@ dup_NC_dimarrayV(NC_dimarray *ncap, const NC_dimarray *ref)
ncap->nelems = 0;
{
NC_dim **dpp = ncap->value;
const NC_dim **drpp = (const NC_dim **)ref->value;
NC_dim *const *const end = &dpp[ref->nelems];
for( /*NADA*/; dpp < end; drpp++, dpp++, ncap->nelems++)
if(dpp != NULL)
{
*dpp = dup_NC_dim(*drpp);
if(*dpp == NULL)
const NC_dim **drpp = (const NC_dim **)ref->value;
NC_dim *const *const end = &dpp[ref->nelems];
for( /*NADA*/; dpp < end; drpp++, dpp++, ncap->nelems++)
{
status = NC_ENOMEM;
break;
*dpp = dup_NC_dim(*drpp);
if(*dpp == NULL)
{
status = NC_ENOMEM;
break;
}
}
}
}

View File

@ -950,7 +950,6 @@ int
NC_calcsize(const NC3_INFO *ncp, off_t *calcsizep)
{
NC_var **vpp = (NC_var **)ncp->vars.value;
NC_var *const *const end = &vpp[ncp->vars.nelems];
NC_var *last_fix = NULL; /* last "non-record" var */
int numrecvars = 0; /* number of record variables */
@ -960,12 +959,16 @@ NC_calcsize(const NC3_INFO *ncp, off_t *calcsizep)
return NC_NOERR;
}
for( /*NADA*/; vpp < end; vpp++) {
if(IS_RECVAR(*vpp)) {
numrecvars++;
} else {
last_fix = *vpp;
}
if (vpp)
{
NC_var *const *const end = &vpp[ncp->vars.nelems];
for( /*NADA*/; vpp < end; vpp++) {
if(IS_RECVAR(*vpp)) {
numrecvars++;
} else {
last_fix = *vpp;
}
}
}
if(numrecvars == 0) {

View File

@ -551,7 +551,7 @@ static int
NCedgeck(const NC3_INFO* ncp, const NC_var *varp,
const size_t *start, const size_t *edges)
{
const size_t *const end = start + varp->ndims;
const size_t *const end = start ? (start + varp->ndims) : NULL;
const size_t *shp = varp->shape;
if(varp->ndims == 0)

View File

@ -448,10 +448,13 @@ ncx_len_NC_dimarray(const NC_dimarray *ncap, int version)
/* else */
{
const NC_dim **dpp = (const NC_dim **)ncap->value;
const NC_dim *const *const end = &dpp[ncap->nelems];
for( /*NADA*/; dpp < end; dpp++)
if (dpp)
{
xlen += ncx_len_NC_dim(*dpp,version);
const NC_dim *const *const end = &dpp[ncap->nelems];
for( /*NADA*/; dpp < end; dpp++)
{
xlen += ncx_len_NC_dim(*dpp,version);
}
}
}
return xlen;
@ -641,11 +644,13 @@ v1h_put_NC_attrV(v1hs *psp, const NC_attr *attrp)
if(status != NC_NOERR)
return status;
(void) memcpy(psp->pos, value, nbytes);
if (value) {
(void) memcpy(psp->pos, value, nbytes);
value = (void *)((char *)value + nbytes);
}
psp->pos = (void *)((char *)psp->pos + nbytes);
value = (void *)((char *)value + nbytes);
remaining -= nbytes;
remaining -= nbytes;
} while(remaining != 0);
@ -709,11 +714,13 @@ v1h_get_NC_attrV(v1hs *gsp, NC_attr *attrp)
if(status != NC_NOERR)
return status;
(void) memcpy(value, gsp->pos, nget);
if (value) {
(void) memcpy(value, gsp->pos, nget);
value = (void *)((signed char *)value + nget);
}
gsp->pos = (void*)((unsigned char *)gsp->pos + nget);
value = (void *)((signed char *)value + nget);
remaining -= nget;
} while(remaining != 0);
@ -790,10 +797,13 @@ ncx_len_NC_attrarray(const NC_attrarray *ncap, int version)
/* else */
{
const NC_attr **app = (const NC_attr **)ncap->value;
const NC_attr *const *const end = &app[ncap->nelems];
for( /*NADA*/; app < end; app++)
if (app)
{
xlen += ncx_len_NC_attr(*app,version);
const NC_attr *const *const end = &app[ncap->nelems];
for( /*NADA*/; app < end; app++)
{
xlen += ncx_len_NC_attr(*app,version);
}
}
}
return xlen;
@ -1090,10 +1100,13 @@ ncx_len_NC_vararray(const NC_vararray *ncap, size_t sizeof_off_t, int version)
/* else */
{
const NC_var **vpp = (const NC_var **)ncap->value;
const NC_var *const *const end = &vpp[ncap->nelems];
for( /*NADA*/; vpp < end; vpp++)
if (vpp)
{
xlen += ncx_len_NC_var(*vpp, sizeof_off_t, version);
const NC_var *const *const end = &vpp[ncap->nelems];
for( /*NADA*/; vpp < end; vpp++)
{
xlen += ncx_len_NC_var(*vpp, sizeof_off_t, version);
}
}
}
return xlen;
@ -1224,7 +1237,6 @@ static int
NC_computeshapes(NC3_INFO* ncp)
{
NC_var **vpp = (NC_var **)ncp->vars.value;
NC_var *const *const end = &vpp[ncp->vars.nelems];
NC_var *first_var = NULL; /* first "non-record" var */
NC_var *first_rec = NULL; /* first "record" var */
int status;
@ -1236,27 +1248,31 @@ NC_computeshapes(NC3_INFO* ncp)
if(ncp->vars.nelems == 0)
return(0);
for( /*NADA*/; vpp < end; vpp++)
if (vpp)
{
status = NC_var_shape(*vpp, &ncp->dims);
if(status != NC_NOERR)
return(status);
NC_var *const *const end = &vpp[ncp->vars.nelems];
for( /*NADA*/; vpp < end; vpp++)
{
status = NC_var_shape(*vpp, &ncp->dims);
if(status != NC_NOERR)
return(status);
if(IS_RECVAR(*vpp))
{
if(first_rec == NULL)
first_rec = *vpp;
ncp->recsize += (*vpp)->len;
}
else
{
if(IS_RECVAR(*vpp))
{
if(first_rec == NULL)
first_rec = *vpp;
ncp->recsize += (*vpp)->len;
}
else
{
if(first_var == NULL)
first_var = *vpp;
/*
* Overwritten each time thru.
* Usually overwritten in first_rec != NULL clause below.
*/
ncp->begin_rec = (*vpp)->begin + (off_t)(*vpp)->len;
/*
* Overwritten each time thru.
* Usually overwritten in first_rec != NULL clause below.
*/
ncp->begin_rec = (*vpp)->begin + (off_t)(*vpp)->len;
}
}
}

View File

@ -267,14 +267,17 @@ dup_NC_vararrayV(NC_vararray *ncap, const NC_vararray *ref)
{
NC_var **vpp = ncap->value;
const NC_var **drpp = (const NC_var **)ref->value;
NC_var *const *const end = &vpp[ref->nelems];
for( /*NADA*/; vpp < end; drpp++, vpp++, ncap->nelems++)
if (vpp)
{
*vpp = dup_NC_var(*drpp);
if(*vpp == NULL)
NC_var *const *const end = &vpp[ref->nelems];
for( /*NADA*/; vpp < end; drpp++, vpp++, ncap->nelems++)
{
status = NC_ENOMEM;
break;
*vpp = dup_NC_var(*drpp);
if(*vpp == NULL)
{
status = NC_ENOMEM;
break;
}
}
}
}