Skip to content

Commit

Permalink
Replaced some sprintf with snprintf with aid of new variable containi…
Browse files Browse the repository at this point in the history
…ng size

One case required slightly complicated accounting of how much space is left in the buffer.
  • Loading branch information
seanm committed Sep 2, 2023
1 parent 9a46fc7 commit cd255af
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
7 changes: 4 additions & 3 deletions libhdf5/hdf5var.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ give_var_secret_name(NC_VAR_INFO_T *var, const char *name)
* clash. */
if (strlen(name) + strlen(NON_COORD_PREPEND) > NC_MAX_NAME)
return NC_EMAXNAME;
if (!(var->alt_name = malloc((strlen(NON_COORD_PREPEND) +
strlen(name) + 1) * sizeof(char))))
size_t alt_name_size = (strlen(NON_COORD_PREPEND) + strlen(name) + 1) *
sizeof(char);
if (!(var->alt_name = malloc(alt_name_size)))
return NC_ENOMEM;

sprintf(var->alt_name, "%s%s", NON_COORD_PREPEND, name);
snprintf(var->alt_name, alt_name_size, "%s%s", NON_COORD_PREPEND, name);

return NC_NOERR;
}
Expand Down
7 changes: 4 additions & 3 deletions libnczarr/zvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,12 @@ give_var_secret_name(NC_VAR_INFO_T *var, const char *name)
* clash. */
if (strlen(name) + strlen(NON_COORD_PREPEND) > NC_MAX_NAME)
return NC_EMAXNAME;
if (!(var->ncz_name = malloc((strlen(NON_COORD_PREPEND) +
strlen(name) + 1) * sizeof(char))))
size_t ncz_name_size = (strlen(NON_COORD_PREPEND) + strlen(name) + 1) *
sizeof(char);
if (!(var->ncz_name = malloc(ncz_name_size)))
return NC_ENOMEM;

sprintf(var->ncz_name, "%s%s", NON_COORD_PREPEND, name);
snprintf(var->ncz_name, ncz_name_size, "%s%s", NON_COORD_PREPEND, name);

return NC_NOERR;
}
Expand Down
5 changes: 3 additions & 2 deletions nc_test/tst_def_var_fill.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ int main(int argc, char** argv) {
if (argc == 2) snprintf(filename, 256, "%s", argv[1]);
else strcpy(filename, "tst_def_var_fill.nc");

char *cmd_str = (char*)malloc(strlen(argv[0]) + 256);
sprintf(cmd_str, "*** TESTING C %s for def_var_fill ", argv[0]);
size_t cmd_str_len = strlen(argv[0]) + 256;
char *cmd_str = (char*)malloc(cmd_str_len);
snprintf(cmd_str, cmd_str_len, "*** TESTING C %s for def_var_fill ", argv[0]);
printf("%-66s ------ ", cmd_str); fflush(stdout);
free(cmd_str);

Expand Down
6 changes: 4 additions & 2 deletions ncdump/dumplib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,8 @@ chars_tostring(
{
long iel;
const char *sp;
char *sout = (char *)emalloc(4*len + 5); /* max len of string */
size_t sout_size = 4*len + 5; /* max len of string */
char *sout = (char *)emalloc(sout_size);
char *cp = sout;
*cp++ = '"';

Expand All @@ -1061,7 +1062,8 @@ chars_tostring(
if (isprint(uc))
*cp++ = *(char *)&uc; /* just copy, even if char is signed */
else {
sprintf(cp,"\\%.3o",uc);
size_t remaining = sout_size - (cp - sout);
snprintf(cp,remaining,"\\%.3o",uc);
cp += 4;
}
break;
Expand Down
10 changes: 6 additions & 4 deletions ncgen/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ 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);
sprintf(s,"NC_<%d>",nctype);
const size_t s_size = 128;
s = poolalloc(s_size);
snprintf(s,s_size,"NC_<%d>",nctype);
return s;
}

Expand All @@ -195,8 +196,9 @@ 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);
sprintf(s,"NC_<%d>",ncc);
const size_t s_size = 128;
s = poolalloc(s_size);
snprintf(s,s_size,"NC_<%d>",ncc);
return s;
}

