Coverity 711857

This commit is contained in:
Ward Fisher 2014-08-11 13:21:01 -06:00
parent 60461bd138
commit 421b999a0e
3 changed files with 78 additions and 55 deletions

View File

@ -17,7 +17,7 @@ static void genbin_definevardata(Symbol* vsym);
static int genbin_write(Generator*,Symbol*,Bytebuffer*,int,size_t*,size_t*);
static int genbin_writevar(Generator*,Symbol*,Bytebuffer*,int,size_t*,size_t*);
static int genbin_writeattr(Generator*,Symbol*,Bytebuffer*,int,size_t*,size_t*);
static void freeDatalist(Datalist **dlist);
#ifdef USE_NETCDF4
static void genbin_deftype(Symbol* tsym);
static void genbin_definespecialattributes(Symbol* var);
@ -57,7 +57,7 @@ gen_netcdf(const char *filename)
stat = nc_create(filename, cmode_modifier, &ncid);
check_err(stat,__LINE__,__FILE__);
/* ncid created above is also root group*/
rootgroup->ncid = ncid;
@ -136,10 +136,10 @@ gen_netcdf(const char *filename)
if(ngatts > 0) {
for(iatt = 0; iatt < ngatts; iatt++) {
Symbol* gasym = (Symbol*)listget(gattdefs,iatt);
genbin_defineattr(gasym);
genbin_defineattr(gasym);
}
}
/* define per-variable attributes */
if(natts > 0) {
for(iatt = 0; iatt < natts; iatt++) {
@ -187,7 +187,7 @@ genbin_defineglobalspecials(void)
/* Watch out, this is a global Attribute */
format = kind_string(/*Main.*/format_flag);
stat = nc_put_att_text(rootgroup->ncid,NC_GLOBAL,"_Format",strlen(format),format);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
}
#endif /*0*/
@ -206,7 +206,7 @@ genbin_definespecialattributes(Symbol* var)
:NC_CHUNKED),
chunks);
check_err(stat,__LINE__,__FILE__);
}
}
if(special->flags & _FLETCHER32_FLAG) {
stat = nc_def_var_fletcher32(var->container->ncid,
var->ncid,
@ -221,7 +221,7 @@ genbin_definespecialattributes(Symbol* var)
(special->_DeflateLevel >= 0?special->_DeflateLevel
:0));
check_err(stat,__LINE__,__FILE__);
}
}
if(special->flags & _ENDIAN_FLAG) {
stat = nc_def_var_endian(var->container->ncid,
var->ncid,
@ -229,14 +229,14 @@ genbin_definespecialattributes(Symbol* var)
NC_ENDIAN_LITTLE
:NC_ENDIAN_BIG));
check_err(stat,__LINE__,__FILE__);
}
}
if(special->flags & _NOFILL_FLAG) {
stat = nc_def_var_fill(var->container->ncid,
var->ncid,
(special->_Fill?NC_FILL:NC_NOFILL),
NULL);
check_err(stat,__LINE__,__FILE__);
}
}
}
#endif /*USE_NETCDF4*/
@ -266,9 +266,9 @@ genbin_deftype(Symbol* tsym)
tsym->typ.size,
tsym->name,
&tsym->ncid);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
break;
case NC_ENUM:
case NC_ENUM:
{
Bytebuffer* datum;
Datalist* ecdl;
@ -276,7 +276,7 @@ genbin_deftype(Symbol* tsym)
tsym->typ.basetype->typ.typecode,
tsym->name,
&tsym->ncid);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
datum = bbNew();
ecdl = builddatalist(1);
dlextend(ecdl); /* make room for one constant*/
@ -291,29 +291,29 @@ genbin_deftype(Symbol* tsym)
tsym->ncid,
econst->name,
bbContents(datum));
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
}
bbFree(datum);
}
dlfree(&ecdl);
}
break;
case NC_VLEN:
stat = nc_def_vlen(tsym->container->ncid,
tsym->name,
tsym->typ.basetype->ncid,
&tsym->ncid);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
break;
case NC_COMPOUND:
stat = nc_def_compound(tsym->container->ncid,
tsym->typ.size,
tsym->typ.size,
tsym->name,
&tsym->ncid);
check_err(stat,__LINE__,__FILE__);
&tsym->ncid);
check_err(stat,__LINE__,__FILE__);
for(i=0;i<listlength(tsym->subnodes);i++) {
Symbol* efield = (Symbol*)listget(tsym->subnodes,i);
ASSERT(efield->subclass == NC_FIELD);
if(efield->typ.dimset.ndims == 0){
if(efield->typ.dimset.ndims == 0){
stat = nc_insert_compound(
tsym->container->ncid,
tsym->ncid,
@ -337,7 +337,7 @@ genbin_deftype(Symbol* tsym)
efield->typ.dimset.ndims,
dimsizes);
}
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
}
break;
default: panic("definectype: unexpected type subclass");
@ -391,7 +391,7 @@ genbin_writevar(Generator* generator, Symbol* vsym, Bytebuffer* memory,
int stat = NC_NOERR;
char* data = bbContents(memory);
#ifdef USE_NOFILL
size_t count[NC_MAX_VAR_DIMS];
size_t count[NC_MAX_VAR_DIMS];
{ int i; for(i=0;i<rank;i++) count[i] = indices[i] - start[i];}
#endif
@ -409,7 +409,7 @@ genbin_writevar(Generator* generator, Symbol* vsym, Bytebuffer* memory,
fflush(stderr);
}
#endif
if(rank == 0) {
size_t count[1] = {1};
stat = nc_put_var1(vsym->container->ncid, vsym->ncid, count, data);
@ -444,7 +444,7 @@ genbin_writeattr(Generator* generator, Symbol* asym, Bytebuffer* databuf,
case NC_BYTE: {
signed char* data = (signed char*)bbContents(databuf);
stat = nc_put_att_schar(grpid,varid,asym->name,typid,len,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
case NC_CHAR: {
char* data = (char*)bbContents(databuf);
@ -452,27 +452,27 @@ genbin_writeattr(Generator* generator, Symbol* asym, Bytebuffer* databuf,
/* Revise length if slen == 0 */
if(slen == 0) {bbAppend(databuf,'\0'); slen++;}
stat = nc_put_att_text(grpid,varid,asym->name,slen,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
case NC_SHORT: {
short* data = (short*)bbContents(databuf);
stat = nc_put_att_short(grpid,varid,asym->name,typid,len,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
case NC_INT: {
int* data = (int*)bbContents(databuf);
stat = nc_put_att_int(grpid,varid,asym->name,typid,len,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
case NC_FLOAT: {
float* data = (float*)bbContents(databuf);
stat = nc_put_att_float(grpid,varid,asym->name,typid,len,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
case NC_DOUBLE: {
double* data = (double*)bbContents(databuf);
stat = nc_put_att_double(grpid,varid,asym->name,typid,len,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
#ifdef USE_NETCDF4
case NC_STRING: {
@ -485,29 +485,29 @@ genbin_writeattr(Generator* generator, Symbol* asym, Bytebuffer* databuf,
case NC_UBYTE: {
unsigned char* data = (unsigned char*)bbContents(databuf);
stat = nc_put_att_uchar(grpid,varid,asym->name,typid,len,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
case NC_USHORT: {
unsigned short* data = (unsigned short*)bbContents(databuf);
stat = nc_put_att_ushort(grpid,varid,asym->name,typid,len,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
case NC_UINT: {
unsigned int* data = (unsigned int*)bbContents(databuf);
stat = nc_put_att_uint(grpid,varid,asym->name,typid,len,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
case NC_INT64: {
long long* data = (long long*)bbContents(databuf);
stat = nc_put_att_longlong(grpid,varid,asym->name,typid,len,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
case NC_UINT64: {
unsigned long long* data = (unsigned long long*)bbContents(databuf);
stat = nc_put_att_ulonglong(grpid,varid,asym->name,typid,len,data);
check_err(stat,__LINE__,__FILE__);
check_err(stat,__LINE__,__FILE__);
} break;
#endif
#endif
default: PANIC1("genbin_defineattr: unexpected basetype: %d",basetype->typ.typecode);
}
} else { /* use the generic put_attribute for user defined types*/

View File

@ -84,7 +84,7 @@ tztrim(
)
{
char *cp, *ep;
cp = ss;
if (*cp == '-')
cp++;
@ -138,7 +138,7 @@ freeSymbol(Symbol* sym)
case NG_FIELD:
case NG_OPAQUE:
default: break;
}
}
efree(sym->name);
efree(sym);
#endif
@ -169,15 +169,15 @@ nctypename(nc_type nctype)
return nctypenamesextend[(nctype - NC_GRP)];
if(nctype == NC_FILLVALUE) return "NC_FILL";
if(nctype == NC_NIL) return "NC_NIL";
s = poolalloc(128);
s = poolalloc(128);
sprintf(s,"NC_<%d>",nctype);
return s;
}
/* These are the augmented NC_ values (0 based from NC_GRP)*/
char* ncclassnames[9] = {
"NC_GRP", "NC_DIM", "NC_VAR", "NC_ATT",
"NC_TYP", "NC_ECONST", "NC_FIELD", "NC_ARRAY",
char* ncclassnames[9] = {
"NC_GRP", "NC_DIM", "NC_VAR", "NC_ATT",
"NC_TYP", "NC_ECONST", "NC_FIELD", "NC_ARRAY",
"NC_PRIM"
};
@ -190,7 +190,7 @@ ncclassname(nc_class ncc)
if(ncc == NC_FILLVALUE) return "NC_FILL";
if(ncc >= NC_GRP && ncc <= NC_PRIM)
return ncclassnames[ncc - NC_GRP];
s = poolalloc(128);
s = poolalloc(128);
sprintf(s,"NC_<%d>",ncc);
return s;
}
@ -331,7 +331,7 @@ prefixtostring(List* prefix, char* separator)
strcat(result,separator);
strcat(result,sym->name); /* append "/<prefix[i]>"*/
}
}
}
return result;
}
#endif
@ -340,7 +340,7 @@ prefixtostring(List* prefix, char* separator)
char*
fullname(Symbol* sym)
{
#ifdef USE_NETCDF4
#ifdef USE_NETCDF4
char* s1;
char* result;
char* prefix;
@ -357,7 +357,7 @@ int
prefixeq(List* x1, List* x2)
{
Symbol** l1;
Symbol** l2;
Symbol** l2;
int len,i;
if((len=listlength(x1)) != listlength(x2)) return 0;
l1=(Symbol**)listcontents(x1);
@ -377,7 +377,7 @@ prefixdup(List* prefix)
dupseq = listnew();
listsetalloc(dupseq,listlength(prefix));
for(i=0;i<listlength(prefix);i++) listpush(dupseq,listget(prefix,i));
return dupseq;
return dupseq;
}
/*
@ -445,14 +445,14 @@ makebytestring(char* s, size_t* lenp)
int i;
ASSERT((slen%2) == 0);
ASSERT(blen > 0);
ASSERT(blen > 0);
bytes = (unsigned char*)emalloc(blen);
b = bytes;
for(i=0;i<slen;i+=2) {
unsigned int digit1 = chartohex(*s++);
unsigned int digit2 = chartohex(*s++);
unsigned int byte = (digit1 << 4) | digit2;
*b++ = byte;
*b++ = byte;
}
if(lenp) *lenp = blen;
return bytes;
@ -525,7 +525,30 @@ builddatasublist(Datalist* dl)
d.value.compoundv = dl;
d.filled = 0;
return d;
}
/*! Function to free an allocated datalist.
This function is used to free an individual datalist
object. It is possible, hypothetically, that a
datalist will appear in the middle of a set of datalists,
in which case we'll need to determine that and shuffle around
'next' pointers. For the time being, this assumes that
we are freeing a datalist which was allocated locally
and must be discarded.
Using this function instead of an inline 'free' just in
case we ever want to extend it, we won't have to go back
and re-write a bunch of stuff. I hope.
@param dlist Pointer to datalist object being freed.
*/
void dlfree(Datalist **dlist) {
if(*dlist) free(*dlist);
dlist = NULL;
}
static void
@ -534,7 +557,7 @@ constantFree(NCConstant* con)
switch(con->nctype) {
case NC_COMPOUND:
/* do nothing; ReclaimDatalists below will take care of the datalist */
break;
break;
case NC_STRING:
if(con->value.stringv.len > 0 && con->value.stringv.stringv != NULL)
efree(con->value.stringv.stringv);
@ -560,7 +583,7 @@ reclaimDatalists(void)
for(i=0,con=list->data;i<list->length;i++,con++)
constantFree(con);
list->data = NULL;
}
}
}
/* Step 2: free up the datalist itself*/
for(list=alldatalists;list != NULL;) {
@ -600,7 +623,7 @@ crossproduct(Dimset* dimset, int start, int stop)
for(i=start;i<stop;i++) {
totalsize = totalsize * dimset->dimsyms[i]->dim.declsize;
}
return totalsize;
return totalsize;
}
/* Do the "complement" of crossproduct;
@ -618,7 +641,7 @@ prefixarraylength(Dimset* dimset, int last)
#ifdef USE_NETCDF4
extern int H5Eprint1(FILE * stream);
#endif
#endif
void
check_err(const int stat, const int line, const char* file) {
@ -627,7 +650,7 @@ check_err(const int stat, const int line, const char* file) {
fprintf(stderr, "\t(%s:%d)\n", file,line);
#ifdef USE_NETCDF4
H5Eprint1(stderr);
#endif
#endif
fflush(stderr);
exit(1);
}
@ -669,4 +692,3 @@ kind_string(int kind)
}
return NULL;
}

View File

@ -53,6 +53,7 @@ extern unsigned char* makebytestring(char* s, size_t* lenp);
extern int getpadding(int offset, int alignment);
extern Datalist* builddatalist(int initialize);
extern void dlfree(Datalist **dlist);
extern void dlappend(Datalist*, NCConstant*);
extern NCConstant builddatasublist(Datalist* dl);
extern void dlextend(Datalist* dl);