Skip to content

Commit

Permalink
Silence conversion warnings from malloc arguments
Browse files Browse the repository at this point in the history
Mostly just add an explicit cast when calling `malloc` and its
variants. Sometimes instead change the type of a local variable if
this would silence multiple warnings.
  • Loading branch information
ZedThree committed Nov 24, 2023
1 parent 0c6fd78 commit d07dac9
Show file tree
Hide file tree
Showing 75 changed files with 283 additions and 258 deletions.
3 changes: 2 additions & 1 deletion h5_test/tst_h_vl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "h5_err_macros.h"
#include <hdf5.h>
#include <stddef.h>

#define FILE_NAME "tst_h_vl.h5"
#define DIM1_LEN 3
Expand All @@ -23,7 +24,7 @@ main()
hsize_t dims[1] = {DIM1_LEN};
hvl_t data[DIM1_LEN], data_in[DIM1_LEN];
int *phoney;
int i, j;
size_t i, j;
size_t size;

/* Create some phoney data, an array of struct s1, which holds a
Expand Down
2 changes: 1 addition & 1 deletion include/nctestserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ parseServers(const char* remotetestservers)

/* Keep LGTM quiet */
if(rtslen > MAXREMOTETESTSERVERS) goto done;
list = (char**)malloc(sizeof(char*) * (int)(rtslen/2));
list = (char**)malloc(sizeof(char*) * (rtslen/2));
if(list == NULL) return NULL;
rts = strdup(remotetestservers);
if(rts == NULL) goto done;
Expand Down
8 changes: 4 additions & 4 deletions libdap2/dapdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dumpmetadata(int ncid, NChdr** hdrp)
fprintf(stdout,"ncid=%d ngatts=%d ndims=%d nvars=%d unlimid=%d\n",
hdr->ncid,hdr->ngatts,hdr->ndims,hdr->nvars,hdr->unlimid);
#endif
hdr->gatts = (NCattribute*)calloc(1,hdr->ngatts*sizeof(NCattribute));
hdr->gatts = (NCattribute*)calloc(1, (size_t)hdr->ngatts*sizeof(NCattribute));
MEMCHECK(hdr->gatts,NC_ENOMEM);
if(hdr->ngatts > 0)
fprintf(stdout,"global attributes:\n");
Expand Down Expand Up @@ -81,7 +81,7 @@ dumpmetadata(int ncid, NChdr** hdrp)
fprintf(stdout,"\n");
}