Expand Down
52 changes: 32 additions & 20 deletions ncgen3/genlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ cstring(
int *intp;
float *floatp;
double *doublep;
size_t cp_size;

switch (type) {
case NC_CHAR:
Expand Down Expand Up @@ -162,34 +163,39 @@ cstring(
return sp;

case NC_BYTE:
cp = (char *) emalloc (7);
cp_size = 7;
cp = (char *) emalloc (cp_size);
bytep = (signed char *)valp;
/* Need to convert '\377' to -1, for example, on all platforms */
(void) sprintf(cp,"%d", (signed char) *(bytep+num));
(void) snprintf(cp,cp_size,"%d", (signed char) *(bytep+num));
return cp;

case NC_SHORT:
cp = (char *) emalloc (10);
cp_size = 10;
cp = (char *) emalloc (cp_size);
shortp = (short *)valp;
(void) sprintf(cp,"%d",* (shortp + num));
(void) snprintf(cp,cp_size,"%d",* (shortp + num));
return cp;

case NC_INT:
cp = (char *) emalloc (20);
cp_size = 20;
cp = (char *) emalloc (cp_size);
intp = (int *)valp;
(void) sprintf(cp,"%d",* (intp + num));
(void) snprintf(cp,cp_size,"%d",* (intp + num));
return cp;

case NC_FLOAT:
cp = (char *) emalloc (20);
cp_size = 20;
cp = (char *) emalloc (cp_size);
floatp = (float *)valp;
(void) sprintf(cp,"%.8g",* (floatp + num));
(void) snprintf(cp,cp_size,"%.8g",* (floatp + num));
return cp;

case NC_DOUBLE:
cp = (char *) emalloc (20);
cp_size = 20;
cp = (char *) emalloc (cp_size);
doublep = (double *)valp;
(void) sprintf(cp,"%.16g",* (doublep + num));
(void) snprintf(cp,cp_size,"%.16g",* (doublep + num));
return cp;

default:
Expand Down Expand Up @@ -1046,36 +1052,42 @@ fstring(
int *intp;
float *floatp;
double *doublep;
size_t cp_size;

switch (type) {
case NC_BYTE:
cp = (char *) emalloc (10);
cp_size = 10;
cp = (char *) emalloc (cp_size);
schp = (signed char *)valp;
sprintf(cp,"%d", schp[num]);
snprintf(cp,cp_size,"%d", schp[num]);
return cp;

case NC_SHORT:
cp = (char *) emalloc (10);
cp_size = 10;
cp = (char *) emalloc (cp_size);
shortp = (short *)valp;
(void) sprintf(cp,"%d",* (shortp + num));
(void) snprintf(cp,cp_size,"%d",* (shortp + num));
return cp;

case NC_INT:
cp = (char *) emalloc (20);
cp_size = 20;
cp = (char *) emalloc (cp_size);
intp = (int *)valp;
(void) sprintf(cp,"%d",* (intp + num));
(void) snprintf(cp,cp_size,"%d",* (intp + num));
return cp;

case NC_FLOAT:
cp = (char *) emalloc (20);
cp_size = 20;
cp = (char *) emalloc (cp_size);
floatp = (float *)valp;
(void) sprintf(cp,"%.8g",* (floatp + num));
(void) snprintf(cp,cp_size,"%.8g",* (floatp + num));
return cp;

case NC_DOUBLE:
cp = (char *) emalloc (25);
cp_size = 25;
cp = (char *) emalloc (cp_size);
doublep = (double *)valp;
(void) sprintf(cp,"%.16g",* (doublep + num));
(void) snprintf(cp,cp_size,"%.16g",* (doublep + num));
expe2d(cp); /* change 'e' to 'd' in exponent */
return cp;

Expand Down

0 comments on commit cd255af

Please sign in to comment.