hdr->dims = (Dim*)malloc(hdr->ndims*sizeof(Dim));
hdr->dims = (Dim*)malloc((size_t)hdr->ndims*sizeof(Dim));
MEMCHECK(hdr->dims,NC_ENOMEM);
for(i=0;i<hdr->ndims;i++) {
hdr->dims[i].dimid = i;
Expand All @@ -93,7 +93,7 @@ dumpmetadata(int ncid, NChdr** hdrp)
fprintf(stdout,"dim[%d]: name=%s size=%lu\n",
i,hdr->dims[i].name,(unsigned long)hdr->dims[i].size);
}
hdr->vars = (Var*)malloc(hdr->nvars*sizeof(Var));
hdr->vars = (Var*)malloc((size_t)hdr->nvars*sizeof(Var));
MEMCHECK(hdr->vars,NC_ENOMEM);
for(i=0;i<hdr->nvars;i++) {
Var* var = &hdr->vars[i];
Expand All @@ -118,7 +118,7 @@ dumpmetadata(int ncid, NChdr** hdrp)
fprintf(stdout," %d",var->dimids[j]);
}
fprintf(stdout,"}\n");
var->atts = (NCattribute*)malloc(var->natts*sizeof(NCattribute));
var->atts = (NCattribute*)malloc((size_t)var->natts*sizeof(NCattribute));
MEMCHECK(var->atts,NC_ENOMEM);
for(j=0;j<var->natts;j++) {
NCattribute* att = &var->atts[j];
Expand Down
3 changes: 2 additions & 1 deletion libdap2/daputil.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*********************************************************************/

#include "config.h"
#include <stddef.h>

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
Expand Down Expand Up @@ -763,7 +764,7 @@ repairname(const char* name, const char* badchars)
const char *p;
char *q;
int c;
int nnlen = 0;
size_t nnlen = 0;

if(name == NULL) return NULL;
nnlen = (3*strlen(name)); /* max needed */
Expand Down
3 changes: 2 additions & 1 deletion libdap4/d4file.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ncd4dispatch.h"
#include "d4includes.h"
#include "d4curlfunctions.h"
#include <stddef.h>

#ifdef _MSC_VER
#include <process.h>
Expand Down Expand Up @@ -301,7 +302,7 @@ set_curl_properties(NCD4INFO* d4info)
/* If no cookie file was defined, define a default */
char* path = NULL;
char* newpath = NULL;
int len;
size_t len;
errno = 0;
NCglobalstate* globalstate = NC_getglobalstate();

Expand Down
7 changes: 4 additions & 3 deletions libdap4/d4meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "d4includes.h"
#include <stdarg.h>
#include <stddef.h>
#include "nc4internal.h"
#include "ncoffsets.h"

Expand Down Expand Up @@ -603,8 +604,8 @@ savevarbyid(NCD4node* group, NCD4node* var)
{
if(group->group.varbyid == NULL)
group->group.varbyid = nclistnew();
nclistsetalloc(group->group.varbyid,var->meta.id);
nclistinsert(group->group.varbyid,var->meta.id,var);
nclistsetalloc(group->group.varbyid, (size_t)var->meta.id);
nclistinsert(group->group.varbyid, (size_t)var->meta.id,var);
}

/* Collect FQN path from var node up to and including
Expand Down Expand Up @@ -730,7 +731,7 @@ compileAttrValues(NCD4meta* builder, NCD4node* attr, void** memoryp, NClist* blo
NCD4node* container = attr->container;
NCD4node* basetype = attr->basetype;
NClist* values = attr->attr.values;
int count = nclistlength(values);
size_t count = nclistlength(values);

memset((void*)&converter,0,sizeof(converter));

Expand Down
8 changes: 4 additions & 4 deletions libdispatch/dinfermodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ parsepair(const char* pair, char** keyp, char** valuep)
key = strdup(pair);
} else {
ptrdiff_t len = (p-pair);
if((key = malloc(len+1))==NULL) return NC_ENOMEM;
memcpy(key,pair,len);
if((key = malloc((size_t)len+1))==NULL) return NC_ENOMEM;
memcpy(key,pair,(size_t)len);
key[len] = '\0';
if(p[1] == '\0')
value = NULL;
Expand Down Expand Up @@ -383,8 +383,8 @@ parseonchar(const char* s, int ch, NClist* segments)
endp = strchr(p,ch);
if(endp == NULL) endp = p + strlen(p);
slen = (endp - p);
if((q = malloc(slen+1)) == NULL) {stat = NC_ENOMEM; goto done;}
memcpy(q,p,slen);
if((q = malloc((size_t)slen+1)) == NULL) {stat = NC_ENOMEM; goto done;}
memcpy(q,p,(size_t)slen);
q[slen] = '\0';
nclistpush(segments,q);
if(*endp == '\0') break;
Expand Down
4 changes: 2 additions & 2 deletions libdispatch/dutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ NC_split_delim(const char* arg, char delim, NClist* segments)
len = (q - p);
if(len == 0)
{stat = NC_EURL; goto done;}
if((seg = malloc(len+1)) == NULL)
if((seg = malloc((size_t)len+1)) == NULL)
{stat = NC_ENOMEM; goto done;}
memcpy(seg,p,len);
memcpy(seg,p,(size_t)len);
seg[len] = '\0';
nclistpush(segments,seg);
seg = NULL; /* avoid mem errors */
Expand Down
4 changes: 2 additions & 2 deletions libdispatch/dv2i.c
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ ncvarputg(
ret = nc_inq_vartype(ncid, varid, &type);
if(ret) return ret;
el_size = nctypelen(type);
imp = (ptrdiff_t*) malloc(ndims * sizeof(ptrdiff_t));
imp = (ptrdiff_t*) malloc((size_t)ndims * sizeof(ptrdiff_t));
for (i=0; i<ndims; i++) imp[i] = map[i] / el_size;
}

Expand Down Expand Up @@ -1327,7 +1327,7 @@ ncvargetg(
ret = nc_inq_vartype(ncid, varid, &type);
if(ret) return ret;
el_size = nctypelen(type);
imp = (ptrdiff_t*) malloc(ndims * sizeof(ptrdiff_t));
imp = (ptrdiff_t*) malloc((size_t)ndims * sizeof(ptrdiff_t));
for (i=0; i<ndims; i++) imp[i] = map[i] / el_size;
}

Expand Down
6 changes: 3 additions & 3 deletions libdispatch/dvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
if(status != NC_NOERR) return status;
if(nunlimdims == 0) return status;

if (!(unlimids = malloc(nunlimdims * sizeof(int))))
if (!(unlimids = malloc((size_t)nunlimdims * sizeof(int))))
return NC_ENOMEM;
status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
if(status != NC_NOERR) {
Expand Down Expand Up @@ -1263,7 +1263,7 @@ NC_check_nulls(int ncid, int varid, const size_t *start, size_t **count,
/* If count is NULL, assume full extent of var. */
if (!*count)
{
if (!(*count = malloc(varndims * sizeof(size_t))))
if (!(*count = malloc((size_t)varndims * sizeof(size_t))))
return NC_ENOMEM;
if ((stat = NC_getshape(ncid, varid, varndims, *count)))
{
Expand All @@ -1279,7 +1279,7 @@ NC_check_nulls(int ncid, int varid, const size_t *start, size_t **count,
{
int i;

if (!(*stride = malloc(varndims * sizeof(ptrdiff_t))))
if (!(*stride = malloc((size_t)varndims * sizeof(ptrdiff_t))))
return NC_ENOMEM;
for (i = 0; i < varndims; i++)
(*stride)[i] = 1;
Expand Down
4 changes: 2 additions & 2 deletions libdispatch/ncexhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ exhashsplit(NCexhashmap* map, ncexhashkey_t hkey, NCexleaf* leaf)
}

/* Re-build the old leaf; keep same uid */
if((leaf->entries = (NCexentry*)calloc(map->leaflen,sizeof(NCexentry))) == NULL)
if((leaf->entries = (NCexentry*)calloc((size_t)map->leaflen, sizeof(NCexentry))) == NULL)
{stat = NC_ENOMEM; goto done;}
leaf->active = 0;

Expand Down Expand Up @@ -588,7 +588,7 @@ exhashnewleaf(NCexhashmap* map, NCexleaf** leafp)
if((leaf = calloc(1,sizeof(NCexleaf))) == NULL)
goto done;
assert(map->leaflen > 0);
if((leaf->entries = calloc(map->leaflen,sizeof(NCexentry))) == NULL)
if((leaf->entries = calloc((size_t)map->leaflen, sizeof(NCexentry))) == NULL)
goto done;
leaf->uid = map->uid++;
*leafp = leaf; leaf = NULL;
Expand Down
4 changes: 2 additions & 2 deletions libdispatch/ncjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -798,9 +798,9 @@ listappend(struct NCjlist* list, NCjson* json)
list->contents[0] = json;
list->len++;
} else {
if((newcontents = (NCjson**)calloc((2*list->len)+1,sizeof(NCjson*)))==NULL)
if((newcontents = (NCjson**)calloc((size_t)(2*list->len)+1,sizeof(NCjson*)))==NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
memcpy(newcontents,list->contents,list->len*sizeof(NCjson*));
memcpy(newcontents,list->contents, (size_t)list->len*sizeof(NCjson*));
newcontents[list->len] = json;
list->len++;
free(list->contents);
Expand Down
2 changes: 1 addition & 1 deletion libdispatch/utf8proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ static nc_utf8proc_ssize_t nc_seqindex_write_char_decomposed(nc_utf8proc_uint16_
*dstptr = NULL;
result = nc_utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data);
if (result < 0) return result;
buffer = (nc_utf8proc_int32_t *) malloc(result * sizeof(nc_utf8proc_int32_t) + 1);
buffer = (nc_utf8proc_int32_t *) malloc((size_t)result * sizeof(nc_utf8proc_int32_t) + 1);
if (!buffer) return UTF8PROC_ERROR_NOMEM;
result = nc_utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data);
if (result < 0) {
Expand Down
4 changes: 2 additions & 2 deletions libhdf5/hdf5internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ find_var_dim_max_length(NC_GRP_INFO_T *grp, int varid, int dimid,
BAIL(NC_EHDFERR);
if (dataset_ndims != var->ndims)
BAIL(NC_EHDFERR);
if (!(h5dimlen = malloc(dataset_ndims * sizeof(hsize_t))))
if (!(h5dimlen = malloc((size_t)dataset_ndims * sizeof(hsize_t))))
BAIL(NC_ENOMEM);
if (!(h5dimlenmax = malloc(dataset_ndims * sizeof(hsize_t))))
if (!(h5dimlenmax = malloc((size_t)dataset_ndims * sizeof(hsize_t))))
BAIL(NC_ENOMEM);
if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid,
h5dimlen, h5dimlenmax)) < 0)
Expand Down
12 changes: 6 additions & 6 deletions libhdf5/hdf5open.c
Original file line number Diff line number Diff line change
Expand Up @@ -1365,9 +1365,9 @@ get_attached_info(NC_VAR_INFO_T *var, NC_HDF5_VAR_INFO_T *hdf5_var, int ndims,
* attached for each dimension, and the HDF5 object IDs of the
* scale(s). */
assert(!hdf5_var->dimscale_hdf5_objids);
if (!(hdf5_var->dimscale_attached = calloc(ndims, sizeof(nc_bool_t))))
if (!(hdf5_var->dimscale_attached = calloc((size_t)ndims, sizeof(nc_bool_t))))
return NC_ENOMEM;
if (!(hdf5_var->dimscale_hdf5_objids = malloc(ndims *
if (!(hdf5_var->dimscale_hdf5_objids = malloc((size_t)ndims *
sizeof(struct hdf5_objid))))
return NC_ENOMEM;

Expand Down Expand Up @@ -1886,7 +1886,7 @@ read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att)
&type_size)))
return retval;
{
if (!(att->data = malloc((unsigned int)(att->len * type_size))))
if (!(att->data = malloc((unsigned int)((size_t)att->len * type_size))))
BAIL(NC_ENOMEM);

/* For a fixed length HDF5 string, the read requires
Expand All @@ -1907,7 +1907,7 @@ read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att)
char** dst = NULL;

/* Alloc space for the contiguous memory read. */
if (!(contig_buf = malloc(att->len * fixed_size * sizeof(char))))
if (!(contig_buf = malloc((size_t)att->len * fixed_size * sizeof(char))))
BAIL(NC_ENOMEM);

/* Read the fixed-len strings as one big block. */
Expand Down Expand Up @@ -2088,7 +2088,7 @@ read_type(NC_GRP_INFO_T *grp, hid_t hdf_typeid, char *type_name)
return NC_EHDFERR;
LOG((5, "compound type has %d members", nmembers));
type->u.c.field = nclistnew();
nclistsetalloc(type->u.c.field,nmembers);
nclistsetalloc(type->u.c.field, (size_t)nmembers);

for (m = 0; m < nmembers; m++)
{
Expand Down Expand Up @@ -2253,7 +2253,7 @@ read_type(NC_GRP_INFO_T *grp, hid_t hdf_typeid, char *type_name)
if ((nmembers = H5Tget_nmembers(hdf_typeid)) < 0)
return NC_EHDFERR;
type->u.e.enum_member = nclistnew();
nclistsetalloc(type->u.e.enum_member,nmembers);
nclistsetalloc(type->u.e.enum_member, (size_t)nmembers);

/* Allocate space for one value. */
if (!(value = calloc(1, type_size)))
Expand Down
2 changes: 1 addition & 1 deletion libhdf5/hdf5var.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ NC4_def_var(int ncid, const char *name, nc_type xtype, int ndims,
* remember whether dimension scales have been attached to each
* dimension. */
if (!hdf5_var->dimscale && ndims)
if (!(hdf5_var->dimscale_attached = calloc(ndims, sizeof(nc_bool_t))))
if (!(hdf5_var->dimscale_attached = calloc((size_t)ndims, sizeof(nc_bool_t))))
BAIL(NC_ENOMEM);

/* Return the varid. */
Expand Down
4 changes: 3 additions & 1 deletion libncxml/ncxml_xml2.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Copyright 2018-2018 University Corporation for Atmospheric Research/Unidata. */

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <libxml2/libxml/parser.h>
Expand Down Expand Up @@ -139,7 +140,8 @@ ncxml_attr_pairs(ncxml_t xml0, char*** pairsp)
char** pairs = NULL;
xmlNode* xml = (xmlNode*)xml0;
xmlAttr* attr = NULL;
int i,count = 0;
int i;
size_t count = 0;

if(xml == NULL) return 0;
/* First count */
Expand Down
10 changes: 5 additions & 5 deletions libnczarr/zmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ nczm_divide_at(const char* key, int nsegs, char** prefixp, char** suffixp)
/* p should point at the presegs+1 start point */
delta = (p-key);
if(prefixp) {
prefix = malloc(delta+1);
memcpy(prefix,key,delta);
prefix = malloc((size_t)delta+1);
memcpy(prefix,key,(size_t)delta);
prefix[delta] = '\0';
*prefixp = prefix;
}
Expand Down Expand Up @@ -436,7 +436,7 @@ nczm_segment1(const char* path, char** seg1p)
q = strchr(p,'/');
if(q == NULL) q = p+strlen(p); /* point to stop character */
delta = (q-p);
if((seg1 = (char*)malloc(delta+1))==NULL)
if((seg1 = (char*)malloc((size_t)delta+1))==NULL)
{ret = NC_ENOMEM; goto done;}
memcpy(seg1,p,delta);
seg1[delta] = '\0';
Expand Down Expand Up @@ -489,9 +489,9 @@ nczm_basename(const char* path, char** basep)
p = strrchr(last,'.');
if(p == NULL) p = last+strlen(last);
delta = (p - last);
if((base = (char*)malloc(delta+1))==NULL)
if((base = (char*)malloc((size_t)delta+1))==NULL)
{stat = NC_ENOMEM; goto done;}
memcpy(base,last,delta);
memcpy(base,last,(size_t)delta);
base[delta] = '\0';
if(basep) {*basep = base; base = NULL;}
done:
Expand Down
10 changes: 5 additions & 5 deletions libnczarr/zodom.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ buildodom(int rank, NCZOdometer** odomp)
if((odom = calloc(1,sizeof(NCZOdometer))) == NULL)
goto done;
odom->rank = rank;
if((odom->start=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem;
if((odom->stop=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem;
if((odom->stride=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem;
if((odom->len=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem;
if((odom->index=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem;
if((odom->start=calloc(1,(sizeof(size64_t)*(size_t)rank)))==NULL) goto nomem;
if((odom->stop=calloc(1,(sizeof(size64_t)*(size_t)rank)))==NULL) goto nomem;
if((odom->stride=calloc(1,(sizeof(size64_t)*(size_t)rank)))==NULL) goto nomem;
if((odom->len=calloc(1,(sizeof(size64_t)*(size_t)rank)))==NULL) goto nomem;
if((odom->index=calloc(1,(sizeof(size64_t)*(size_t)rank)))==NULL) goto nomem;
*odomp = odom; odom = NULL;
}
done:
Expand Down
4 changes: 2 additions & 2 deletions libnczarr/zsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,7 @@ define_vars(NC_FILE_INFO_T* file, NC_GRP_INFO_T* grp, NClist* varnames)
/* Save the rank of the variable */
if((stat = nc4_var_set_ndims(var, rank))) goto done;
/* extract the shapes */
if((shapes = (size64_t*)malloc(sizeof(size64_t)*zarr_rank)) == NULL)
if((shapes = (size64_t*)malloc(sizeof(size64_t)*(size_t)zarr_rank)) == NULL)
{stat = (THROW(NC_ENOMEM)); goto done;}
if((stat = decodeints(jvalue, shapes))) goto done;
}
Expand All @@ -1702,7 +1702,7 @@ define_vars(NC_FILE_INFO_T* file, NC_GRP_INFO_T* grp, NClist* varnames)
var->storage = NC_CHUNKED;
if(var->ndims != rank)
{stat = (THROW(NC_ENCZARR)); goto done;}
if((var->chunksizes = malloc(sizeof(size_t)*zarr_rank)) == NULL)
if((var->chunksizes = malloc(sizeof(size_t)*(size_t)zarr_rank)) == NULL)
{stat = NC_ENOMEM; goto done;}
if((stat = decodeints(jvalue, chunks))) goto done;
/* validate the chunk sizes */
Expand Down
Loading

0 comments on commit d07dac9

Please sign in to comment.