From dfc2ac729690ccc7d89ae657e5c574120476820e Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Sun, 30 Apr 2023 18:01:49 -0400 Subject: [PATCH 1/2] Replaced trivial uses of sprintf with snprintf In all these cases the size of the buffer can be computed with sizeof. --- h5_test/tst_h_compounds.c | 10 +-- h5_test/tst_h_dimscales.c | 2 +- h5_test/tst_h_dimscales4.c | 2 +- h5_test/tst_h_files.c | 6 +- h5_test/tst_h_mem.c | 2 +- h5_test/tst_h_vars.c | 2 +- h5_test/tst_h_vars2.c | 2 +- hdf4_test/tst_interops2.c | 2 +- libdap2/dapodom.c | 2 +- libdap2/dcetab.c | 2 +- libdap4/d4odom.c | 2 +- libdispatch/dparallel.c | 2 +- libdispatch/nctime.c | 2 +- libhdf5/hdf5open.c | 2 +- libhdf5/nc4hdf.c | 10 +-- libsrc/ffio.c | 2 +- libsrc/pstdint.h | 20 ++--- libsrc4/nc4internal.c | 4 +- nc_perf/bm_file.c | 4 +- nc_perf/bm_many_atts.c | 4 +- nc_perf/bm_many_objs.c | 6 +- nc_perf/tst_attsperf.c | 8 +- nc_perf/tst_bm_rando.c | 6 +- nc_perf/tst_chunks3.c | 36 ++++----- nc_perf/tst_compress.c | 4 +- nc_perf/tst_compress_par.c | 4 +- nc_perf/tst_create_files.c | 10 +-- nc_perf/tst_files2.c | 16 ++-- nc_perf/tst_h_many_atts.c | 2 +- nc_perf/tst_wrf_reads.c | 6 +- nc_test/large_files.c | 2 +- nc_test/quick_large_files.c | 2 +- nc_test/testnc3perf.c | 12 +-- nc_test/tst_addvar.c | 2 +- nc_test/tst_big_rvar.c | 2 +- nc_test/tst_big_var.c | 2 +- nc_test/tst_big_var2.c | 2 +- nc_test/tst_big_var6.c | 2 +- nc_test/tst_cdf5format.c | 6 +- nc_test/tst_diskless3.c | 2 +- nc_test/tst_diskless4.c | 2 +- nc_test/tst_formats.c | 8 +- nc_test/tst_large.c | 2 +- nc_test/tst_large_cdf5.c | 2 +- nc_test/tst_parallel2.c | 2 +- nc_test/tst_pnetcdf.c | 8 +- nc_test/tst_small.c | 4 +- nc_test/util.c | 6 +- nc_test4/tst_atts2.c | 2 +- nc_test4/tst_chunks.c | 8 +- nc_test4/tst_chunks2.c | 16 ++-- nc_test4/tst_compounds.c | 4 +- nc_test4/tst_compounds3.c | 4 +- nc_test4/tst_dims2.c | 2 +- nc_test4/tst_files.c | 2 +- nc_test4/tst_grps.c | 4 +- nc_test4/tst_interops4.c | 8 +- nc_test4/tst_interops5.c | 4 +- nc_test4/tst_large2.c | 2 +- nc_test4/tst_large3.c | 2 +- nc_test4/tst_large5.c | 2 +- nc_test4/tst_nc4perf.c | 8 +- nc_test4/tst_parallel.c | 2 +- nc_test4/tst_parallel3.c | 2 +- nc_test4/tst_parallel4.c | 2 +- nc_test4/tst_parallel_zlib.c | 4 +- nc_test4/tst_quantize.c | 6 +- nc_test4/tst_quantize_par.c | 4 +- nc_test4/tst_rename2.c | 12 +-- nc_test4/tst_rename3.c | 6 +- nc_test4/tst_simplerw_coll_r.c | 2 +- nc_test4/tst_strings.c | 2 +- nc_test4/tst_types.c | 4 +- nc_test4/tst_vars.c | 2 +- nc_test4/tst_vars3.c | 2 +- ncdump/ncvalidator.c | 10 +-- ncdump/ocprint.c | 4 +- ncgen/cmldata.c | 22 +++--- ncgen/cvt.c | 22 +++--- ncgen/dump.c | 24 +++--- ncgen/f77data.c | 14 ++-- ncgen/gencml.c | 2 +- ncgen/genjjni.c | 26 +++---- ncgen/jdatajni.c | 14 ++-- ncgen/ncgen.l | 24 +++--- ncgen/ncgen.y | 4 +- ncgen/ncgenl.c | 24 +++--- ncgen/ncgeny.c | 6 +- ncgen/odom.c | 2 +- ncgen3/genlib.c | 136 ++++++++++++++++----------------- ncgen3/load.c | 82 ++++++++++---------- ncgen3/ncgen.l | 14 ++-- ncgen3/ncgenl.c | 14 ++-- ncgen3/ncgeny.c | 2 +- nctest/driver.c | 2 +- nczarr_test/bm_chunks3.c | 4 +- nczarr_test/test_zchunks.c | 8 +- nczarr_test/test_zchunks2.c | 16 ++-- oc2/dapparse.c | 4 +- oc2/dapy.c | 2 +- oc2/ocdump.c | 18 ++--- 101 files changed, 434 insertions(+), 434 deletions(-) diff --git a/h5_test/tst_h_compounds.c b/h5_test/tst_h_compounds.c index c53032c3f9..952e2585a3 100644 --- a/h5_test/tst_h_compounds.c +++ b/h5_test/tst_h_compounds.c @@ -452,7 +452,7 @@ main() hr_data_out[i].starfleet_id = i; hr_data_out[i].svc_rec.i1 = 95; hr_data_out[i].svc_rec.i2 = 90; - if (sprintf(hr_data_out[i].name, "alien_%d", i) < 0) ERR; + if (snprintf(hr_data_out[i].name, sizeof(hr_data_out[i].name), "alien_%d", i) < 0) ERR; hr_data_out[i].max_temp = 99.99f; hr_data_out[i].min_temp = -9.99f; hr_data_out[i].percent_transporter_errosion = .1; @@ -557,7 +557,7 @@ main() hr_data_out[i].starfleet_id = i; hr_data_out[i].svc_rec.i1 = 95; hr_data_out[i].svc_rec.i2 = 90; - if (sprintf(hr_data_out[i].name, "alien_%d", i) < 0) ERR; + if (snprintf(hr_data_out[i].name, sizeof(hr_data_out[i].name), "alien_%d", i) < 0) ERR; hr_data_out[i].max_temp = 99.99f; hr_data_out[i].min_temp = -9.99f; hr_data_out[i].percent_transporter_errosion = .1; @@ -665,7 +665,7 @@ main() hr_data_out[i].starfleet_id = i; hr_data_out[i].svc_rec.i1 = 95; hr_data_out[i].svc_rec.i2 = 90; - if (sprintf(hr_data_out[i].name, "alien_%d", i) < 0) ERR; + if (snprintf(hr_data_out[i].name, sizeof(hr_data_out[i].name), "alien_%d", i) < 0) ERR; hr_data_out[i].max_temp = 99.99f; hr_data_out[i].min_temp = -9.99f; hr_data_out[i].percent_transporter_errosion = .1; @@ -756,7 +756,7 @@ main() /* Create some phony data. */ for (i = 0; i < DIM1_LEN; i++) { - if (sprintf(hr_data_out[i].name, "alien_%d", i) < 0) ERR; + if (snprintf(hr_data_out[i].name, sizeof(hr_data_out[i].name), "alien_%d", i) < 0) ERR; hr_data_out[i].max_temp = 99.99f; } @@ -841,7 +841,7 @@ main() /* Create some phony data. */ for (i = 0; i < DIM2_LEN; i++) { - if (sprintf(hr_data_out[i].name, "alien_%d", i) < 0) ERR; + if (snprintf(hr_data_out[i].name, sizeof(hr_data_out[i].name), "alien_%d", i) < 0) ERR; hr_data_out[i].max_temp = 99.99f; } diff --git a/h5_test/tst_h_dimscales.c b/h5_test/tst_h_dimscales.c index 7186778fe7..8eeee14c42 100644 --- a/h5_test/tst_h_dimscales.c +++ b/h5_test/tst_h_dimscales.c @@ -308,7 +308,7 @@ main() if ((var1_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; for (v = 0; v < NUM_DATASETS; v++) { - sprintf(var_name, "var_%d", v); + snprintf(var_name, sizeof(var_name), "var_%d", v); if ((var1_datasetid[v] = H5Dcreate1(grpid, var_name, H5T_NATIVE_INT, var1_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(var1_datasetid[v], dimscaleid, 0) < 0) ERR; diff --git a/h5_test/tst_h_dimscales4.c b/h5_test/tst_h_dimscales4.c index c12ca9624e..430960926b 100644 --- a/h5_test/tst_h_dimscales4.c +++ b/h5_test/tst_h_dimscales4.c @@ -93,7 +93,7 @@ main() spaceid, create_propid)) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; if (H5Pclose(create_propid) < 0) ERR; - sprintf(dimscale_wo_var, "%s%10d", DIM_WITHOUT_VARIABLE, DIM_LEN); + snprintf(dimscale_wo_var, sizeof(dimscale_wo_var), "%s%10d", DIM_WITHOUT_VARIABLE, DIM_LEN); if (H5DSset_scale(dimscaleid, dimscale_wo_var) < 0) ERR; /* Create a variable that uses this dimension scale. */ diff --git a/h5_test/tst_h_files.c b/h5_test/tst_h_files.c index f4bc617f0c..15765ea5bd 100644 --- a/h5_test/tst_h_files.c +++ b/h5_test/tst_h_files.c @@ -248,7 +248,7 @@ main() void *bufr; void *fillp = NULL; - sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE_NAME); /* Create file access and create property lists. */ if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; @@ -285,7 +285,7 @@ main() spaceid, plistid)) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; if (H5Pclose(plistid) < 0) ERR; - sprintf(dimscale_wo_var, "%s%10d", DIM_WITHOUT_VARIABLE, DIM1); + snprintf(dimscale_wo_var, sizeof(dimscale_wo_var), "%s%10d", DIM_WITHOUT_VARIABLE, DIM1); if (H5DSset_scale(dim1_dimscaleid, dimscale_wo_var) < 0) ERR; /* Create the dim2 dimscale. */ @@ -299,7 +299,7 @@ main() spaceid, plistid)) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; if (H5Pclose(plistid) < 0) ERR; - sprintf(dimscale_wo_var, "%s%10d", DIM_WITHOUT_VARIABLE, DIM2); + snprintf(dimscale_wo_var, sizeof(dimscale_wo_var), "%s%10d", DIM_WITHOUT_VARIABLE, DIM2); if (H5DSset_scale(dim2_dimscaleid, dimscale_wo_var) < 0) ERR; /* Now create the 2D dataset. */ diff --git a/h5_test/tst_h_mem.c b/h5_test/tst_h_mem.c index d2b1d4a242..d7b0b9e5d1 100644 --- a/h5_test/tst_h_mem.c +++ b/h5_test/tst_h_mem.c @@ -67,7 +67,7 @@ main() /* Create the variables. */ for (v = 0; v < NUM_DATASETS; v++) { - sprintf(var_name, "var_%d", v); + snprintf(var_name, sizeof(var_name), "var_%d", v); /* printf("creating var %s\n", var_name);*/ if ((datasetid[v] = H5Dcreate1(grpid, var_name, H5T_NATIVE_INT, spaceid, plistid)) < 0) ERR_RET; diff --git a/h5_test/tst_h_vars.c b/h5_test/tst_h_vars.c index 77be6abd4f..7fa840f412 100644 --- a/h5_test/tst_h_vars.c +++ b/h5_test/tst_h_vars.c @@ -517,7 +517,7 @@ main() "szip_and_zlib"}; /* Open file and create group. */ - sprintf(file_name, "%s_%s.h5", TEST_NAME, desc[f]); + snprintf(file_name, sizeof(file_name), "%s_%s.h5", TEST_NAME, desc[f]); if ((fileid = H5Fcreate(file_name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate1(fileid, GRP_NAME, 0)) < 0) ERR; diff --git a/h5_test/tst_h_vars2.c b/h5_test/tst_h_vars2.c index 400be6bb49..e53b677529 100644 --- a/h5_test/tst_h_vars2.c +++ b/h5_test/tst_h_vars2.c @@ -478,7 +478,7 @@ main() /* Create the variables. */ for (v = 0; v < NUM_DATASETS; v++) { - sprintf(var_name, "var_%d", v); + snprintf(var_name, sizeof(var_name), "var_%d", v); /* printf("creating var %s\n", var_name);*/ if ((datasetid[v] = H5Dcreate1(grpid, var_name, H5T_NATIVE_INT, spaceid, plistid)) < 0) ERR_RET; diff --git a/hdf4_test/tst_interops2.c b/hdf4_test/tst_interops2.c index 2b7ff2681a..7951dc5e0e 100644 --- a/hdf4_test/tst_interops2.c +++ b/hdf4_test/tst_interops2.c @@ -141,7 +141,7 @@ main(int argc, char **argv) /* Create some HDF4 datasets. */ for (t = 0; t < NUM_TYPES; t++) { - sprintf(tmp_name, "hdf4_dataset_type_%d", t); + snprintf(tmp_name, sizeof(tmp_name), "hdf4_dataset_type_%d", t); if ((sds_id = SDcreate(sd_id, tmp_name, hdf4_type[t], DIMS_3, dim_size)) == FAIL) ERR; /* Set up dimensions. By giving them the same names for each diff --git a/libdap2/dapodom.c b/libdap2/dapodom.c index 2c6611a98d..c8d4a22396 100644 --- a/libdap2/dapodom.c +++ b/libdap2/dapodom.c @@ -81,7 +81,7 @@ dapodom_print(Dapodometer* odom) if(odom->rank == 0) { strlcat(line,"[]",sizeof(line)); } else for(i=0;irank;i++) { - sprintf(tmp,"[%lu/%lu:%lu:%lu]", + snprintf(tmp,sizeof(tmp),"[%lu/%lu:%lu:%lu]", (size_t)odom->index[i], (size_t)odom->start[i], (size_t)odom->stride[i], diff --git a/libdap2/dcetab.c b/libdap2/dcetab.c index 60a602449d..4b4adf465b 100644 --- a/libdap2/dcetab.c +++ b/libdap2/dcetab.c @@ -978,7 +978,7 @@ yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg, return 1; } - /* Avoid sprintf, as that infringes on the user's name space. + /* Avoid snprintf, as that infringes on the user's name space. Don't have undefined behavior even if the translation produced a string with the wrong number of "%s"s. */ { diff --git a/libdap4/d4odom.c b/libdap4/d4odom.c index 1b7f650810..ed7d4639fd 100644 --- a/libdap4/d4odom.c +++ b/libdap4/d4odom.c @@ -78,7 +78,7 @@ d4odom_print(D4odometer* odom) if(odom->rank == 0) { strlcat(line,"[]",sizeof(line)); } else for(i=0;irank;i++) { - sprintf(tmp,"[%lu/%lu:%lu:%lu]", + snprintf(tmp,sizeof(tmp),"[%lu/%lu:%lu:%lu]", (size_t)odom->index[i], (size_t)odom->start[i], (size_t)odom->stride[i], diff --git a/libdispatch/dparallel.c b/libdispatch/dparallel.c index 6a9aba720c..3968c6fd0c 100644 --- a/libdispatch/dparallel.c +++ b/libdispatch/dparallel.c @@ -76,7 +76,7 @@ parallel I/O. MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); - sprintf(file_name, "%s/%s", TEMP_LARGE, FILE); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE); if ((res = nc_create_par(file_name, NC_NETCDF4, comm, info, &ncid))) ERR; if (nc_def_dim(ncid, "d1", DIMSIZE, dimids)) ERR; diff --git a/libdispatch/nctime.c b/libdispatch/nctime.c index e265ba1f49..aa937de54b 100644 --- a/libdispatch/nctime.c +++ b/libdispatch/nctime.c @@ -386,7 +386,7 @@ cdParseRelunits(cdCalenType timetype, char* relunits, cdUnitTime* unit, cdCompTi else{ cdTrim(basetime_1,CD_MAX_CHARTIME); cdTrim(basetime_2,CD_MAX_CHARTIME); - sprintf(basetime,"%s %s",basetime_1,basetime_2); + snprintf(basetime,sizeof(basetime),"%s %s",basetime_1,basetime_2); } } diff --git a/libhdf5/hdf5open.c b/libhdf5/hdf5open.c index 0c2f5b5234..6e7154493b 100644 --- a/libhdf5/hdf5open.c +++ b/libhdf5/hdf5open.c @@ -482,7 +482,7 @@ create_phony_dims(NC_GRP_INFO_T *grp, hid_t hdf_datasetid, NC_VAR_INFO_T *var) if (!match) { char phony_dim_name[NC_MAX_NAME + 1]; - sprintf(phony_dim_name, "phony_dim_%d", grp->nc4_info->next_dimid); + snprintf(phony_dim_name, sizeof(phony_dim_name), "phony_dim_%d", grp->nc4_info->next_dimid); LOG((3, "%s: creating phony dim for var %s", __func__, var->hdr.name)); /* Add phony dim to metadata list. */ diff --git a/libhdf5/nc4hdf.c b/libhdf5/nc4hdf.c index a84abd0223..89cc5f973a 100644 --- a/libhdf5/nc4hdf.c +++ b/libhdf5/nc4hdf.c @@ -702,13 +702,13 @@ write_quantize_att(NC_VAR_INFO_T *var) switch (var->quantize_mode) { case NC_QUANTIZE_BITGROOM: - sprintf(att_name, "%s", NC_QUANTIZE_BITGROOM_ATT_NAME); + snprintf(att_name, sizeof(att_name), "%s", NC_QUANTIZE_BITGROOM_ATT_NAME); break; case NC_QUANTIZE_GRANULARBR: - sprintf(att_name, "%s", NC_QUANTIZE_GRANULARBR_ATT_NAME); + snprintf(att_name, sizeof(att_name), "%s", NC_QUANTIZE_GRANULARBR_ATT_NAME); break; case NC_QUANTIZE_BITROUND: - sprintf(att_name, "%s", NC_QUANTIZE_BITROUND_ATT_NAME); + snprintf(att_name, sizeof(att_name), "%s", NC_QUANTIZE_BITROUND_ATT_NAME); break; default: return NC_EINVAL; @@ -1826,7 +1826,7 @@ nc4_create_dim_wo_var(NC_DIM_INFO_T *dim) /* Indicate that this is a scale. Also indicate that not * be shown to the user as a variable. It is hidden. It is * a DIM WITHOUT A VARIABLE! */ - sprintf(dimscale_wo_var, "%s%10d", DIM_WITHOUT_VARIABLE, (int)dim->len); + snprintf(dimscale_wo_var, sizeof(dimscale_wo_var), "%s%10d", DIM_WITHOUT_VARIABLE, (int)dim->len); if (H5DSset_scale(hdf5_dim->hdf_dimscaleid, dimscale_wo_var) < 0) BAIL(NC_EHDFERR); @@ -2257,7 +2257,7 @@ nc4_rec_match_dimscales(NC_GRP_INFO_T *grp) if (match < 0) { char phony_dim_name[NC_MAX_NAME + 1]; - sprintf(phony_dim_name, "phony_dim_%d", grp->nc4_info->next_dimid); + snprintf(phony_dim_name, sizeof(phony_dim_name), "phony_dim_%d", grp->nc4_info->next_dimid); LOG((3, "%s: creating phony dim for var %s", __func__, var->hdr.name)); if ((retval = nc4_dim_list_add(grp, phony_dim_name, h5dimlen[d], -1, &dim))) { diff --git a/libsrc/ffio.c b/libsrc/ffio.c index 80bc6cb930..a59246e9a8 100644 --- a/libsrc/ffio.c +++ b/libsrc/ffio.c @@ -604,7 +604,7 @@ ncio_ffio_assign(const char *filename) { errno=E2BIG; return (const char *) NULL; } - (void) sprintf(buffer,"-F %s %s", envstr,xtra_assign); + (void) snprintf(buffer,sizeof(buffer),"-F %s %s", envstr,xtra_assign); #ifdef __crayx1 ASNFILE(filename, buffer, &istat, strlen(filename)+1, strlen(buffer)+1); #else diff --git a/libsrc/pstdint.h b/libsrc/pstdint.h index c0b4addb34..98936cc334 100644 --- a/libsrc/pstdint.h +++ b/libsrc/pstdint.h @@ -763,27 +763,27 @@ int main () { uintmax_t umax = UINTMAX_C(0); char str0[256], str1[256]; - sprintf (str0, "%d %x\n", 0, ~0); + snprintf (str0, sizeof(str0), "%d %x\n", 0, ~0); - sprintf (str1, "%d %x\n", i8, ~0); + snprintf (str1, sizeof(str1), "%d %x\n", i8, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with i8 : %s\n", str1); - sprintf (str1, "%u %x\n", u8, ~0); + snprintf (str1, sizeof(str1), "%u %x\n", u8, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with u8 : %s\n", str1); - sprintf (str1, "%d %x\n", i16, ~0); + snprintf (str1, sizeof(str1), "%d %x\n", i16, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with i16 : %s\n", str1); - sprintf (str1, "%u %x\n", u16, ~0); + snprintf (str1, sizeof(str1), "%u %x\n", u16, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with u16 : %s\n", str1); - sprintf (str1, "%" PRINTF_INT32_MODIFIER "d %x\n", i32, ~0); + snprintf (str1, sizeof(str1), "%" PRINTF_INT32_MODIFIER "d %x\n", i32, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with i32 : %s\n", str1); - sprintf (str1, "%" PRINTF_INT32_MODIFIER "u %x\n", u32, ~0); + snprintf (str1, sizeof(str1), "%" PRINTF_INT32_MODIFIER "u %x\n", u32, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with u32 : %s\n", str1); #ifdef INT64_MAX - sprintf (str1, "%" PRINTF_INT64_MODIFIER "d %x\n", i64, ~0); + snprintf (str1, sizeof(str1), "%" PRINTF_INT64_MODIFIER "d %x\n", i64, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with i64 : %s\n", str1); #endif - sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "d %x\n", imax, ~0); + snprintf (str1, sizeof(str1), "%" PRINTF_INTMAX_MODIFIER "d %x\n", imax, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with imax : %s\n", str1); - sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "u %x\n", umax, ~0); + snprintf (str1, sizeof(str1), "%" PRINTF_INTMAX_MODIFIER "u %x\n", umax, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with umax : %s\n", str1); TESTUMAX(8); diff --git a/libsrc4/nc4internal.c b/libsrc4/nc4internal.c index 2c6e64d1b3..fc5f62aed6 100644 --- a/libsrc4/nc4internal.c +++ b/libsrc4/nc4internal.c @@ -1742,7 +1742,7 @@ nc4_init_logging(void) } /* Create a filename with the rank in it. */ - sprintf(log_filename, "nc4_log_%d.log", my_rank); + snprintf(log_filename, sizeof(log_filename), "nc4_log_%d.log", my_rank); /* Open a file for this rank to log messages. */ if (!(LOG_FILE = fopen(log_filename, "w"))) @@ -1875,7 +1875,7 @@ rec_print_metadata(NC_GRP_INFO_T *grp, int tab_count) strcpy(dims_string, ""); for (d = 0; d < var->ndims; d++) { - sprintf(temp_string, " %d", var->dimids[d]); + snprintf(temp_string, sizeof(temp_string), " %d", var->dimids[d]); strcat(dims_string, temp_string); } } diff --git a/nc_perf/bm_file.c b/nc_perf/bm_file.c index ee04cfdd7a..925c14e009 100644 --- a/nc_perf/bm_file.c +++ b/nc_perf/bm_file.c @@ -1075,8 +1075,8 @@ main(int argc, char **argv) #endif /* Create a copy of file_out. This will defeat any buffering * that may exist from the fact that we just wrote file_out. */ - sprintf(file_out_2, "tst_copy_%s", file_out); - sprintf(cmd, "cp %s %s\n", file_out, file_out_2); + snprintf(file_out_2, sizeof(file_out_2), "tst_copy_%s", file_out); + snprintf(cmd, sizeof(cmd), "cp %s %s\n", file_out, file_out_2); system(cmd); if ((ret = cmp_file(file_in, file_out_2, &meta_read2_us, &data_read2_us, diff --git a/nc_perf/bm_many_atts.c b/nc_perf/bm_many_atts.c index 7cccf6563c..07334110c9 100644 --- a/nc_perf/bm_many_atts.c +++ b/nc_perf/bm_many_atts.c @@ -58,12 +58,12 @@ int main(int argc, char **argv) ERR; for(g = 1; g < numgrp + 1; g++) { - sprintf(gname, "group%d", g); + snprintf(gname, sizeof(gname), "group%d", g); if (nc_def_grp(ncid, gname, &grp)) ERR; natts = g < numgrp ? NC_MAX_ATTRS : aleft; /* leftovers on last time through */ for(an = 1; an < natts + 1; an++) { char aname[20]; - sprintf(aname, "attribute%d", a); + snprintf(aname, sizeof(aname), "attribute%d", a); if (nc_put_att_int(grp, NC_GLOBAL, aname, NC_INT, 1, data)) ERR; if(a%100 == 0) { /* only print every 100th attribute name */ if (gettimeofday(&end_time, NULL)) ERR; diff --git a/nc_perf/bm_many_objs.c b/nc_perf/bm_many_objs.c index defc65a143..43e4f49da9 100644 --- a/nc_perf/bm_many_objs.c +++ b/nc_perf/bm_many_objs.c @@ -52,7 +52,7 @@ int main(int argc, char **argv) /* create N groups, printing time after every 1000 */ numgrp = nitem; for(g = 1; g < numgrp + 1; g++) { - sprintf(gname, "group%d", g); + snprintf(gname, sizeof(gname), "group%d", g); if (nc_def_grp(ncid, gname, &grp)) ERR; if (nc_def_var(grp, "var", NC_INT, 0, NULL, &var)) ERR; if(nc_enddef (grp)) ERR; @@ -80,13 +80,13 @@ int main(int argc, char **argv) ERR; for(g = 1; g < numgrp + 1; g++) { - sprintf(gname, "group%d", g); + snprintf(gname, sizeof(gname), "group%d", g); if (nc_def_grp(ncid, gname, &grp)) ERR; nvars = g < numgrp ? NC_MAX_VARS : vleft; /* leftovers on last time through */ for(vn = 1; vn < nvars + 1; vn++) { int var; char vname[20]; - sprintf(vname, "variable%d", v); + snprintf(vname, sizeof(vname), "variable%d", v); if(nc_def_var(grp, vname, NC_INT, 0, NULL, &var)) ERR; if(nc_put_var(grp, var, data)) ERR; if(v%1000 == 0) { /* only print every 1000th variable name */ diff --git a/nc_perf/tst_attsperf.c b/nc_perf/tst_attsperf.c index 26a2b48440..d9911ea397 100644 --- a/nc_perf/tst_attsperf.c +++ b/nc_perf/tst_attsperf.c @@ -48,7 +48,7 @@ add_attributes(int ncid, int varid, size_t num_atts, size_t att_len) /* Write a bunch of attributes. */ for (a = 0; a < num_atts; a++) { - sprintf(att_name, "%s_varid_%d_att_%d", TEST, varid, a); + snprintf(att_name, sizeof(att_name), "%s_varid_%d_att_%d", TEST, varid, a); if (nc_put_att_double(ncid, varid, att_name, NC_DOUBLE, att_len, att_data)) ERR; } @@ -73,7 +73,7 @@ buildfile(size_t num_vars, size_t num_atts, size_t att_len, char *file_name) for (v = 0; v < num_vars; v++) { char var_name[NC_MAX_NAME + 1]; - sprintf(var_name, "%s_var_%d", TEST, v); + snprintf(var_name, sizeof(var_name), "%s_var_%d", TEST, v); if (nc_def_var(ncid, var_name, NC_INT, NDIMS, dimids, &varid)) ERR; if (add_attributes(ncid, v, num_atts, att_len)) ERR; } @@ -200,7 +200,7 @@ main(int argc, char **argv) long long hdf5_open_time = 0; /* Determine file name. */ - sprintf(file_name, "%s_%d_%d.nc", TEST, num_vars, r); + snprintf(file_name, sizeof(file_name), "%s_%d_%d.nc", TEST, num_vars, r); if (buildfile(num_vars, num_atts, ATT_LEN, file_name)) ERR; if (readfile(file_name, &nc4_open_time, do_inq, num_vars)) ERR; @@ -242,7 +242,7 @@ main(int argc, char **argv) long long hdf5_open_time; /* Determine file name. */ - sprintf(file_name, "%s_%d_%d_%d.nc", TEST, num_vars, s, r); + snprintf(file_name, sizeof(file_name), "%s_%d_%d_%d.nc", TEST, num_vars, s, r); if (buildfile(num_vars, num_atts, ATT_LEN, file_name)) ERR; if (readfile(file_name, &nc4_open_time, do_inq, num_vars)) ERR; diff --git a/nc_perf/tst_bm_rando.c b/nc_perf/tst_bm_rando.c index 05dfd216b6..825c49af20 100644 --- a/nc_perf/tst_bm_rando.c +++ b/nc_perf/tst_bm_rando.c @@ -74,7 +74,7 @@ main(int argc, char **argv) if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimid[2])) ERR; for (v = 0; v < NUM_VAR; v++) { - sprintf(name, "var_%d", v); + snprintf(name, sizeof(name), "var_%d", v); if (nc_def_var(ncid, name, NC_FLOAT, NDIM3, dimid, &varid[v])) ERR; if (nc_def_var_chunking(ncid, v, NC_CHUNKED, chunksize)) ERR; } @@ -103,8 +103,8 @@ main(int argc, char **argv) { /* Create a copy of file_out. This will defeat any OS * buffering. */ - sprintf(file_2, "tst_copy_%d_%s", c, FILE_NAME); - sprintf(cmd, "cp %s %s\n", FILE_NAME, file_2); + snprintf(file_2, sizeof(file_2), "tst_copy_%d_%s", c, FILE_NAME); + snprintf(cmd, sizeof(cmd), "cp %s %s\n", FILE_NAME, file_2); system(cmd); } else diff --git a/nc_perf/tst_chunks3.c b/nc_perf/tst_chunks3.c index 701a2c4bae..1cc9168462 100644 --- a/nc_perf/tst_chunks3.c +++ b/nc_perf/tst_chunks3.c @@ -307,7 +307,7 @@ main(int argc, char *argv[]) { count[1] = dims[1]; count[2] = dims[2]; - sprintf(time_mess," contiguous write %3d %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," contiguous write %3d %3ld %3ld", 1, dims[1], dims[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { @@ -319,7 +319,7 @@ main(int argc, char *argv[]) { printf("\n"); contig_time = TMsec; - sprintf(time_mess," chunked write %3d %3ld %3ld %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," chunked write %3d %3ld %3ld %3ld %3ld %3ld", 1, dims[1], dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { @@ -335,7 +335,7 @@ main(int argc, char *argv[]) { else printf(" %5.2g x slower\n", 1.0/ratio); - sprintf(time_mess," compressed write %3d %3ld %3ld %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," compressed write %3d %3ld %3ld %3ld %3ld %3ld", 1, dims[1], dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { @@ -360,7 +360,7 @@ main(int argc, char *argv[]) { count[1] = 1; count[2] = dims[2]; - sprintf(time_mess," contiguous write %3ld %3d %3ld", + snprintf(time_mess, sizeof(time_mess)," contiguous write %3ld %3d %3ld", dims[0], 1, dims[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { @@ -372,7 +372,7 @@ main(int argc, char *argv[]) { printf("\n"); contig_time = TMsec; - sprintf(time_mess," chunked write %3ld %3d %3ld %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," chunked write %3ld %3d %3ld %3ld %3ld %3ld", dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { @@ -388,7 +388,7 @@ main(int argc, char *argv[]) { else printf(" %5.2g x slower\n", 1.0/ratio); - sprintf(time_mess," compressed write %3ld %3d %3ld %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," compressed write %3ld %3d %3ld %3ld %3ld %3ld", dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { @@ -413,7 +413,7 @@ main(int argc, char *argv[]) { count[1] = dims[1]; count[2] = 1; - sprintf(time_mess," contiguous write %3ld %3ld %3d", + snprintf(time_mess, sizeof(time_mess)," contiguous write %3ld %3ld %3d", dims[0], dims[1], 1); TIMING_START ; for(i = 0; i < dims[2]; i++) { @@ -425,7 +425,7 @@ main(int argc, char *argv[]) { printf("\n"); contig_time = TMsec; - sprintf(time_mess," chunked write %3ld %3ld %3d %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," chunked write %3ld %3ld %3d %3ld %3ld %3ld", dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[2]; i++) { @@ -441,7 +441,7 @@ main(int argc, char *argv[]) { else printf(" %5.2g x slower\n", 1.0/ratio); - sprintf(time_mess," compressed write %3ld %3ld %3d %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," compressed write %3ld %3ld %3d %3ld %3ld %3ld", dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[2]; i++) { @@ -466,7 +466,7 @@ main(int argc, char *argv[]) { count[1] = dims[1]; count[2] = dims[2]; - sprintf(time_mess," contiguous read %3d %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," contiguous read %3d %3ld %3ld", 1, dims[1], dims[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { @@ -478,7 +478,7 @@ main(int argc, char *argv[]) { printf("\n"); contig_time = TMsec; - sprintf(time_mess," chunked read %3d %3ld %3ld %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," chunked read %3d %3ld %3ld %3ld %3ld %3ld", 1, dims[1], dims[2] , chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { @@ -494,7 +494,7 @@ main(int argc, char *argv[]) { else printf(" %5.2g x slower\n", 1.0/ratio); - sprintf(time_mess," compressed read %3d %3ld %3ld %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," compressed read %3d %3ld %3ld %3ld %3ld %3ld", 1, dims[1], dims[2] , chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { @@ -519,7 +519,7 @@ main(int argc, char *argv[]) { count[1] = 1; count[2] = dims[2]; - sprintf(time_mess," contiguous read %3ld %3d %3ld", + snprintf(time_mess, sizeof(time_mess)," contiguous read %3ld %3d %3ld", dims[0], 1, dims[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { @@ -531,7 +531,7 @@ main(int argc, char *argv[]) { printf("\n"); contig_time = TMsec; - sprintf(time_mess," chunked read %3ld %3d %3ld %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," chunked read %3ld %3d %3ld %3ld %3ld %3ld", dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { @@ -547,7 +547,7 @@ main(int argc, char *argv[]) { else printf(" %5.2g x slower\n", 1.0/ratio); - sprintf(time_mess," compressed read %3ld %3d %3ld %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," compressed read %3ld %3d %3ld %3ld %3ld %3ld", dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { @@ -572,7 +572,7 @@ main(int argc, char *argv[]) { count[1] = dims[1]; count[2] = 1; - sprintf(time_mess," contiguous read %3ld %3ld %3d", + snprintf(time_mess, sizeof(time_mess)," contiguous read %3ld %3ld %3d", dims[0], dims[1], 1); TIMING_START ; for(i = 0; i < dims[2]; i++) { @@ -584,7 +584,7 @@ main(int argc, char *argv[]) { printf("\n"); contig_time = TMsec; - sprintf(time_mess," chunked read %3ld %3ld %3d %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," chunked read %3ld %3ld %3d %3ld %3ld %3ld", dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[2]; i++) { @@ -600,7 +600,7 @@ main(int argc, char *argv[]) { else printf(" %5.2g x slower\n", 1.0/ratio); - sprintf(time_mess," compressed read %3ld %3ld %3d %3ld %3ld %3ld", + snprintf(time_mess, sizeof(time_mess)," compressed read %3ld %3ld %3d %3ld %3ld %3ld", dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[2]; i++) { diff --git a/nc_perf/tst_compress.c b/nc_perf/tst_compress.c index 1495ff4848..bd3a816cc8 100644 --- a/nc_perf/tst_compress.c +++ b/nc_perf/tst_compress.c @@ -282,7 +282,7 @@ write_meta(int ncid, int *data_varid, int s, int f, int nsd, int deflate, int u, { char data_var_name[NC_MAX_NAME + 1]; - sprintf(data_var_name, "var_%d", dv); + snprintf(data_var_name, sizeof(data_var_name), "var_%d", dv); if (nc_redef(ncid)) ERR; if (nc_def_var(ncid, data_var_name, NC_FLOAT, NDIM4, dimid_data, &data_varid[dv])) ERR; @@ -567,7 +567,7 @@ main(int argc, char **argv) /* Use the same filename every time, so we don't * create many large files, just one. ;-) */ - sprintf(file_name, "%s.nc", TEST_NAME); + snprintf(file_name, sizeof(file_name), "%s.nc", TEST_NAME); /* Remove the last file. Ignore errors. */ remove(file_name); diff --git a/nc_perf/tst_compress_par.c b/nc_perf/tst_compress_par.c index 39b769c960..c38b494b12 100644 --- a/nc_perf/tst_compress_par.c +++ b/nc_perf/tst_compress_par.c @@ -290,7 +290,7 @@ write_meta(int ncid, int *data_varid, int s, int f, int nsd, int deflate, int u, { char data_var_name[NC_MAX_NAME + 1]; - sprintf(data_var_name, "var_%d", dv); + snprintf(data_var_name, sizeof(data_var_name), "var_%d", dv); if (nc_redef(ncid)) ERR; if (nc_def_var(ncid, data_var_name, NC_FLOAT, NDIM4, dimid_data, &data_varid[dv])) ERR; @@ -641,7 +641,7 @@ main(int argc, char **argv) /* Use the same filename every time, so we don't * create many large files, just one. ;-) */ - sprintf(file_name, "%s.nc", TEST_NAME); + snprintf(file_name, sizeof(file_name), "%s.nc", TEST_NAME); /* nc_set_log_level(3); */ /* Create a parallel netcdf-4 file. */ diff --git a/nc_perf/tst_create_files.c b/nc_perf/tst_create_files.c index c3fdd87460..753b3979ac 100644 --- a/nc_perf/tst_create_files.c +++ b/nc_perf/tst_create_files.c @@ -50,7 +50,7 @@ main(int argc, char **argv) /* User TEMP_LARGE as the directory. */ if (strlen(TEMP_LARGE) + strlen(LARGE_FILE) > NC_MAX_NAME * 2) ERR; - sprintf(file_name, "%s/%s", TEMP_LARGE, LARGE_FILE); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, LARGE_FILE); /* Create file with 3 dims, one variable. */ if (nc_create(file_name, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; @@ -171,11 +171,11 @@ main(int argc, char **argv) * dimensions. */ for (ndims = 1; ndims <= MAX_DIMS; ndims++) { - sprintf(file_name, "tst_%s2_%dD.nc", type_name[t], ndims); + snprintf(file_name, sizeof(file_name), "tst_%s2_%dD.nc", type_name[t], ndims); if (nc_create(file_name, 0, &ncid)) ERR; for (len = pow(TOTAL_SIZE, (float)1/ndims), d = 0; d < ndims; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, len, &dimids[d])) ERR; } if (nc_def_var(ncid, VAR_NAME, typeid[t], ndims, dimids, &varid)) ERR; @@ -284,11 +284,11 @@ main(int argc, char **argv) * dimensions. */ for (ndims = 1; ndims <= MAX_DIMS; ndims++) { - sprintf(file_name, "tst_%s2_%dD.nc", type_name[t], ndims); + snprintf(file_name, sizeof(file_name), "tst_%s2_%dD.nc", type_name[t], ndims); if (nc_create(file_name, 0, &ncid)) ERR; for (len = pow(TOTAL_SIZE, (float)1/ndims), d = 0; d < ndims; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, len, &dimids[d])) ERR; } if (nc_def_var(ncid, SIMPLE_VAR_NAME, typeid[t], ndims, dimids, &varid)) ERR; diff --git a/nc_perf/tst_files2.c b/nc_perf/tst_files2.c index ca9c8211ac..ccc8580edc 100644 --- a/nc_perf/tst_files2.c +++ b/nc_perf/tst_files2.c @@ -41,7 +41,7 @@ get_mem_used1(int *mem_used) /* Run the ps command for this process, putting output (one number) * into file TMP_FILE_NAME. */ - sprintf(cmd, "ps -o size= %d > %s", getpid(), TMP_FILE_NAME); + snprintf(cmd, sizeof(cmd), "ps -o size= %d > %s", getpid(), TMP_FILE_NAME); system(cmd); /* Read the results and delete temp file. */ @@ -116,7 +116,7 @@ create_sample_file(char *file_name, int ndims, int *dim_len, /* Create the dimensions. */ for (d = 0; d < ndims; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR_RET; } @@ -124,7 +124,7 @@ create_sample_file(char *file_name, int ndims, int *dim_len, if (!(varids = malloc(num_vars * sizeof(int)))) ERR_RET; for (i = 0; i < num_vars; i++) { - sprintf(varname, "a_%d", i); + snprintf(varname, sizeof(varname), "a_%d", i); if (nc_def_var(ncid, varname, NC_FLOAT, ndims, dimids, &varids[i])) ERR_RET; } @@ -218,7 +218,7 @@ main(int argc, char **argv) for (f = 0; f < num_files[t]; f++) { /* Set up filename. */ - sprintf(file_name[t], "tst_files2_%d_%d.nc", t, f); + snprintf(file_name[t], sizeof(file_name[t]), "tst_files2_%d_%d.nc", t, f); if (create_sample_file(file_name[t], ndims[t], dim_len[t], num_vars[t], mode[t], num_recs[t])) ERR; @@ -273,20 +273,20 @@ main(int argc, char **argv) /* Prepare the dimensions string. */ if (ndims[t] == MAX_DIMS) - sprintf(dimstr, "%dx%dx%dx%d", dim_len[t][0], dim_len[t][1], + snprintf(dimstr, sizeof(dimstr), "%dx%dx%dx%d", dim_len[t][0], dim_len[t][1], dim_len[t][2], dim_len[t][3]); else - sprintf(dimstr, "%dx%dx%d", dim_len[t][0], dim_len[t][1], + snprintf(dimstr, sizeof(dimstr), "%dx%dx%d", dim_len[t][0], dim_len[t][1], dim_len[t][2]); /* Prepare the chunksize string. */ if (storage == NC_CHUNKED) { if (ndims[t] == MAX_DIMS) - sprintf(chunkstr, "%dx%dx%dx%d", (int)chunksize[0], (int)chunksize[1], + snprintf(chunkstr, sizeof(chunkstr), "%dx%dx%dx%d", (int)chunksize[0], (int)chunksize[1], (int)chunksize[2], (int)chunksize[3]); else - sprintf(chunkstr, "%dx%dx%d", (int)chunksize[0], (int)chunksize[1], + snprintf(chunkstr, sizeof(chunkstr), "%dx%dx%d", (int)chunksize[0], (int)chunksize[1], (int)chunksize[2]); } else diff --git a/nc_perf/tst_h_many_atts.c b/nc_perf/tst_h_many_atts.c index f484c43103..f5cd1f4129 100644 --- a/nc_perf/tst_h_many_atts.c +++ b/nc_perf/tst_h_many_atts.c @@ -58,7 +58,7 @@ main() if ((spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR; for (i = 0; i < NUM_ATTS; i++) { - sprintf(name, "att_%d", i); + snprintf(name, sizeof(name), "att_%d", i); if ((attid1 = H5Acreate2(grpid, name, H5T_NATIVE_INT, spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if (H5Awrite(attid1, H5T_NATIVE_INT, &one) < 0) ERR; diff --git a/nc_perf/tst_wrf_reads.c b/nc_perf/tst_wrf_reads.c index e38805fb28..34d998f035 100644 --- a/nc_perf/tst_wrf_reads.c +++ b/nc_perf/tst_wrf_reads.c @@ -26190,7 +26190,7 @@ main(int argc, char **argv) char file_name[NC_MAX_NAME + 1]; nc_set_log_level(4); - sprintf(file_name, "%s_wrf_chem_%d.nc", FILE_NAME, i); + snprintf(file_name, sizeof(file_name), "%s_wrf_chem_%d.nc", FILE_NAME, i); run_test(2, file_name, cmode[c], &open_time, &create_time, &close_time); remove(file_name); } @@ -26214,7 +26214,7 @@ main(int argc, char **argv) /* char file_name[NC_MAX_NAME + 1]; */ /* nc_set_log_level(4); */ - /* sprintf(file_name, "%s_%d.nc", FILE_NAME, i); */ + /* snprintf(file_name, sizeof(file_name), "%s_%d.nc", FILE_NAME, i); */ /* run_test(0, file_name, cmode[c], &open_time, &create_time, &close_time); */ /* remove(file_name); */ /* } */ @@ -26237,7 +26237,7 @@ main(int argc, char **argv) /* char file_name[NC_MAX_NAME + 1]; */ /* nc_set_log_level(4); */ - /* sprintf(file_name, "%s_%d.nc", FILE_NAME, i); */ + /* snprintf(file_name, sizeof(file_name), "%s_%d.nc", FILE_NAME, i); */ /* run_test(1, file_name, cmode[c], &open_time, &create_time, &close_time); */ /* remove(file_name); */ /* } */ diff --git a/nc_test/large_files.c b/nc_test/large_files.c index 114fa3d2fd..ec4f1f0884 100644 --- a/nc_test/large_files.c +++ b/nc_test/large_files.c @@ -72,7 +72,7 @@ main(int argc, char **argv) { printf("\n*** Testing large files, slowly.\n"); - sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE_NAME); printf("*** Creating large file %s...", file_name); /* enter define mode */ diff --git a/nc_test/quick_large_files.c b/nc_test/quick_large_files.c index 756a6faa62..7d2e385dee 100644 --- a/nc_test/quick_large_files.c +++ b/nc_test/quick_large_files.c @@ -65,7 +65,7 @@ main(int argc, char **argv) } /* Create a netCDF 64-bit offset format file. Write a value. */ - sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE_NAME); printf("*** Creating %s for 64-bit offset large file test...", file_name); { if ((res = nc_create(file_name, cflag|NC_64BIT_OFFSET, &ncid))) diff --git a/nc_test/testnc3perf.c b/nc_test/testnc3perf.c index 3f97c3f290..b4f0a8a872 100644 --- a/nc_test/testnc3perf.c +++ b/nc_test/testnc3perf.c @@ -298,7 +298,7 @@ test_slabs(ncid, sizes) edge[idim] = dims[idim].size; } - sprintf(time_mess,"ncvarput %ldx%ldx%ldx%ld", + snprintf(time_mess, sizeof(time_mess),"ncvarput %ldx%ldx%ldx%ld", edge[0], edge[1], edge[2], edge[3]); TIMING_START ; @@ -321,7 +321,7 @@ test_slabs(ncid, sizes) point[idim] = corner[idim]; } - sprintf(time_mess,"ncvarget %ldx%ldx%ldx%ld" + snprintf(time_mess, sizeof(time_mess),"ncvarget %ldx%ldx%ldx%ld" ,edge[0],edge[1],edge[2],edge[3]); TIMING_START ; @@ -343,7 +343,7 @@ test_slabs(ncid, sizes) corner[idim] = 0; /* get vector along dimension idim */ edge[idim] = dims[idim].size; - sprintf(time_mess,"ncvarget %ldx%ldx%ldx%ld" + snprintf(time_mess, sizeof(time_mess),"ncvarget %ldx%ldx%ldx%ld" ,edge[0],edge[1],edge[2],edge[3]); TIMING_START ; @@ -372,7 +372,7 @@ test_slabs(ncid, sizes) edge[idim] = dims[idim].size; edge[jdim] = dims[jdim].size; - sprintf(time_mess,"ncvarget %ldx%ldx%ldx%ld" + snprintf(time_mess, sizeof(time_mess),"ncvarget %ldx%ldx%ldx%ld" ,edge[0],edge[1],edge[2],edge[3]); TIMING_START ; @@ -411,7 +411,7 @@ test_slabs(ncid, sizes) edge[jdim] = dims[jdim].size; edge[kdim] = dims[kdim].size; - sprintf(time_mess,"ncvarget %ldx%ldx%ldx%ld" + snprintf(time_mess, sizeof(time_mess),"ncvarget %ldx%ldx%ldx%ld" ,edge[0],edge[1],edge[2],edge[3]); TIMING_START ; @@ -447,7 +447,7 @@ test_slabs(ncid, sizes) edge[idim] = dims[idim].size; } - sprintf(time_mess,"ncvarget %ldx%ldx%ldx%ld" + snprintf(time_mess, sizeof(time_mess),"ncvarget %ldx%ldx%ldx%ld" ,edge[0],edge[1],edge[2],edge[3]); TIMING_START ; diff --git a/nc_test/tst_addvar.c b/nc_test/tst_addvar.c index bde8bba25a..01d6100b3d 100644 --- a/nc_test/tst_addvar.c +++ b/nc_test/tst_addvar.c @@ -74,7 +74,7 @@ int main(int argc, char** argv) { for (i=0; i<3; i++) { char varname[32]; - sprintf(varname, "fixed_var_%d",2*i+1); + snprintf(varname, sizeof(varname), "fixed_var_%d",2*i+1); for (j=0; j<5; j++) { if (get_buf[i][j] != old_buf[i][j]) { printf("Error in %s line %d: expecting %s[%d]=%d but got %d\n", diff --git a/nc_test/tst_big_rvar.c b/nc_test/tst_big_rvar.c index dd885120a3..5df0079f73 100644 --- a/nc_test/tst_big_rvar.c +++ b/nc_test/tst_big_rvar.c @@ -112,7 +112,7 @@ main(int argc, char **argv) { char testfile[NC_MAX_NAME + 1]; printf("\n*** Testing files with multidimensional variable with more than 2**32 values\n"); - sprintf(testfile, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(testfile, sizeof(testfile), "%s/%s", TEMP_LARGE, FILE_NAME); for (i = NC_FORMAT_CLASSIC; i <= NUM_FORMATS; i++) { printf("*** testing format %d file with record variable with > 2**32 values...", i); diff --git a/nc_test/tst_big_var.c b/nc_test/tst_big_var.c index 98dc8dfc68..d5734ab07c 100644 --- a/nc_test/tst_big_var.c +++ b/nc_test/tst_big_var.c @@ -70,7 +70,7 @@ main(int argc, char **argv) { char testfile[NC_MAX_NAME + 1]; printf("\n*** Testing files with one very big variable.\n"); - sprintf(testfile, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(testfile, sizeof(testfile), "%s/%s", TEMP_LARGE, FILE_NAME); for (i = NC_FORMAT_CLASSIC; i <= NUM_FORMATS; i++) { printf("*** testing format %d file with byte variable with > 2**32 values...", i); diff --git a/nc_test/tst_big_var2.c b/nc_test/tst_big_var2.c index f3c2cf3160..78c36bbfba 100644 --- a/nc_test/tst_big_var2.c +++ b/nc_test/tst_big_var2.c @@ -121,7 +121,7 @@ main(int argc, char **argv) { char testfile[NC_MAX_NAME + 1]; printf("\n*** Testing multidimensional variable with more than 2**32 values\n"); - sprintf(testfile, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(testfile, sizeof(testfile), "%s/%s", TEMP_LARGE, FILE_NAME); for (i = NC_FORMAT_CLASSIC; i <= NUM_FORMATS; i++) { printf("*** testing format %d file with byte variable with > 2**32 values...", i); diff --git a/nc_test/tst_big_var6.c b/nc_test/tst_big_var6.c index b237a50317..ad85ede919 100644 --- a/nc_test/tst_big_var6.c +++ b/nc_test/tst_big_var6.c @@ -102,7 +102,7 @@ main(int argc, char **argv) { char testfile[NC_MAX_NAME + 1]; printf("\n*** Testing multidimensional variable with more than 2**32 values\n"); - sprintf(testfile, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(testfile, sizeof(testfile), "%s/%s", TEMP_LARGE, FILE_NAME); for (i = NC_FORMAT_CLASSIC; i <= NUM_FORMATS; i++) { printf("*** testing format %d file with short variable with > 2**32 values...", i); diff --git a/nc_test/tst_cdf5format.c b/nc_test/tst_cdf5format.c index f5162ab782..68ebf9306a 100644 --- a/nc_test/tst_cdf5format.c +++ b/nc_test/tst_cdf5format.c @@ -35,12 +35,12 @@ write2(int ncid, int parallel) { if (i % 2) { - sprintf(str, "fixed_var_%d",i); + snprintf(str, sizeof(str), "fixed_var_%d",i); if (nc_def_var(ncid, str, NC_INT, 1, &dimid[1], &varid[i])) ERR; } else { - sprintf(str, "record_var_%d",i); + snprintf(str, sizeof(str), "record_var_%d",i); if (nc_def_var(ncid, str, NC_INT, 2, dimid, &varid[i])) ERR; } } @@ -86,7 +86,7 @@ extend(int ncid) /* add attributes to make header grow */ for (i = 0; i < NVARS; i++) { - sprintf(str, "annotation_for_var_%d", i); + snprintf(str, sizeof(str), "annotation_for_var_%d", i); if (nc_put_att_text(ncid, i, "text_attr", strlen(str), str)) ERR; } if (nc_enddef(ncid)) ERR; diff --git a/nc_test/tst_diskless3.c b/nc_test/tst_diskless3.c index 2c428f33c4..31765fa958 100644 --- a/nc_test/tst_diskless3.c +++ b/nc_test/tst_diskless3.c @@ -133,7 +133,7 @@ test_two_growing_with_att(const char *testfile) { count[0] = 1; start[0] = r; - sprintf(att_name, "a_%d", data[r]); + snprintf(att_name, sizeof(att_name), "a_%d", data[r]); for (v = 0; v < NUM_VARS; v++) { if((status=nc_put_vara_text(ncid, varid[v], start, count, &data[r]))) ERRSTAT(status); diff --git a/nc_test/tst_diskless4.c b/nc_test/tst_diskless4.c index be667fe745..8519f87b95 100644 --- a/nc_test/tst_diskless4.c +++ b/nc_test/tst_diskless4.c @@ -124,7 +124,7 @@ main(int argc, char **argv) for(iv=0;iv 0) { /* system error */ const char *cp = (const char *) strerror(err); if (cp == NULL) - sprintf(unknown_str,"Unknown error code %d",err); + snprintf(unknown_str,sizeof(unknown_str),"Unknown error code %d",err); else - sprintf(unknown_str,"Error code %d (%s)",err,cp); + snprintf(unknown_str,sizeof(unknown_str),"Error code %d (%s)",err,cp); return unknown_str; } @@ -1396,7 +1396,7 @@ char* nc_err_code_name(int err) #endif #endif default: - sprintf(unknown_str,"Unknown code %d",err); + snprintf(unknown_str,sizeof(unknown_str),"Unknown code %d",err); } return unknown_str; } diff --git a/nc_test4/tst_atts2.c b/nc_test4/tst_atts2.c index eb3201511d..700e34ff65 100644 --- a/nc_test4/tst_atts2.c +++ b/nc_test4/tst_atts2.c @@ -133,7 +133,7 @@ main(int argc, char **argv) if (nc_create(FILE_NAME1, NC_NETCDF4, &ncid1)) ERR; for (a = 0; a < NUM_ATT; a++) { - sprintf(name[a], "atomic_att_type_%d", a + 1); + snprintf(name[a], sizeof(name[a]), "atomic_att_type_%d", a + 1); nc_put_att(ncid1, NC_GLOBAL, name[a], a + 1, ATT_LEN, (void *)&data); } diff --git a/nc_test4/tst_chunks.c b/nc_test4/tst_chunks.c index 1391f1eab8..280c644023 100644 --- a/nc_test4/tst_chunks.c +++ b/nc_test4/tst_chunks.c @@ -90,7 +90,7 @@ main(int argc, char **argv) if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; for (d = 0; d < NUM_DIM; d++) { - sprintf(dim_name, "dim_%d", dim_len[d]); + snprintf(dim_name, sizeof(dim_name), "dim_%d", dim_len[d]); #ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE printf("creating dim %s\n", dim_name); #endif @@ -99,7 +99,7 @@ main(int argc, char **argv) for (t = 0; t < NUM_TYPE; t++) { - sprintf(var_name, "var_%d", type_id[t]); + snprintf(var_name, sizeof(var_name), "var_%d", type_id[t]); if (nc_def_var(ncid, var_name, type_id[t], NUM_DIM, dimid, &varid[t])) ERR; if (nc_inq_var_chunking(ncid, varid[t], &contig, chunksize_in)) ERR; #ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE @@ -120,7 +120,7 @@ main(int argc, char **argv) for (t = 0; t < NUM_TYPE; t++) { - sprintf(var_name, "var_%d", type_id[t]); + snprintf(var_name, sizeof(var_name), "var_%d", type_id[t]); if (nc_inq_var_chunking(ncid, varid[t], &contig, chunksize_in)) ERR; if (contig) ERR; #ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE @@ -216,7 +216,7 @@ main(int argc, char **argv) /* Oh that tricky Cardinal Richelieu, he had many plans! */ for (i = 0; i < NUM_PLANS; i++) { - sprintf(plan_name, "Richelieu_sneaky_plan_%d", i); + snprintf(plan_name, sizeof(plan_name), "Richelieu_sneaky_plan_%d", i); if (nc_def_var(ncid, plan_name, i % (NC_STRING - 1) + 1, NDIMS_3, dimids, &varid[i])) ERR; if (i % 2 && nc_def_var_chunking(ncid, varid[i], 0, chunksize)) ERR; diff --git a/nc_test4/tst_chunks2.c b/nc_test4/tst_chunks2.c index 0499d6f392..3970ff243e 100644 --- a/nc_test4/tst_chunks2.c +++ b/nc_test4/tst_chunks2.c @@ -161,7 +161,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -199,7 +199,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -237,7 +237,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -278,7 +278,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -318,7 +318,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -360,7 +360,7 @@ main(int argc, char **argv) for (d = 0; d < NDIMS3; d++) { dim_len[d] = rand(); - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -399,7 +399,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -438,7 +438,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } diff --git a/nc_test4/tst_compounds.c b/nc_test4/tst_compounds.c index 731ed8bc4f..a2c6da7de8 100644 --- a/nc_test4/tst_compounds.c +++ b/nc_test4/tst_compounds.c @@ -545,7 +545,7 @@ main(int argc, char **argv) /* hr data */ hr_data_out[i].starfleet_id = i; hr_data_out[i].svc_rec = data[i]; - if (sprintf(hr_data_out[i].name, "alien_%d", i) < 0) ERR; + if (snprintf(hr_data_out[i].name, sizeof(hr_data_out[i].name), "alien_%d", i) < 0) ERR; hr_data_out[i].max_temp = 99.99f; hr_data_out[i].min_temp = -9.99f; hr_data_out[i].percent_transporter_errosion = .030303; @@ -862,7 +862,7 @@ main(int argc, char **argv) /* Create some phony data. */ for (i = 0; i < DIM1_LEN; i++) { - if (sprintf(hr_data_out[i].name, "alien_%d", i) < 0) ERR; + if (snprintf(hr_data_out[i].name, sizeof(hr_data_out[i].name), "alien_%d", i) < 0) ERR; hr_data_out[i].max_temp = 99.99f; } diff --git a/nc_test4/tst_compounds3.c b/nc_test4/tst_compounds3.c index d388658bbb..ce4a495d4f 100644 --- a/nc_test4/tst_compounds3.c +++ b/nc_test4/tst_compounds3.c @@ -173,11 +173,11 @@ main(int argc, char **argv) if (strcmp(name_in, TYPE2_NAME) || size_in != sizeof(g2_d_t)) ERR; /* This fails because it's not a fully-qualified name. */ - sprintf(full_name, "%s/%s", GROUP2_NAME, TYPE2_NAME); + snprintf(full_name, sizeof(full_name), "%s/%s", GROUP2_NAME, TYPE2_NAME); if (nc_inq_typeid(root_grp, full_name, &type2id) != NC_EINVAL) ERR; /* Check the type using it's full name. */ - sprintf(full_name, "/%s/%s", GROUP2_NAME, TYPE2_NAME); + snprintf(full_name, sizeof(full_name), "/%s/%s", GROUP2_NAME, TYPE2_NAME); /* if (nc_inq_typeid(root_grp, full_name, &type2id)) ERR; */ /* if (nc_inq_type(g2_grp, type2id, name_in, &size_in)) ERR; */ /* if (strcmp(name_in, TYPE2_NAME) || size_in != sizeof(g2_d_t)) ERR; */ diff --git a/nc_test4/tst_dims2.c b/nc_test4/tst_dims2.c index 5237d29c72..0af77d72bf 100644 --- a/nc_test4/tst_dims2.c +++ b/nc_test4/tst_dims2.c @@ -433,7 +433,7 @@ main(int argc, char **argv) { char dim_name[NC_MAX_NAME + 1]; - sprintf(dim_name, "dim_%d", i); + snprintf(dim_name, sizeof(dim_name), "dim_%d", i); if (nc_def_dim(ncid, dim_name, 1, &dimids[i])) ERR; } diff --git a/nc_test4/tst_files.c b/nc_test4/tst_files.c index f2ad31bedd..2a8417166a 100644 --- a/nc_test4/tst_files.c +++ b/nc_test4/tst_files.c @@ -329,7 +329,7 @@ main(int argc, char **argv) /* Create a bunch of files. */ for (f = 0; f < NUM_FILES; f++) { - sprintf(file_name, "tst_files2_%d.nc", f); + snprintf(file_name, sizeof(file_name), "tst_files2_%d.nc", f); if (nc_create(file_name, NC_NETCDF4, &ncid[f])) ERR; if (nc_def_dim(ncid[f], D1_NAME, TEXT_LEN + 1, &dimid)) ERR; if (nc_def_var(ncid[f], VAR_NAME, NC_CHAR, NDIMS, &dimid, &varid)) ERR; diff --git a/nc_test4/tst_grps.c b/nc_test4/tst_grps.c index 9224c96df7..30f60de15c 100644 --- a/nc_test4/tst_grps.c +++ b/nc_test4/tst_grps.c @@ -1186,11 +1186,11 @@ main(int argc, char **argv) if (nc_create(FILE_NAME, NC_CLOBBER | NC_NETCDF4, &ncid)) ERR; for (g = 0; g < PARENT_NUM_GRPS; g++) { - sprintf(grp_name, "grp_%d", g); + snprintf(grp_name, sizeof(grp_name), "grp_%d", g); if (nc_def_grp(ncid, grp_name, &g1id)) ERR; for (s = 0; s < SUB_NUM_GRPS; s++) { - sprintf(grp_name, "sub_grp_%d", s); + snprintf(grp_name, sizeof(grp_name), "sub_grp_%d", s); if (nc_def_grp(g1id, grp_name, &sub_grpid)) ERR; } } diff --git a/nc_test4/tst_interops4.c b/nc_test4/tst_interops4.c index 535a80e4fe..22fa429514 100644 --- a/nc_test4/tst_interops4.c +++ b/nc_test4/tst_interops4.c @@ -29,7 +29,7 @@ write_atts(int ncid, int varid) for (a = 0; a < NUM_ATTS; a++) { - sprintf(att_name, "att_%d", a); + snprintf(att_name, sizeof(att_name), "att_%d", a); if (nc_put_att_int(ncid, varid, att_name, NC_INT, ATT_LEN, att_data)) ERR_RET; } @@ -45,7 +45,7 @@ read_atts(int ncid, int varid) for (a = 0; a < NUM_ATTS; a++) { - sprintf(att_name, "att_%d", a); + snprintf(att_name, sizeof(att_name), "att_%d", a); if (nc_get_att_int(ncid, varid, att_name, att_data_in)) ERR_RET; for (i = 0; i < ATT_LEN; i++) @@ -67,7 +67,7 @@ write_vars(int ncid) if (nc_def_dim(ncid, DIM_NAME, VAR_LEN, NULL)) ERR; for (v = 0; v < NUM_VARS; v++) { - sprintf(var_name, "var_%d", v); + snprintf(var_name, sizeof(var_name), "var_%d", v); if (nc_def_var(ncid, var_name, NC_INT, NUM_DIMS, dimid, NULL)) ERR_RET; write_atts(ncid, v); @@ -89,7 +89,7 @@ read_vars(int ncid) { if (nc_inq_var(ncid, v, var_name_in, &xtype_in, &ndims_in, NULL, &natts_in)) ERR_RET; - sprintf(var_name, "var_%d", v); + snprintf(var_name, sizeof(var_name), "var_%d", v); if (strcmp(var_name, var_name_in) || xtype_in != NC_INT || ndims_in != NUM_DIMS || natts_in != NUM_ATTS) ERR_RET; read_atts(ncid, v); diff --git a/nc_test4/tst_interops5.c b/nc_test4/tst_interops5.c index d696eb09a7..39ec195202 100644 --- a/nc_test4/tst_interops5.c +++ b/nc_test4/tst_interops5.c @@ -99,11 +99,11 @@ main(int argc, char **argv) if (H5Sclose(ydimSpaceId) < 0) ERR; /* Create xdim scale */ - sprintf(dimNameBuf, "%s%10d", dimNameBase, nrowCur); + snprintf(dimNameBuf, sizeof(dimNameBuf), "%s%10d", dimNameBase, nrowCur); if (H5DSset_scale(xdimId, dimNameBuf) < 0) ERR; /* Create ydim scale */ - sprintf(dimNameBuf, "%s%10d", dimNameBase, ncolCur); + snprintf(dimNameBuf, sizeof(dimNameBuf), "%s%10d", dimNameBase, ncolCur); if (H5DSset_scale(ydimId, dimNameBuf) < 0) ERR; /* Attach dimension scales to the dataset */ diff --git a/nc_test4/tst_large2.c b/nc_test4/tst_large2.c index 55fea5ec9b..2c0c849c24 100644 --- a/nc_test4/tst_large2.c +++ b/nc_test4/tst_large2.c @@ -50,7 +50,7 @@ int main(int argc, char **argv) for (f = 0; f < NUM_FORMATS; f++) { printf("\t...testing with %s\n", format_name[f]); - sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE_NAME); if (nc_create(file_name, this_format[f], &ncid)) ERR; if (nc_def_dim(ncid, "lat", LAT_LEN, &dimids[1])) ERR; if (nc_def_dim(ncid, "lon", LON_LEN, &dimids[2])) ERR; diff --git a/nc_test4/tst_large3.c b/nc_test4/tst_large3.c index 296f68d329..5bea1478a5 100644 --- a/nc_test4/tst_large3.c +++ b/nc_test4/tst_large3.c @@ -59,7 +59,7 @@ main(int argc, char **argv) int i; /* Create a netCDF netCDF-4/HDF5 format file, with 4 vars. */ - sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE_NAME); if (nc_create(file_name, NC_NETCDF4, &ncid)) ERR; if (nc_set_fill(ncid, NC_NOFILL, NULL)) ERR; if (nc_def_dim(ncid, DIM_NAME, QTR_CLASSIC_MAX, dimids)) ERR; diff --git a/nc_test4/tst_large5.c b/nc_test4/tst_large5.c index 2b3eaf8248..20ff0447c7 100644 --- a/nc_test4/tst_large5.c +++ b/nc_test4/tst_large5.c @@ -92,7 +92,7 @@ main(int argc, char **argv) for (f = 0; f < NUM_FORMATS; f++) { printf("\t...testing with %s\n", format_name[f]); - sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE_NAME); if (nc_create(file_name, this_format[f], &ncid)) ERR; if (nc_def_dim(ncid, "lat", LAT_LEN, &dimids[1])) ERR; if (nc_def_dim(ncid, "lon", LON_LEN, &dimids[2])) ERR; diff --git a/nc_test4/tst_nc4perf.c b/nc_test4/tst_nc4perf.c index af39e20943..ded16c908a 100644 --- a/nc_test4/tst_nc4perf.c +++ b/nc_test4/tst_nc4perf.c @@ -53,7 +53,7 @@ int test_pio_2d(size_t cache_size, int access_flag, MPI_Comm comm, data[j * DIMSIZE1 / mpi_size + i] = (float)mpi_rank * (j + 1); /* Get the file name. */ - sprintf(file_name, "%s/%s", TEMP_LARGE, FILENAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILENAME); /* Set the cache size. */ if (nc_get_chunk_cache(NULL, &nelems_in, &preemption_in)) ERR; @@ -114,7 +114,7 @@ int test_pio_2d(size_t cache_size, int access_flag, MPI_Comm comm, /* What was our chunking? */ if (chunk_size[0]) - sprintf(chunk_string, "%dx%d ", (int)chunk_size[0], (int)chunk_size[1]); + snprintf(chunk_string, sizeof(chunk_string), "%dx%d ", (int)chunk_size[0], (int)chunk_size[1]); else strcat(chunk_string, "contiguous"); @@ -162,7 +162,7 @@ int test_pio_4d(size_t cache_size, int access_flag, MPI_Comm comm, } /* Get the file name. */ - sprintf(file_name, "%s/%s", TEMP_LARGE, FILENAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILENAME); /* Set the cache size. */ if (nc_get_chunk_cache(NULL, &nelems_in, &preemption_in)) ERR; @@ -226,7 +226,7 @@ int test_pio_4d(size_t cache_size, int access_flag, MPI_Comm comm, /* What was our chunking? */ if (chunk_size[0]) - sprintf(chunk_string, "%dx%dx%dx%d", (int)chunk_size[0], (int)chunk_size[1], + snprintf(chunk_string, sizeof(chunk_string), "%dx%dx%dx%d", (int)chunk_size[0], (int)chunk_size[1], (int)chunk_size[2], (int)chunk_size[3]); else strcat(chunk_string, "contiguous"); diff --git a/nc_test4/tst_parallel.c b/nc_test4/tst_parallel.c index ced8b70512..25cf4a55f1 100644 --- a/nc_test4/tst_parallel.c +++ b/nc_test4/tst_parallel.c @@ -92,7 +92,7 @@ main(int argc, char **argv) /* Create a parallel netcdf-4 file. */ /*nc_set_log_level(3);*/ - sprintf(file_name, "%s/%s", TEMP_LARGE, FILE); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE); if ((res = nc_create_par(file_name, NC_NETCDF4, comm, info, &ncid))) ERR; diff --git a/nc_test4/tst_parallel3.c b/nc_test4/tst_parallel3.c index 17f5e0bde5..4d711bc9be 100644 --- a/nc_test4/tst_parallel3.c +++ b/nc_test4/tst_parallel3.c @@ -83,7 +83,7 @@ int main(int argc, char **argv) facc_type_open = 0; /* Create file name. */ - sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE_NAME); /* Test NetCDF4 with MPI-IO driver */ if (mpi_rank == 0) diff --git a/nc_test4/tst_parallel4.c b/nc_test4/tst_parallel4.c index b2e8df2645..7ea3c78d45 100644 --- a/nc_test4/tst_parallel4.c +++ b/nc_test4/tst_parallel4.c @@ -106,7 +106,7 @@ main(int argc, char **argv) #endif /* USE_MPE */ /* Create a parallel netcdf-4 file. */ - sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE_NAME); if (nc_create_par(file_name, NC_NETCDF4, comm, info, &ncid)) ERR; /* A global attribute holds the number of processors that created diff --git a/nc_test4/tst_parallel_zlib.c b/nc_test4/tst_parallel_zlib.c index 6e99234e88..f60a7e0679 100644 --- a/nc_test4/tst_parallel_zlib.c +++ b/nc_test4/tst_parallel_zlib.c @@ -103,8 +103,8 @@ main(int argc, char **argv) /* Create a parallel netcdf-4 file. */ /*nc_set_log_level(3);*/ - /* sprintf(file_name, "%s/%s", TEMP_LARGE, FILE); */ - sprintf(file_name, "%s", FILE); + /* snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILE); */ + snprintf(file_name, sizeof(file_name), "%s", FILE); if ((res = nc_create_par(file_name, NC_NETCDF4, comm, info, &ncid))) ERR; /* Create three dimensions. */ diff --git a/nc_test4/tst_quantize.c b/nc_test4/tst_quantize.c index eb43c30437..c2694e7a9f 100644 --- a/nc_test4/tst_quantize.c +++ b/nc_test4/tst_quantize.c @@ -54,7 +54,7 @@ pf(float myf) uint32_t u; } fu; fu.f = myf; - sprintf(pf_str, "0x%x", fu.u); + snprintf(pf_str, sizeof(pf_str), "0x%x", fu.u); return pf_str; } @@ -67,7 +67,7 @@ pd(double myd) uint64_t u; } du; du.d = myd; - sprintf(pf_str, "0x%llx", (unsigned long long)du.u); + snprintf(pf_str, sizeof(pf_str), "0x%llx", (unsigned long long)du.u); return pf_str; } @@ -239,7 +239,7 @@ main(int argc, char **argv) printf("\t\t**** testing quantize algorithm %d...\n", quantize_mode[q]); for (t = 0; t < NTYPES; t++) { - sprintf(file_name, "%s_quantize_%d_type_%d.nc", TEST, quantize_mode[q], xtype[t]); + snprintf(file_name, sizeof(file_name), "%s_quantize_%d_type_%d.nc", TEST, quantize_mode[q], xtype[t]); #ifdef TESTNCZARR { char url[NC_MAX_FILENAME + 1]; diff --git a/nc_test4/tst_quantize_par.c b/nc_test4/tst_quantize_par.c index 9aad7c0a9b..efc0d87c85 100644 --- a/nc_test4/tst_quantize_par.c +++ b/nc_test4/tst_quantize_par.c @@ -50,7 +50,7 @@ pf(float myf) uint32_t u; } fu; fu.f = myf; - sprintf(pf_str, "0x%x", fu.u); + snprintf(pf_str, sizeof(pf_str), "0x%x", fu.u); return pf_str; } @@ -63,7 +63,7 @@ pd(double myd) uint64_t u; } du; du.d = myd; - sprintf(pf_str, "0x%lx", du.u); + snprintf(pf_str, sizeof(pf_str), "0x%lx", du.u); return pf_str; } diff --git a/nc_test4/tst_rename2.c b/nc_test4/tst_rename2.c index a86f7ffb1a..c366363fa4 100644 --- a/nc_test4/tst_rename2.c +++ b/nc_test4/tst_rename2.c @@ -62,7 +62,7 @@ main(int argc, char **argv) for (enddef_setting = 0; enddef_setting < NUM_ENDDEF_SETTINGS; enddef_setting++) { - sprintf(filename, "%s_%d_%d.nc", TEST_NAME, formats[format], + snprintf(filename, sizeof(filename), "%s_%d_%d.nc", TEST_NAME, formats[format], enddef_setting); /* Create file with three dims. */ @@ -110,7 +110,7 @@ main(int argc, char **argv) if (nc_set_default_format(formats[format], NULL)) ERR; - sprintf(filename, "%s_data_%d.nc", TEST_NAME, formats[format]); + snprintf(filename, sizeof(filename), "%s_data_%d.nc", TEST_NAME, formats[format]); /* Create file with three dims. */ if (nc_create(filename, 0, &ncid)) ERR; @@ -168,7 +168,7 @@ main(int argc, char **argv) char name[NC_MAX_NAME + 1]; /* Create file with dim and associated coordinate var. */ - sprintf(file_name, "%s_sync.nc", TEST_NAME); + snprintf(file_name, sizeof(file_name), "%s_sync.nc", TEST_NAME); if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, DIM_NAME_END, DIM1_LEN, &dimid)) ERR; if (nc_def_var(ncid, DIM_NAME_END, NC_INT, NDIM1, &dimid, &varid)) ERR; @@ -201,7 +201,7 @@ main(int argc, char **argv) char name[NC_MAX_NAME + 1]; /* Create file with dim and associated coordinate var. */ - sprintf(file_name, "%s_sync.nc", TEST_NAME); + snprintf(file_name, sizeof(file_name), "%s_sync.nc", TEST_NAME); if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, DIM_NAME_START, DIM1_LEN, &dimid)) ERR; if (nc_def_var(ncid, VAR_NAME_START, NC_INT, NDIM1, &dimid, &varid)) ERR; @@ -244,7 +244,7 @@ main(int argc, char **argv) char file_name[NC_MAX_NAME + 1]; /* Create file with dim and associated coordinate var. */ - sprintf(file_name, "%s_non_coord_to_dim.nc", TEST_NAME); + snprintf(file_name, sizeof(file_name), "%s_non_coord_to_dim.nc", TEST_NAME); if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, D1_NAME, DIM1_LEN, &dimid1)) ERR; if (nc_def_dim(ncid, D2_NAME, DIM1_LEN, &dimid2)) ERR; @@ -280,7 +280,7 @@ main(int argc, char **argv) char file_name[NC_MAX_NAME + 1]; /* Create file with two scalar vars. */ - sprintf(file_name, "%s_rename_affect_varid_order.nc", TEST_NAME); + snprintf(file_name, sizeof(file_name), "%s_rename_affect_varid_order.nc", TEST_NAME); if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_var(ncid, D1_NAME, NC_INT, 0, NULL, &varid1)) ERR; if (nc_def_var(ncid, D2_NAME, NC_INT, 0, NULL, &varid2)) ERR; diff --git a/nc_test4/tst_rename3.c b/nc_test4/tst_rename3.c index 6f8c95ae67..e17af66344 100644 --- a/nc_test4/tst_rename3.c +++ b/nc_test4/tst_rename3.c @@ -39,7 +39,7 @@ main(int argc, char **argv) char file_name[NC_MAX_NAME + 1]; /* Create file with two scalar vars. */ - sprintf(file_name, "%s_coord_to_non_coord.nc", TEST_NAME); + snprintf(file_name, sizeof(file_name), "%s_coord_to_non_coord.nc", TEST_NAME); if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_var(ncid, D1_NAME, NC_INT, 0, NULL, &varid1)) ERR; if (nc_def_var(ncid, D2_NAME, NC_INT, 0, NULL, &varid2)) ERR; @@ -67,7 +67,7 @@ main(int argc, char **argv) char file_name[NC_MAX_NAME + 1]; /* Create file with two dims and associated coordinate vars. */ - sprintf(file_name, "%s_coord_to_non_coord.nc", TEST_NAME); + snprintf(file_name, sizeof(file_name), "%s_coord_to_non_coord.nc", TEST_NAME); if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, D1_NAME, DIM1_LEN, &dimid1)) ERR; if (nc_def_dim(ncid, D2_NAME, DIM1_LEN, &dimid2)) ERR; @@ -121,7 +121,7 @@ main(int argc, char **argv) char file_name[NC_MAX_NAME + 1]; /* Create file with dim and associated coordinate var. */ - sprintf(file_name, "%s_non_coord_to_dim.nc", TEST_NAME); + snprintf(file_name, sizeof(file_name), "%s_non_coord_to_dim.nc", TEST_NAME); if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, D1_NAME, DIM1_LEN, &dimid1)) ERR; if (nc_def_dim(ncid, D2_NAME, DIM1_LEN, &dimid2)) ERR; diff --git a/nc_test4/tst_simplerw_coll_r.c b/nc_test4/tst_simplerw_coll_r.c index bf6637df69..b91d2a383d 100644 --- a/nc_test4/tst_simplerw_coll_r.c +++ b/nc_test4/tst_simplerw_coll_r.c @@ -236,7 +236,7 @@ main(int argc, char **argv) } /* Create a file name. */ - sprintf(file_name, "%s_type_%d_fv_%d.nc", TEST_NAME, test_type[tt], fv); + snprintf(file_name, sizeof(file_name), "%s_type_%d_fv_%d.nc", TEST_NAME, test_type[tt], fv); /* Create a parallel netcdf-4 file. */ if (nc_create_par(file_name, NC_NETCDF4, comm, info, &ncid)) ERR; diff --git a/nc_test4/tst_strings.c b/nc_test4/tst_strings.c index 301ca6cd79..ed78351a55 100644 --- a/nc_test4/tst_strings.c +++ b/nc_test4/tst_strings.c @@ -440,7 +440,7 @@ main(int argc, char **argv) char **string_fillp = dim_combo == 3 ? my_string_fill : &default_fill; char *data_in; - sprintf(filename, "%s_dim_combo_%d.nc", TEST_NAME, dim_combo); + snprintf(filename, sizeof(filename), "%s_dim_combo_%d.nc", TEST_NAME, dim_combo); if (nc_create(filename, NC_NETCDF4, &ncid)) ERR; /* Create an array of strings for the Universal Declaraion of Human Rights. */ diff --git a/nc_test4/tst_types.c b/nc_test4/tst_types.c index d4d82c30f6..f565dda33e 100644 --- a/nc_test4/tst_types.c +++ b/nc_test4/tst_types.c @@ -49,7 +49,7 @@ create_test_file(char *filename, int *varid, int *ncid) for (type = 0; type < NUM_TYPES; type++) { /* Create a var... */ - sprintf(varname, "var_%d", type); + snprintf(varname, sizeof(varname), "var_%d", type); if (nc_def_var(*ncid, varname, type + NC_UBYTE, 1, &dimid, &varid[type])) ERR; } return 0; @@ -101,7 +101,7 @@ int main(int argc, char *argv[]) for (type = 0; type < NUM_TYPES; type++) { /* Create a var... */ - sprintf(varname, "var_%d", type); + snprintf(varname, sizeof(varname), "var_%d", type); if (nc_def_var(ncid1, varname, type + NC_UBYTE, 1, &dimid, &varid[type])) ERR; if (nc_inq_type_equal(ncid1, type + NC_UBYTE, ncid2, type + NC_UBYTE, &equal)) ERR; if (!equal) ERR; diff --git a/nc_test4/tst_vars.c b/nc_test4/tst_vars.c index f94fc0dcbb..0124291e94 100644 --- a/nc_test4/tst_vars.c +++ b/nc_test4/tst_vars.c @@ -1279,7 +1279,7 @@ main(int argc, char **argv) /* Create a large number of variables. */ for (i = 0; i < NUM_VARS; i++) { - sprintf(varname, "a_%d", i); + snprintf(varname, sizeof(varname), "a_%d", i); if (nc_def_var(ncid, varname, NC_FLOAT, 1, dimids, &varids[i])) { ERR; break; diff --git a/nc_test4/tst_vars3.c b/nc_test4/tst_vars3.c index 19d51de13a..f0420bccda 100644 --- a/nc_test4/tst_vars3.c +++ b/nc_test4/tst_vars3.c @@ -314,7 +314,7 @@ main(int argc, char **argv) if (nc_def_dim(ncid, D0_NAME3, NC_UNLIMITED, &dimid)) ERR; for (v = 0; v < NUM_VARS; v++) { - sprintf(var_name, "var_%d", v); + snprintf(var_name, sizeof(var_name), "var_%d", v); if (nc_def_var(ncid, var_name, NC_INT, 1, &dimid, &varid)) ERR_RET; if (nc_set_var_chunk_cache(ncid, varid, 0, 0, 0.75)) ERR_RET; } diff --git a/ncdump/ncvalidator.c b/ncdump/ncvalidator.c index f3661269cb..971629085d 100644 --- a/ncdump/ncvalidator.c +++ b/ncdump/ncvalidator.c @@ -815,7 +815,7 @@ compute_var_shape(NC *ncp) ncp->recsize = 0; for (i=0; ivars.ndefined; i++) { - sprintf(xloc,"var %s:",ncp->vars.value[i]->name); + snprintf(xloc,sizeof(xloc),"var %s:",ncp->vars.value[i]->name); /* check if dimids are valid */ for (j=0; jvars.value[i]->ndims; j++) { if (ncp->vars.value[i]->dimids[j] < 0) { @@ -1464,7 +1464,7 @@ val_get_NC_attr(int fd, return status; } - sprintf(xloc,"%s \"%s\"",loc,name); + snprintf(xloc,sizeof(xloc),"%s \"%s\"",loc,name); err = val_get_nc_type(fd, gbp, &xtype, xloc); if (err != NC_NOERR) { if (name != NULL) free(name); @@ -1575,7 +1575,7 @@ val_get_NC_attrarray(int fd, } #endif } else { - sprintf(xloc, "%s attribute", loc); + snprintf(xloc, sizeof(xloc), "%s attribute", loc); if (tag != NC_ATTRIBUTE) { if (verbose) printf("Error @ [0x%8.8zx]:\n", tag_err_addr); if (verbose) printf("\t%s: Invalid NC component tag (%d), expecting NC_ATTRIBUTE (%d)\n",xloc,tag,NC_ATTRIBUTE); @@ -1698,7 +1698,7 @@ val_get_NC_var(int fd, if (trace) printf("\t\tname = \"%s\"\n", name); /* read number of dimensions */ - sprintf(xloc,"%s \"%s\"",loc,name); + snprintf(xloc,sizeof(xloc),"%s \"%s\"",loc,name); err_addr = ERR_ADDR; err = hdr_get_NON_NEG(fd, gbp, &ndims); if (err != NC_NOERR) { @@ -1760,7 +1760,7 @@ val_get_NC_var(int fd, /* var = name nelems [dimid ...] vatt_list nc_type vsize begin * ^^^^^^^^^ */ - sprintf(xloc,"%s \"%s\"",loc,name); + snprintf(xloc,sizeof(xloc),"%s \"%s\"",loc,name); err = val_get_NC_attrarray(fd, gbp, &varp->attrs, xloc); if (err != NC_NOERR && err != NC_ENULLPAD) { free_NC_var(varp); diff --git a/ncdump/ocprint.c b/ncdump/ocprint.c index 1e25f2daf6..7c0abc47d9 100755 --- a/ncdump/ocprint.c +++ b/ncdump/ocprint.c @@ -1033,7 +1033,7 @@ dumpdatanode(OClink link, OCdatanode datanode, size_t count, void* memory, NCbyt ncbytescat(path,id); if(entry->rank > 0) { for(i=0;irank;i++) { - sprintf(tmp,"[%lu]",(unsigned long)entry->indices[i]); + snprintf(tmp,sizeof(tmp),"[%lu]",(unsigned long)entry->indices[i]); ncbytescat(path,tmp); } } @@ -1047,7 +1047,7 @@ dumpdatanode(OClink link, OCdatanode datanode, size_t count, void* memory, NCbyt case OC_Sequence: ncbytescat(path,"/"); ncbytescat(path,id); - sprintf(tmp,"[%lu]",(unsigned long)entry->indices[0]); + snprintf(tmp,sizeof(tmp),"[%lu]",(unsigned long)entry->indices[0]); ncbytescat(path,tmp); break; diff --git a/ncgen/cmldata.c b/ncgen/cmldata.c index f2034b495d..07931d0dc9 100644 --- a/ncgen/cmldata.c +++ b/ncgen/cmldata.c @@ -288,37 +288,37 @@ xconst(Constant* ci) return result; } break; case NC_BYTE: - sprintf(tmp,"%hhd",ci->value.int8v); + snprintf(tmp,sizeof(tmp),"%hhd",ci->value.int8v); break; case NC_SHORT: - sprintf(tmp,"%hd",ci->value.int16v); + snprintf(tmp,sizeof(tmp),"%hd",ci->value.int16v); break; case NC_INT: - sprintf(tmp,"%d",ci->value.int32v); + snprintf(tmp,sizeof(tmp),"%d",ci->value.int32v); break; case NC_FLOAT: - sprintf(tmp,"%.8g",ci->value.floatv); + snprintf(tmp,sizeof(tmp),"%.8g",ci->value.floatv); break; case NC_DOUBLE: - sprintf(tmp,"%.16g",ci->value.doublev); + snprintf(tmp,sizeof(tmp),"%.16g",ci->value.doublev); break; case NC_UBYTE: - sprintf(tmp,"%hhu",ci->value.uint8v); + snprintf(tmp,sizeof(tmp),"%hhu",ci->value.uint8v); break; case NC_USHORT: - sprintf(tmp,"%hu",ci->value.uint16v); + snprintf(tmp,sizeof(tmp),"%hu",ci->value.uint16v); break; case NC_UINT: - sprintf(tmp,"%uU",ci->value.uint32v); + snprintf(tmp,sizeof(tmp),"%uU",ci->value.uint32v); break; case NC_INT64: - sprintf(tmp,"%lldLL",ci->value.int64v); + snprintf(tmp,sizeof(tmp),"%lldLL",ci->value.int64v); break; case NC_UINT64: - sprintf(tmp,"%lluLLU",ci->value.uint64v); + snprintf(tmp,sizeof(tmp),"%lluLLU",ci->value.uint64v); break; case NC_ECONST: - sprintf(tmp,"%s",cname(ci->value.enumv)); + snprintf(tmp,sizeof(tmp),"%s",cname(ci->value.enumv)); break; case NC_STRING: { diff --git a/ncgen/cvt.c b/ncgen/cvt.c index 8224ea6ad7..29995d82f1 100644 --- a/ncgen/cvt.c +++ b/ncgen/cvt.c @@ -459,57 +459,57 @@ case CASE(NC_STRING,NC_STRING): /* What is the proper conversion for T->STRING?*/ case CASE(NC_CHAR,NC_STRING): - sprintf(stmp,"%c",src->value.charv); + snprintf(stmp, sizeof(stmp),"%c",src->value.charv); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; case CASE(NC_BYTE,NC_STRING): - sprintf(stmp,"%hhd",src->value.uint8v); + snprintf(stmp, sizeof(stmp),"%hhd",src->value.uint8v); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; case CASE(NC_UBYTE,NC_STRING): - sprintf(stmp,"%hhu",src->value.uint8v); + snprintf(stmp, sizeof(stmp),"%hhu",src->value.uint8v); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; case CASE(NC_USHORT,NC_STRING): - sprintf(stmp,"%hu",src->value.uint16v); + snprintf(stmp, sizeof(stmp),"%hu",src->value.uint16v); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; case CASE(NC_UINT,NC_STRING): - sprintf(stmp,"%u",src->value.uint32v); + snprintf(stmp, sizeof(stmp),"%u",src->value.uint32v); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; case CASE(NC_UINT64,NC_STRING): - sprintf(stmp,"%llu",src->value.uint64v); + snprintf(stmp, sizeof(stmp),"%llu",src->value.uint64v); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; case CASE(NC_SHORT,NC_STRING): - sprintf(stmp,"%hd",src->value.int16v); + snprintf(stmp, sizeof(stmp),"%hd",src->value.int16v); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; case CASE(NC_INT,NC_STRING): - sprintf(stmp,"%d",src->value.int32v); + snprintf(stmp, sizeof(stmp),"%d",src->value.int32v); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; case CASE(NC_INT64,NC_STRING): - sprintf(stmp,"%lld",src->value.int64v); + snprintf(stmp, sizeof(stmp),"%lld",src->value.int64v); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; case CASE(NC_FLOAT,NC_STRING): - sprintf(stmp,"%.8g",src->value.floatv); + snprintf(stmp, sizeof(stmp),"%.8g",src->value.floatv); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; case CASE(NC_DOUBLE,NC_STRING): - sprintf(stmp,"%.8g",src->value.doublev); + snprintf(stmp, sizeof(stmp),"%.8g",src->value.doublev); tmp.stringv.len = nulllen(stmp); tmp.stringv.stringv = nulldup(stmp); break; diff --git a/ncgen/dump.c b/ncgen/dump.c index bd4d775b86..024f92c41d 100644 --- a/ncgen/dump.c +++ b/ncgen/dump.c @@ -83,7 +83,7 @@ bufdump(Datalist* list, Bytebuffer* buf) dumpdataprim(dp,buf); } else { char tmp[64]; - sprintf(tmp,"?%d? ",dp->nctype); + snprintf(tmp,sizeof(tmp),"?%d? ",dp->nctype); bbCat(buf,tmp); } break; } @@ -104,47 +104,47 @@ dumpdataprim(NCConstant* ci, Bytebuffer* buf) bbCat(buf,"'"); } break; case NC_BYTE: - sprintf(tmp,"%hhd",ci->value.int8v); + snprintf(tmp,sizeof(tmp),"%hhd",ci->value.int8v); bbCat(buf,tmp); break; case NC_SHORT: - sprintf(tmp,"%hd",ci->value.int16v); + snprintf(tmp,sizeof(tmp),"%hd",ci->value.int16v); bbCat(buf,tmp); break; case NC_INT: - sprintf(tmp,"%d",ci->value.int32v); + snprintf(tmp,sizeof(tmp),"%d",ci->value.int32v); bbCat(buf,tmp); break; case NC_FLOAT: - sprintf(tmp,"%g",ci->value.floatv); + snprintf(tmp,sizeof(tmp),"%g",ci->value.floatv); bbCat(buf,tmp); break; case NC_DOUBLE: - sprintf(tmp,"%lg",ci->value.doublev); + snprintf(tmp,sizeof(tmp),"%lg",ci->value.doublev); bbCat(buf,tmp); break; case NC_UBYTE: - sprintf(tmp,"%hhu",ci->value.int8v); + snprintf(tmp,sizeof(tmp),"%hhu",ci->value.int8v); bbCat(buf,tmp); break; case NC_USHORT: - sprintf(tmp,"%hu",ci->value.uint16v); + snprintf(tmp,sizeof(tmp),"%hu",ci->value.uint16v); bbCat(buf,tmp); break; case NC_UINT: - sprintf(tmp,"%u",ci->value.uint32v); + snprintf(tmp,sizeof(tmp),"%u",ci->value.uint32v); bbCat(buf,tmp); break; case NC_INT64: - sprintf(tmp,"%lld",ci->value.int64v); + snprintf(tmp,sizeof(tmp),"%lld",ci->value.int64v); bbCat(buf,tmp); break; case NC_UINT64: - sprintf(tmp,"%llu",ci->value.uint64v); + snprintf(tmp,sizeof(tmp),"%llu",ci->value.uint64v); bbCat(buf,tmp); break; case NC_ECONST: - sprintf(tmp,"%s",ci->value.enumv->fqn); + snprintf(tmp,sizeof(tmp),"%s",ci->value.enumv->fqn); bbCat(buf,tmp); break; case NC_STRING: diff --git a/ncgen/f77data.c b/ncgen/f77data.c index e4863af2ac..28521890a3 100644 --- a/ncgen/f77data.c +++ b/ncgen/f77data.c @@ -35,26 +35,26 @@ f77_constant(Generator* generator, Symbol* sym, NCConstant* ci, Bytebuffer* code case NC_CHAR: if(ci->value.charv == '\'') - sprintf(tmp,"'\\''"); + snprintf(tmp,sizeof(tmp),"'\\''"); else - sprintf(tmp,"'%c'",ci->value.charv); + snprintf(tmp,sizeof(tmp),"'%c'",ci->value.charv); break; case NC_BYTE: - sprintf(tmp,"%hhd",ci->value.int8v); + snprintf(tmp,sizeof(tmp),"%hhd",ci->value.int8v); break; case NC_SHORT: - sprintf(tmp,"%hd",ci->value.int16v); + snprintf(tmp,sizeof(tmp),"%hd",ci->value.int16v); break; case NC_INT: - sprintf(tmp,"%d",ci->value.int32v); + snprintf(tmp,sizeof(tmp),"%d",ci->value.int32v); break; case NC_FLOAT: - sprintf(tmp,"%.8g",ci->value.floatv); + snprintf(tmp,sizeof(tmp),"%.8g",ci->value.floatv); break; case NC_DOUBLE: { char* p = tmp; /* FORTRAN requires e|E->D */ - sprintf(tmp,"%.16g",ci->value.doublev); + snprintf(tmp,sizeof(tmp),"%.16g",ci->value.doublev); while(*p) {if(*p == 'e' || *p == 'E') {*p = 'D';}; p++;} } break; case NC_STRING: diff --git a/ncgen/gencml.c b/ncgen/gencml.c index b88b080175..912cc49751 100644 --- a/ncgen/gencml.c +++ b/ncgen/gencml.c @@ -316,7 +316,7 @@ definextype(Symbol* tsym) if(j > 0) bbCat(dimbuf," "); dim = efield->typ.dimset.dimsyms[j]; ASSERT(dim->dim.isconstant); - sprintf(tmp,"%lu",(unsigned long)dim->dim.size); + snprintf(tmp,sizeof(tmp),"%lu",(unsigned long)dim->dim.size); bbCat(dimbuf,tmp); } bbCat(dimbuf,"\""); diff --git a/ncgen/genjjni.c b/ncgen/genjjni.c index 86c8682b4b..03e0166f7c 100644 --- a/ncgen/genjjni.c +++ b/ncgen/genjjni.c @@ -838,7 +838,7 @@ genjjni_deftype(Symbol* tsym) char tmp[256]; Symbol* e = efield->typ.dimset.dimsyms[j]; ASSERT(e->dim.isconstant); - sprintf(tmp,"%u",e->dim.size); + snprintf(tmp,sizeof(tmp),"%u",e->dim.size); strcat(stmt,(j==0?"":", ")); strcat(stmt,tmp); } @@ -849,7 +849,7 @@ genjjni_deftype(Symbol* tsym) Symbol* efield = (Symbol*)listget(tsym->subnodes,i); char tmp[1024]; ASSERT(efield->subclass == NC_FIELD); - sprintf(tmp,"%lu",efield->typ.offset); + snprintf(tmp,sizeof(tmp),"%lu",efield->typ.offset); if(efield->typ.dimset.ndims > 0){ nprintf(stmt,sizeof(stmt),"check_err(nc_insert_array_compound(%s, %s, \"%s\", %s, %s, %d, %s_dims));", jgroupncid(tsym->container), @@ -1449,39 +1449,39 @@ jconst(Constant* ci) } break; case NC_BYTE: - sprintf(tmp,"%hhd",ci->value.int8v); + snprintf(tmp,sizeof(tmp),"%hhd",ci->value.int8v); break; case NC_SHORT: - sprintf(tmp,"%hd",ci->value.int16v); + snprintf(tmp,sizeof(tmp),"%hd",ci->value.int16v); break; case NC_INT: - sprintf(tmp,"%d",ci->value.int32v); + snprintf(tmp,sizeof(tmp),"%d",ci->value.int32v); break; case NC_FLOAT: - sprintf(tmp,"%.8g",ci->value.floatv); + snprintf(tmp,sizeof(tmp),"%.8g",ci->value.floatv); break; case NC_DOUBLE: - sprintf(tmp,"%.16g",ci->value.doublev); + snprintf(tmp,sizeof(tmp),"%.16g",ci->value.doublev); break; case NC_UBYTE: - sprintf(tmp,"%hhu",ci->value.uint8v); + snprintf(tmp,sizeof(tmp),"%hhu",ci->value.uint8v); break; case NC_USHORT: - sprintf(tmp,"%hu",ci->value.uint16v); + snprintf(tmp,sizeof(tmp),"%hu",ci->value.uint16v); break; case NC_UINT: - sprintf(tmp,"%uL",ci->value.uint32v); /* upgrade to long */ + snprintf(tmp,sizeof(tmp),"%uL",ci->value.uint32v); /* upgrade to long */ break; case NC_INT64: - sprintf(tmp,"%lldL",ci->value.int64v); + snprintf(tmp,sizeof(tmp),"%lldL",ci->value.int64v); break; case NC_UINT64: { /* HACK to handle unsigned values */ long long l = (long)ci->value.uint64v; - sprintf(tmp,"%lldL",l); + snprintf(tmp,sizeof(tmp),"%lldL",l); } break; case NC_ECONST: - sprintf(tmp,"%s",jname(ci->value.enumv)); + snprintf(tmp,sizeof(tmp),"%s",jname(ci->value.enumv)); break; case NC_STRING: { diff --git a/ncgen/jdatajni.c b/ncgen/jdatajni.c index f5edb8d7e0..9faf6ae4f1 100644 --- a/ncgen/jdatajni.c +++ b/ncgen/jdatajni.c @@ -244,13 +244,13 @@ genjjni_data(Symbol* tsym, Datasrc* datasrc, Datalist* fillsrc, semerror(srcline(datasrc),"Vlen data must be enclosed in {..}"); } cp = srcnext(datasrc); - sprintf(vlenname,"vlen_%u",cp->value.compoundv->vlen.uid); + snprintf(vlenname,sizeof(vlenname),"vlen_%u",cp->value.compoundv->vlen.uid); /* Use special alignment */ nprintf(stmt,sizeof(stmt),"Memory.align(%s,%d);\n", vlenname,jvlenalignment()); bbCat(databuf,stmt); /* generate and store the nc_vlen_t instance*/ - sprintf(stmt,"Memory.put_vlen(%s,new long[]{%lld, %s_addr});\n", + snprintf(stmt,sizeof(stmt),"Memory.put_vlen(%s,new long[]{%lld, %s_addr});\n", memname, (long long)cp->value.compoundv->vlen.count, vlenname); @@ -336,7 +336,7 @@ genjjni_vlendata(List* vlenconstants, Bytebuffer* databuf) Symbol* basetype = tsym->typ.basetype; int typecode = basetype->typ.typecode; - sprintf(memname,"vlen_%u",cmpd->value.compoundv->vlen.uid); + snprintf(memname,sizeof(memname),"vlen_%u",cmpd->value.compoundv->vlen.uid); count = 0; vlensrc = datalist2src(cmpd->value.compoundv); @@ -362,7 +362,7 @@ genjjni_vlendata(List* vlenconstants, Bytebuffer* databuf) bbCat(databuf,bbContents(vlenbuf)); } /* Now store the address */ - sprintf(tmp,"%s_addr = Memory.address(%s);\n", + snprintf(tmp,sizeof(tmp),"%s_addr = Memory.address(%s);\n", memname,memname); bbCat(databuf,tmp); } @@ -380,11 +380,11 @@ genjjni_vlenconstants(List* vlenconstants, Bytebuffer* databuf) for(i=0;ivalue.compoundv->vlen.uid); + snprintf(memname,sizeof(memname),"vlen_%u",cmpd->value.compoundv->vlen.uid); /* Define the memory buffer and a place to stick its address */ - sprintf(tmp,"static long %s = Memory.create();\n",memname); + snprintf(tmp,sizeof(tmp),"static long %s = Memory.create();\n",memname); bbCat(databuf,tmp); - sprintf(tmp,"static long %s_addr = 0L;\n",memname); + snprintf(tmp,sizeof(tmp),"static long %s_addr = 0L;\n",memname); bbCat(databuf,tmp); } } diff --git a/ncgen/ncgen.l b/ncgen/ncgen.l index c46fb38634..963ecefcca 100644 --- a/ncgen/ncgen.l +++ b/ncgen/ncgen.l @@ -243,7 +243,7 @@ yytext[MAXTRST-1] = '\0'; */ len = unescape((char *)yytext+1,yyleng-2,!ISIDENT,&s); if(len < 0) { - sprintf(errstr,"Illegal character: %s",yytext); + snprintf(errstr, sizeof(errstr),"Illegal character: %s",yytext); yyerror(errstr); } bbClear(lextext); @@ -411,7 +411,7 @@ NIL|nil|Nil { /* capture the tag string */ tag = collecttag(pos,&stag); if(tag == NC_NAT) { - sprintf(errstr,"Illegal integer suffix: %s",stag); + snprintf(errstr, sizeof(errstr),"Illegal integer suffix: %s",stag); yyerror(errstr); goto done; } @@ -433,13 +433,13 @@ NIL|nil|Nil { radix = 10; if(isneg && hasU) { - sprintf(errstr,"Unsigned integer cannot be signed: %s",ncgtext); + snprintf(errstr, sizeof(errstr),"Unsigned integer cannot be signed: %s",ncgtext); yyerror(errstr); goto done; } uint64_val = parseULL(radix, pos,&fail); if(fail) { - sprintf(errstr,"integer constant out of range: %s",ncgtext); + snprintf(errstr, sizeof(errstr),"integer constant out of range: %s",ncgtext); yyerror(errstr); goto done; } @@ -453,7 +453,7 @@ NIL|nil|Nil { case NC_FORMAT_64BIT_OFFSET: case NC_FORMAT_NETCDF4_CLASSIC: if(nct > NC_INT) { - sprintf(errstr,"Illegal integer constant for classic format: %s",ncgtext); + snprintf(errstr, sizeof(errstr),"Illegal integer constant for classic format: %s",ncgtext); yyerror(errstr); goto done; } @@ -479,7 +479,7 @@ done: return 0; /* capture the tag string */ tag = collecttag(yytext,&stag); if(tag == NC_NAT) { - sprintf(errstr,"Illegal integer suffix: %s",stag); + snprintf(errstr, sizeof(errstr),"Illegal integer suffix: %s",stag); yyerror(errstr); //goto done; return 0; @@ -507,7 +507,7 @@ done: return 0; break; default: /* should never happen */ if (sscanf((char*)yytext, "%i", &uint32_val) != 1) { - sprintf(errstr,"bad unsigned int constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad unsigned int constant: %s",(char*)yytext); yyerror(errstr); } token = UINT_CONST; @@ -516,14 +516,14 @@ done: return 0; } {DBLNUMBER} { if (sscanf((char*)yytext, "%le", &double_val) != 1) { - sprintf(errstr,"bad long or double constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad long or double constant: %s",(char*)yytext); yyerror(errstr); } return lexdebug(DOUBLE_CONST); } {FLTNUMBER} { if (sscanf((char*)yytext, "%e", &float_val) != 1) { - sprintf(errstr,"bad float constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad float constant: %s",(char*)yytext); yyerror(errstr); } return lexdebug(FLOAT_CONST); @@ -535,7 +535,7 @@ done: return 0; \'\\[0-7][0-7][0-7]\' { int oct = unescapeoct(&yytext[2]); if(oct < 0) { - sprintf(errstr,"bad octal character constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad octal character constant: %s",(char*)yytext); yyerror(errstr); } byte_val = (unsigned int)oct; @@ -544,7 +544,7 @@ done: return 0; \'\\[xX][0-9a-fA-F][0-9a-fA-F]\' { int hex = unescapehex(&yytext[3]); if(byte_val < 0) { - sprintf(errstr,"bad hex character constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad hex character constant: %s",(char*)yytext); yyerror(errstr); } byte_val = (unsigned int)hex; @@ -647,7 +647,7 @@ makepath(char* text0) refsym = lookupingroup(NC_GRP,ident,container); if(!lastident) { if(refsym == NULL) { - sprintf(errstr,"Undefined or forward referenced group: %s",ident); + snprintf(errstr, sizeof(errstr),"Undefined or forward referenced group: %s",ident); yyerror(errstr); refsym = rootgroup; } else diff --git a/ncgen/ncgen.y b/ncgen/ncgen.y index 10f3e36ea9..c8c2d5ac62 100644 --- a/ncgen/ncgen.y +++ b/ncgen/ncgen.y @@ -650,7 +650,7 @@ fielddim: { /* Anonymous integer dimension. Can only occur in type definitions*/ char anon[32]; - sprintf(anon,"const%u",uint32_val); + snprintf(anon, sizeof(anon),"const%u",uint32_val); $$ = install(anon); $$->objectclass = NC_DIM; $$->dim.isconstant = 1; @@ -664,7 +664,7 @@ fielddim: derror("field dimension must be positive"); YYABORT; } - sprintf(anon,"const%d",int32_val); + snprintf(anon, sizeof(anon),"const%d",int32_val); $$ = install(anon); $$->objectclass = NC_DIM; $$->dim.isconstant = 1; diff --git a/ncgen/ncgenl.c b/ncgen/ncgenl.c index 6ea3a2270e..af03296e16 100644 --- a/ncgen/ncgenl.c +++ b/ncgen/ncgenl.c @@ -2081,7 +2081,7 @@ yytext[MAXTRST-1] = '\0'; */ len = unescape((char *)yytext+1,yyleng-2,!ISIDENT,&s); if(len < 0) { - sprintf(errstr,"Illegal character: %s",yytext); + snprintf(errstr,sizeof(errstr),"Illegal character: %s",yytext); yyerror(errstr); } bbClear(lextext); @@ -2385,7 +2385,7 @@ YY_RULE_SETUP /* capture the tag string */ tag = collecttag(pos,&stag); if(tag == NC_NAT) { - sprintf(errstr,"Illegal integer suffix: %s",stag); + snprintf(errstr,sizeof(errstr),"Illegal integer suffix: %s",stag); yyerror(errstr); goto done; } @@ -2407,13 +2407,13 @@ YY_RULE_SETUP radix = 10; if(isneg && hasU) { - sprintf(errstr,"Unsigned integer cannot be signed: %s",ncgtext); + snprintf(errstr,sizeof(errstr),"Unsigned integer cannot be signed: %s",ncgtext); yyerror(errstr); goto done; } uint64_val = parseULL(radix, pos,&fail); if(fail) { - sprintf(errstr,"integer constant out of range: %s",ncgtext); + snprintf(errstr,sizeof(errstr),"integer constant out of range: %s",ncgtext); yyerror(errstr); goto done; } @@ -2427,7 +2427,7 @@ YY_RULE_SETUP case NC_FORMAT_64BIT_OFFSET: case NC_FORMAT_NETCDF4_CLASSIC: if(nct > NC_INT) { - sprintf(errstr,"Illegal integer constant for classic format: %s",ncgtext); + snprintf(errstr,sizeof(errstr),"Illegal integer constant for classic format: %s",ncgtext); yyerror(errstr); goto done; } @@ -2456,7 +2456,7 @@ YY_RULE_SETUP /* capture the tag string */ tag = collecttag(yytext,&stag); if(tag == NC_NAT) { - sprintf(errstr,"Illegal integer suffix: %s",stag); + snprintf(errstr,sizeof(errstr),"Illegal integer suffix: %s",stag); yyerror(errstr); //goto done; return 0; @@ -2484,7 +2484,7 @@ YY_RULE_SETUP break; default: /* should never happen */ if (sscanf((char*)yytext, "%i", &uint32_val) != 1) { - sprintf(errstr,"bad unsigned int constant: %s",(char*)yytext); + snprintf(errstr,sizeof(errstr),"bad unsigned int constant: %s",(char*)yytext); yyerror(errstr); } token = UINT_CONST; @@ -2497,7 +2497,7 @@ YY_RULE_SETUP #line 517 "ncgen.l" { if (sscanf((char*)yytext, "%le", &double_val) != 1) { - sprintf(errstr,"bad long or double constant: %s",(char*)yytext); + snprintf(errstr,sizeof(errstr),"bad long or double constant: %s",(char*)yytext); yyerror(errstr); } return lexdebug(DOUBLE_CONST); @@ -2508,7 +2508,7 @@ YY_RULE_SETUP #line 524 "ncgen.l" { if (sscanf((char*)yytext, "%e", &float_val) != 1) { - sprintf(errstr,"bad float constant: %s",(char*)yytext); + snprintf(errstr,sizeof(errstr),"bad float constant: %s",(char*)yytext); yyerror(errstr); } return lexdebug(FLOAT_CONST); @@ -2529,7 +2529,7 @@ YY_RULE_SETUP { int oct = unescapeoct(&yytext[2]); if(oct < 0) { - sprintf(errstr,"bad octal character constant: %s",(char*)yytext); + snprintf(errstr,sizeof(errstr),"bad octal character constant: %s",(char*)yytext); yyerror(errstr); } byte_val = (unsigned int)oct; @@ -2542,7 +2542,7 @@ YY_RULE_SETUP { int hex = unescapehex(&yytext[3]); if(byte_val < 0) { - sprintf(errstr,"bad hex character constant: %s",(char*)yytext); + snprintf(errstr,sizeof(errstr),"bad hex character constant: %s",(char*)yytext); yyerror(errstr); } byte_val = (unsigned int)hex; @@ -3681,7 +3681,7 @@ makepath(char* text0) refsym = lookupingroup(NC_GRP,ident,container); if(!lastident) { if(refsym == NULL) { - sprintf(errstr,"Undefined or forward referenced group: %s",ident); + snprintf(errstr,sizeof(errstr),"Undefined or forward referenced group: %s",ident); yyerror(errstr); refsym = rootgroup; } else diff --git a/ncgen/ncgeny.c b/ncgen/ncgeny.c index 105a221b4c..ae2c0a81e6 100644 --- a/ncgen/ncgeny.c +++ b/ncgen/ncgeny.c @@ -1543,7 +1543,7 @@ yysyntax_error (YYPTRDIFF_T *yymsg_alloc, char **yymsg, return -1; } - /* Avoid sprintf, as that infringes on the user's name space. + /* Avoid snprintf, as that infringes on the user's name space. Don't have undefined behavior even if the translation produced a string with the wrong number of "%s"s. */ { @@ -2409,7 +2409,7 @@ fprintf(stderr,"dimension: %s = UNLIMITED\n",(yyvsp[-2].sym)->name); { /* Anonymous integer dimension. Can only occur in type definitions*/ char anon[32]; - sprintf(anon,"const%u",uint32_val); + snprintf(anon,sizeof(anon),"const%u",uint32_val); (yyval.sym) = install(anon); (yyval.sym)->objectclass = NC_DIM; (yyval.sym)->dim.isconstant = 1; @@ -2427,7 +2427,7 @@ fprintf(stderr,"dimension: %s = UNLIMITED\n",(yyvsp[-2].sym)->name); derror("field dimension must be positive"); YYABORT; } - sprintf(anon,"const%d",int32_val); + snprintf(anon,sizeof(anon),"const%d",int32_val); (yyval.sym) = install(anon); (yyval.sym)->objectclass = NC_DIM; (yyval.sym)->dim.isconstant = 1; diff --git a/ncgen/odom.c b/ncgen/odom.c index 1f96828d65..93e9a34ec0 100644 --- a/ncgen/odom.c +++ b/ncgen/odom.c @@ -70,7 +70,7 @@ odometerprint(Odometer* odom) strcat(line,"[]"); } else for(i=0;irank;i++) { int ioffset = i + odom->offset; - sprintf(tmp,"[%lu/%lu..%lu:%lu]", + snprintf(tmp,sizeof(tmp),"[%lu/%lu..%lu:%lu]", (unsigned long)odom->origin->index[ioffset], (unsigned long)odom->origin->start[ioffset], (unsigned long)odom->origin->declsize[ioffset], diff --git a/ncgen3/genlib.c b/ncgen3/genlib.c index 1e728502ec..ead98b08df 100644 --- a/ncgen3/genlib.c +++ b/ncgen3/genlib.c @@ -225,7 +225,7 @@ gen_c( cline("}"); cline(""); cline("int"); - sprintf(stmnt, "main() {\t\t\t/* create %s */", filename); + snprintf(stmnt, sizeof(stmnt), "main() {\t\t\t/* create %s */", filename); cline(stmnt); /* create necessary declarations */ @@ -237,7 +237,7 @@ gen_c( cline(""); cline(" /* dimension ids */"); for (idim = 0; idim < ndims; idim++) { - sprintf(stmnt, " int %s_dim;", dims[idim].lname); + snprintf(stmnt, sizeof(stmnt), " int %s_dim;", dims[idim].lname); cline(stmnt); } @@ -245,10 +245,10 @@ gen_c( cline(" /* dimension lengths */"); for (idim = 0; idim < ndims; idim++) { if (dims[idim].size == NC_UNLIMITED) { - sprintf(stmnt, " size_t %s_len = NC_UNLIMITED;", + snprintf(stmnt, sizeof(stmnt), " size_t %s_len = NC_UNLIMITED;", dims[idim].lname); } else { - sprintf(stmnt, " size_t %s_len = %lu;", + snprintf(stmnt, sizeof(stmnt), " size_t %s_len = %lu;", dims[idim].lname, (unsigned long) dims[idim].size); } @@ -265,14 +265,14 @@ gen_c( cline(""); cline(" /* variable ids */"); for (ivar = 0; ivar < nvars; ivar++) { - sprintf(stmnt, " int %s_id;", vars[ivar].lname); + snprintf(stmnt, sizeof(stmnt), " int %s_id;", vars[ivar].lname); cline(stmnt); } cline(""); cline(" /* rank (number of dimensions) for each variable */"); for (ivar = 0; ivar < nvars; ivar++) { - sprintf(stmnt, "# define RANK_%s %d", vars[ivar].lname, + snprintf(stmnt, sizeof(stmnt), "# define RANK_%s %d", vars[ivar].lname, vars[ivar].ndims); cline(stmnt); } @@ -281,7 +281,7 @@ gen_c( cline(" /* variable shapes */"); for (ivar = 0; ivar < nvars; ivar++) { if (vars[ivar].ndims > 0) { - sprintf(stmnt, " int %s_dims[RANK_%s];", + snprintf(stmnt, sizeof(stmnt), " int %s_dims[RANK_%s];", vars[ivar].lname, vars[ivar].lname); cline(stmnt); } @@ -302,7 +302,7 @@ gen_c( cline(" /* attribute vectors */"); for (iatt = 0; iatt < natts; iatt++) { if (atts[iatt].type != NC_CHAR) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " %s %s_%s[%lu];", ncatype(atts[iatt].type), atts[iatt].var == -1 ? "cdf" : vars[atts[iatt].var].lname, @@ -318,20 +318,20 @@ gen_c( cline(" /* enter define mode */"); if (!cmode_modifier) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_create(\"%s\", NC_CLOBBER, &ncid);", filename); } else if (cmode_modifier & NC_64BIT_OFFSET) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_create(\"%s\", NC_CLOBBER|NC_64BIT_OFFSET, &ncid);", filename); #ifdef USE_NETCDF4 } else if (cmode_modifier & NC_CLASSIC_MODEL) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_create(\"%s\", NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid);", filename); } else if (cmode_modifier & NC_NETCDF4) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_create(\"%s\", NC_CLOBBER|NC_NETCDF4, &ncid);", filename); #endif @@ -347,7 +347,7 @@ gen_c( cline(" /* define dimensions */"); } for (idim = 0; idim < ndims; idim++) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_def_dim(ncid, \"%s\", %s_len, &%s_dim);", dims[idim].name, dims[idim].lname, dims[idim].lname); cline(stmnt); @@ -361,7 +361,7 @@ gen_c( for (ivar = 0; ivar < nvars; ivar++) { cline(""); for (idim = 0; idim < vars[ivar].ndims; idim++) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " %s_dims[%d] = %s_dim;", vars[ivar].lname, idim, @@ -369,7 +369,7 @@ gen_c( cline(stmnt); } if (vars[ivar].ndims > 0) { /* a dimensioned variable */ - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_def_var(ncid, \"%s\", %s, RANK_%s, %s_dims, &%s_id);", vars[ivar].name, nctype(vars[ivar].type), @@ -377,7 +377,7 @@ gen_c( vars[ivar].lname, vars[ivar].lname); } else { /* a scalar */ - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_def_var(ncid, \"%s\", %s, RANK_%s, 0, &%s_id);", vars[ivar].name, nctype(vars[ivar].type), @@ -396,7 +396,7 @@ gen_c( for (iatt = 0; iatt < natts; iatt++) { if (atts[iatt].type == NC_CHAR) { /* string */ val_string = cstrstr((char *) atts[iatt].val, atts[iatt].len); - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_put_att_text(ncid, %s%s, \"%s\", %lu, %s);", atts[iatt].var == -1 ? "NC_GLOBAL" : vars[atts[iatt].var].lname, atts[iatt].var == -1 ? "" : "_id", @@ -409,7 +409,7 @@ gen_c( else { /* vector attribute */ for (jatt = 0; jatt < atts[iatt].len ; jatt++) { val_string = cstring(atts[iatt].type,atts[iatt].val,jatt); - sprintf(stmnt, " %s_%s[%d] = %s;", + snprintf(stmnt, sizeof(stmnt), " %s_%s[%d] = %s;", atts[iatt].var == -1 ? "cdf" : vars[atts[iatt].var].lname, atts[iatt].lname, jatt, @@ -418,7 +418,7 @@ gen_c( free (val_string); } - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_put_att_%s(ncid, %s%s, \"%s\", %s, %lu, %s_%s);", ncatype(atts[iatt].type), atts[iatt].var == -1 ? "NC_GLOBAL" : vars[atts[iatt].var].lname, @@ -610,21 +610,21 @@ gen_fortran( if (ndims > 0) { fline("* dimension ids"); for (idim = 0; idim < ndims; idim++) { - sprintf(stmnt, "integer %s_dim", dims[idim].lname); + snprintf(stmnt, sizeof(stmnt), "integer %s_dim", dims[idim].lname); fline(stmnt); } fline("* dimension lengths"); for (idim = 0; idim < ndims; idim++) { - sprintf(stmnt, "integer %s_len", dims[idim].lname); + snprintf(stmnt, sizeof(stmnt), "integer %s_len", dims[idim].lname); fline(stmnt); } for (idim = 0; idim < ndims; idim++) { if (dims[idim].size == NC_UNLIMITED) { - sprintf(stmnt, "parameter (%s_len = NF_UNLIMITED)", + snprintf(stmnt, sizeof(stmnt), "parameter (%s_len = NF_UNLIMITED)", dims[idim].lname); } else { - sprintf(stmnt, "parameter (%s_len = %lu)", + snprintf(stmnt, sizeof(stmnt), "parameter (%s_len = %lu)", dims[idim].lname, (unsigned long) dims[idim].size); } @@ -641,17 +641,17 @@ gen_fortran( if (nvars > 0) { fline("* variable ids"); for (ivar = 0; ivar < nvars; ivar++) { - sprintf(stmnt, "integer %s_id", vars[ivar].lname); + snprintf(stmnt, sizeof(stmnt), "integer %s_id", vars[ivar].lname); fline(stmnt); } fline("* rank (number of dimensions) for each variable"); for (ivar = 0; ivar < nvars; ivar++) { - sprintf(stmnt, "integer %s_rank", vars[ivar].lname); + snprintf(stmnt, sizeof(stmnt), "integer %s_rank", vars[ivar].lname); fline(stmnt); } for (ivar = 0; ivar < nvars; ivar++) { - sprintf(stmnt, "parameter (%s_rank = %d)", vars[ivar].lname, + snprintf(stmnt, sizeof(stmnt), "parameter (%s_rank = %d)", vars[ivar].lname, vars[ivar].ndims); fline(stmnt); } @@ -659,7 +659,7 @@ gen_fortran( fline("* variable shapes"); for (ivar = 0; ivar < nvars; ivar++) { if (vars[ivar].ndims > 0) { - sprintf(stmnt, "integer %s_dims(%s_rank)", + snprintf(stmnt, sizeof(stmnt), "integer %s_dims(%s_rank)", vars[ivar].lname, vars[ivar].lname); fline(stmnt); } @@ -683,14 +683,14 @@ gen_fortran( continue; } if (v->ndims == 0) { /* scalar */ - sprintf(stmnt, "%s %s", ncftype(v->type), + snprintf(stmnt, sizeof(stmnt), "%s %s", ncftype(v->type), v->lname); } else { - sprintf(stmnt, "%s %s(", ncftype(v->type), + snprintf(stmnt, sizeof(stmnt), "%s %s(", ncftype(v->type), v->lname); /* reverse dimensions for FORTRAN */ for (idim = v->ndims-1; idim >= 0; idim--) { - sprintf(s2, "%s_len, ", + snprintf(s2, sizeof(s2), "%s_len, ", dims[v->dims[idim]].lname); strcat(stmnt, s2); } @@ -719,7 +719,7 @@ gen_fortran( fline("* attribute vectors"); for (itype = 0; itype < ntypes; itype++) { if (types[itype] != NC_CHAR && max_atts[(int)types[itype]] > 0) { - sprintf(stmnt, "%s %sval(%lu)", ncftype(types[itype]), + snprintf(stmnt, sizeof(stmnt), "%s %sval(%lu)", ncftype(types[itype]), nfstype(types[itype]), (unsigned long) max_atts[(int)types[itype]]); fline(stmnt); @@ -730,14 +730,14 @@ gen_fortran( /* create netCDF file, uses NC_CLOBBER mode */ fline("* enter define mode"); if (!cmode_modifier) { - sprintf(stmnt, "iret = nf_create(\'%s\', NF_CLOBBER, ncid)", filename); + snprintf(stmnt, sizeof(stmnt), "iret = nf_create(\'%s\', NF_CLOBBER, ncid)", filename); } else if (cmode_modifier & NC_64BIT_OFFSET) { - sprintf(stmnt, "iret = nf_create(\'%s\', OR(NF_CLOBBER,NF_64BIT_OFFSET), ncid)", filename); + snprintf(stmnt, sizeof(stmnt), "iret = nf_create(\'%s\', OR(NF_CLOBBER,NF_64BIT_OFFSET), ncid)", filename); #ifdef USE_NETCDF4 } else if (cmode_modifier & NC_CLASSIC_MODEL) { - sprintf(stmnt, "iret = nf_create(\'%s\', OR(NF_CLOBBER,NC_NETCDF4,NC_CLASSIC_MODEL), ncid)", filename); + snprintf(stmnt, sizeof(stmnt), "iret = nf_create(\'%s\', OR(NF_CLOBBER,NC_NETCDF4,NC_CLASSIC_MODEL), ncid)", filename); } else if (cmode_modifier & NC_NETCDF4) { - sprintf(stmnt, "iret = nf_create(\'%s\', OR(NF_CLOBBER,NF_NETCDF4), ncid)", filename); + snprintf(stmnt, sizeof(stmnt), "iret = nf_create(\'%s\', OR(NF_CLOBBER,NF_NETCDF4), ncid)", filename); #endif } else { derror("unknown cmode modifier"); @@ -750,10 +750,10 @@ gen_fortran( fline("* define dimensions"); for (idim = 0; idim < ndims; idim++) { if (dims[idim].size == NC_UNLIMITED) - sprintf(stmnt, "iret = nf_def_dim(ncid, \'%s\', NF_UNLIMITED, %s_dim)", + snprintf(stmnt, sizeof(stmnt), "iret = nf_def_dim(ncid, \'%s\', NF_UNLIMITED, %s_dim)", dims[idim].name, dims[idim].lname); else - sprintf(stmnt, "iret = nf_def_dim(ncid, \'%s\', %lu, %s_dim)", + snprintf(stmnt, sizeof(stmnt), "iret = nf_def_dim(ncid, \'%s\', %lu, %s_dim)", dims[idim].name, (unsigned long) dims[idim].size, dims[idim].lname); fline(stmnt); @@ -765,14 +765,14 @@ gen_fortran( fline("* define variables"); for (ivar = 0; ivar < nvars; ivar++) { for (idim = 0; idim < vars[ivar].ndims; idim++) { - sprintf(stmnt, "%s_dims(%d) = %s_dim", + snprintf(stmnt, sizeof(stmnt), "%s_dims(%d) = %s_dim", vars[ivar].lname, vars[ivar].ndims - idim, /* reverse dimensions */ dims[vars[ivar].dims[idim]].lname); fline(stmnt); } if (vars[ivar].ndims > 0) { /* a dimensioned variable */ - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), "iret = nf_def_var(ncid, \'%s\', %s, %s_rank, %s_dims, %s_id)", vars[ivar].name, ftypename(vars[ivar].type), @@ -780,7 +780,7 @@ gen_fortran( vars[ivar].lname, vars[ivar].lname); } else { /* a scalar */ - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), "iret = nf_def_var(ncid, \'%s\', %s, %s_rank, 0, %s_id)", vars[ivar].name, ftypename(vars[ivar].type), @@ -798,7 +798,7 @@ gen_fortran( for (iatt = 0; iatt < natts; iatt++) { if (atts[iatt].type == NC_CHAR) { /* string */ val_string = fstrstr((char *) atts[iatt].val, atts[iatt].len); - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), "iret = nf_put_att_text(ncid, %s%s, \'%s\', %lu, %s)", atts[iatt].var == -1 ? "NF_GLOBAL" : vars[atts[iatt].var].lname, atts[iatt].var == -1 ? "" : "_id", @@ -811,7 +811,7 @@ gen_fortran( } else { for (jatt = 0; jatt < atts[iatt].len ; jatt++) { val_string = fstring(atts[iatt].type,atts[iatt].val,jatt); - sprintf(stmnt, "%sval(%d) = %s", + snprintf(stmnt, sizeof(stmnt), "%sval(%d) = %s", nfstype(atts[iatt].type), jatt+1, val_string); @@ -819,7 +819,7 @@ gen_fortran( free (val_string); } - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), "iret = nf_put_att_%s(ncid, %s%s, \'%s\', %s, %lu, %sval)", nfftype(atts[iatt].type), atts[iatt].var == -1 ? "NCGLOBAL" : vars[atts[iatt].var].lname, @@ -1206,7 +1206,7 @@ fstrstr( *cp = '\0'; was_print = 1; } else { - sprintf(tstr, "char(%d)", (unsigned char)*istr); + snprintf(tstr, sizeof(tstr), "char(%d)", (unsigned char)*istr); strcat(cp, tstr); cp += strlen(tstr); was_print = 0; @@ -1239,7 +1239,7 @@ fstrstr( *cp++ = '\''; *cp = '\0'; } - sprintf(tstr, "//char(%d)", (unsigned char)*istr); + snprintf(tstr, sizeof(tstr), "//char(%d)", (unsigned char)*istr); strcat(cp, tstr); cp += strlen(tstr); was_print = 0; @@ -1346,13 +1346,13 @@ cl_fortran(void) if (have_rec_var) { fline(" "); fline("* Write record variables"); - sprintf(stmnt, "call writerecs(ncid,"); + snprintf(stmnt, sizeof(stmnt), "call writerecs(ncid,"); /* generate parameter list for subroutine to write record vars */ for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; /* if a record variable, include id in parameter list */ if (v->ndims > 0 && v->dims[0] == rec_dim) { - sprintf(s2, "%s_id,", v->lname); + snprintf(s2, sizeof(s2), "%s_id,", v->lname); strcat(stmnt, s2); } } @@ -1372,11 +1372,11 @@ cl_fortran(void) fline(" "); if (have_rec_var) { - sprintf(stmnt, "subroutine writerecs(ncid,"); + snprintf(stmnt, sizeof(stmnt), "subroutine writerecs(ncid,"); for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { - sprintf(s2, "%s_id,", v->lname); + snprintf(s2, sizeof(s2), "%s_id,", v->lname); strcat(stmnt, s2); } } @@ -1394,7 +1394,7 @@ cl_fortran(void) for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { - sprintf(stmnt, "integer %s_id", v->lname); + snprintf(stmnt, sizeof(stmnt), "integer %s_id", v->lname); fline(stmnt); } } @@ -1413,9 +1413,9 @@ cl_fortran(void) for (idim = 0; idim < ndims; idim++) { /* if used in a record variable and not record dimension */ if (used_in_rec_var(idim) && dims[idim].size != NC_UNLIMITED) { - sprintf(stmnt, "integer %s_len", dims[idim].lname); + snprintf(stmnt, sizeof(stmnt), "integer %s_len", dims[idim].lname); fline(stmnt); - sprintf(stmnt, "parameter (%s_len = %lu)", + snprintf(stmnt, sizeof(stmnt), "parameter (%s_len = %lu)", dims[idim].lname, (unsigned long) dims[idim].size); fline(stmnt); } @@ -1426,14 +1426,14 @@ cl_fortran(void) for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { - sprintf(stmnt, "integer %s_rank", v->lname); + snprintf(stmnt, sizeof(stmnt), "integer %s_rank", v->lname); fline(stmnt); } } for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { - sprintf(stmnt, "parameter (%s_rank = %d)", v->lname, + snprintf(stmnt, sizeof(stmnt), "parameter (%s_rank = %d)", v->lname, v->ndims); fline(stmnt); } @@ -1443,7 +1443,7 @@ cl_fortran(void) for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), "integer %s_start(%s_rank), %s_count(%s_rank)", v->lname, v->lname, v->lname, v->lname); fline(stmnt); @@ -1459,25 +1459,25 @@ cl_fortran(void) char *sp; fline(" "); - sprintf(stmnt, "integer %s_nr", v->lname); + snprintf(stmnt, sizeof(stmnt), "integer %s_nr", v->lname); fline(stmnt); if (v->nrecs > 0) { - sprintf(stmnt, "parameter (%s_nr = %lu)", + snprintf(stmnt, sizeof(stmnt), "parameter (%s_nr = %lu)", v->lname, (unsigned long) v->nrecs); } else { - sprintf(stmnt, "parameter (%s_nr = 1)", + snprintf(stmnt, sizeof(stmnt), "parameter (%s_nr = 1)", v->lname); } fline(stmnt); if (v->type != NC_CHAR) { - sprintf(stmnt, "%s %s(", ncftype(v->type), + snprintf(stmnt, sizeof(stmnt), "%s %s(", ncftype(v->type), v->lname); /* reverse dimensions for FORTRAN */ for (idim = v->ndims-1; idim >= 0; idim--) { if(v->dims[idim] == rec_dim) { - sprintf(s2, "%s_nr, ", v->lname); + snprintf(s2, sizeof(s2), "%s_nr, ", v->lname); } else { - sprintf(s2, "%s_len, ", + snprintf(s2, sizeof(s2), "%s_len, ", dims[v->dims[idim]].lname); } strcat(stmnt, s2); @@ -1507,7 +1507,7 @@ cl_fortran(void) for (idim = 1; idim < v->ndims; idim++) { rec_len *= dims[v->dims[idim]].size; } - sprintf(stmnt,"data %s /%lu * %s/", v->lname, + snprintf(stmnt, sizeof(stmnt),"data %s /%lu * %s/", v->lname, (unsigned long) rec_len, f_fill_name(v->type)); fline(stmnt); @@ -1521,28 +1521,28 @@ cl_fortran(void) if (v->ndims > 0 && v->dims[0] == rec_dim) { if (!v->has_data) continue; - sprintf(stmnt, "* store %s", v->name); + snprintf(stmnt, sizeof(stmnt), "* store %s", v->name); fline(stmnt); for (idim = 0; idim < v->ndims; idim++) { - sprintf(stmnt, "%s_start(%d) = 1", v->lname, idim+1); + snprintf(stmnt, sizeof(stmnt), "%s_start(%d) = 1", v->lname, idim+1); fline(stmnt); } for (idim = v->ndims-1; idim > 0; idim--) { - sprintf(stmnt, "%s_count(%d) = %s_len", v->lname, + snprintf(stmnt, sizeof(stmnt), "%s_count(%d) = %s_len", v->lname, v->ndims - idim, dims[v->dims[idim]].lname); fline(stmnt); } - sprintf(stmnt, "%s_count(%d) = %s_nr", v->lname, + snprintf(stmnt, sizeof(stmnt), "%s_count(%d) = %s_nr", v->lname, v->ndims, v->lname); fline(stmnt); if (v->type != NC_CHAR) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), "iret = nf_put_vara_%s(ncid, %s_id, %s_start, %s_count, %s)", nfftype(v->type), v->lname, v->lname, v->lname, v->lname); } else { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), "iret = nf_put_vara_%s(ncid, %s_id, %s_start, %s_count, %s)", nfftype(v->type), v->lname, v->lname, v->lname, v->data_stmnt); diff --git a/ncgen3/load.c b/ncgen3/load.c index 634751d664..df0db88ee0 100644 --- a/ncgen3/load.c +++ b/ncgen3/load.c @@ -77,22 +77,22 @@ gen_load_c( return; cline(""); - sprintf(stmnt, " {\t\t\t/* store %s */", vars[varnum].name); + snprintf(stmnt, sizeof(stmnt), " {\t\t\t/* store %s */", vars[varnum].name); cline(stmnt); if (vars[varnum].ndims > 0) { if (vars[varnum].dims[0] == rec_dim) { - sprintf(stmnt, " static size_t %s_start[RANK_%s];", + snprintf(stmnt, sizeof(stmnt), " static size_t %s_start[RANK_%s];", vars[varnum].lname, vars[varnum].lname); cline(stmnt); - sprintf(stmnt, " static size_t %s_count[RANK_%s];", + snprintf(stmnt, sizeof(stmnt), " static size_t %s_count[RANK_%s];", vars[varnum].lname, vars[varnum].lname); cline(stmnt); } /* load variable with data values using static initialization */ - sprintf(stmnt, " static %s %s[] = {", + snprintf(stmnt, sizeof(stmnt), " static %s %s[] = {", ncctype(vars[varnum].type), vars[varnum].lname); @@ -100,7 +100,7 @@ gen_load_c( switch (vars[varnum].type) { case NC_CHAR: val_string = cstrstr((char *) rec_start, var_len); - sprintf(s2, "%s", val_string); + snprintf(s2, sizeof(s2), "%s", val_string); strlcat(stmnt, s2, C_MAX_STMNT); free(val_string); break; @@ -127,23 +127,23 @@ gen_load_c( switch (vars[varnum].type) { case NC_BYTE: assert(charvalp != NULL); - sprintf(s2, "%d, ", *charvalp++); + snprintf(s2, sizeof(s2), "%d, ", *charvalp++); break; case NC_SHORT: assert(shortvalp != NULL); - sprintf(s2, "%d, ", *shortvalp++); + snprintf(s2, sizeof(s2), "%d, ", *shortvalp++); break; case NC_INT: assert(intvalp != NULL); - sprintf(s2, "%ld, ", (long)*intvalp++); + snprintf(s2, sizeof(s2), "%ld, ", (long)*intvalp++); break; case NC_FLOAT: assert(floatvalp != NULL); - sprintf(s2, "%.8g, ", *floatvalp++); + snprintf(s2, sizeof(s2), "%.8g, ", *floatvalp++); break; case NC_DOUBLE: assert(doublevalp != NULL); - sprintf(s2, "%#.16g", *doublevalp++); + snprintf(s2, sizeof(s2), "%#.16g", *doublevalp++); tztrim(s2); strcat(s2, ", "); break; @@ -162,23 +162,23 @@ gen_load_c( switch (vars[varnum].type) { case NC_BYTE: assert(charvalp != NULL); - sprintf(s2, "%d", *charvalp); + snprintf(s2, sizeof(s2), "%d", *charvalp); break; case NC_SHORT: assert(shortvalp != NULL); - sprintf(s2, "%d", *shortvalp); + snprintf(s2, sizeof(s2), "%d", *shortvalp); break; case NC_INT: assert(intvalp != NULL); - sprintf(s2, "%ld", (long)*intvalp); + snprintf(s2, sizeof(s2), "%ld", (long)*intvalp); break; case NC_FLOAT: assert(floatvalp != NULL); - sprintf(s2, "%.8g", *floatvalp); + snprintf(s2, sizeof(s2), "%.8g", *floatvalp); break; case NC_DOUBLE: assert(doublevalp != NULL); - sprintf(s2, "%#.16g", *doublevalp++); + snprintf(s2, sizeof(s2), "%#.16g", *doublevalp++); tztrim(s2); break; default: break; @@ -198,7 +198,7 @@ gen_load_c( cline(stmnt); if (vars[varnum].dims[0] == rec_dim) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " %s_len = %lu; /* number of records of %s data */", dims[rec_dim].lname, (unsigned long)vars[varnum].nrecs, /* number of recs for this variable */ @@ -206,14 +206,14 @@ gen_load_c( cline(stmnt); for (idim = 0; idim < vars[varnum].ndims; idim++) { - sprintf(stmnt, " %s_start[%d] = 0;", + snprintf(stmnt, sizeof(stmnt), " %s_start[%d] = 0;", vars[varnum].lname, idim); cline(stmnt); } for (idim = 0; idim < vars[varnum].ndims; idim++) { - sprintf(stmnt, " %s_count[%d] = %s_len;", + snprintf(stmnt, sizeof(stmnt), " %s_count[%d] = %s_len;", vars[varnum].lname, idim, dims[vars[varnum].dims[idim]].lname); @@ -222,7 +222,7 @@ gen_load_c( } if (vars[varnum].dims[0] == rec_dim) { - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_put_vara_%s(ncid, %s_id, %s_start, %s_count, %s);", ncstype(vars[varnum].type), vars[varnum].lname, @@ -230,7 +230,7 @@ gen_load_c( vars[varnum].lname, vars[varnum].lname); } else { /* non-record variables */ - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_put_var_%s(ncid, %s_id, %s);", ncstype(vars[varnum].type), vars[varnum].lname, @@ -239,7 +239,7 @@ gen_load_c( cline(stmnt); } else { /* scalar variables */ /* load variable with data values using static initialization */ - sprintf(stmnt, " static %s %s = ", + snprintf(stmnt, sizeof(stmnt), " static %s %s = ", ncctype(vars[varnum].type), vars[varnum].lname); @@ -247,28 +247,28 @@ gen_load_c( case NC_CHAR: val_string = cstrstr((char *) rec_start, var_len); val_string[strlen(val_string)-1] = '\0'; - sprintf(s2, "'%s'", &val_string[1]); + snprintf(s2, sizeof(s2), "'%s'", &val_string[1]); free(val_string); break; case NC_BYTE: charvalp = (char *) rec_start; - sprintf(s2, "%d", *charvalp); + snprintf(s2, sizeof(s2), "%d", *charvalp); break; case NC_SHORT: shortvalp = (short *) rec_start; - sprintf(s2, "%d", *shortvalp); + snprintf(s2, sizeof(s2), "%d", *shortvalp); break; case NC_INT: intvalp = (int *) rec_start; - sprintf(s2, "%ld", (long)*intvalp); + snprintf(s2, sizeof(s2), "%ld", (long)*intvalp); break; case NC_FLOAT: floatvalp = (float *) rec_start; - sprintf(s2, "%.8g", *floatvalp); + snprintf(s2, sizeof(s2), "%.8g", *floatvalp); break; case NC_DOUBLE: doublevalp = (double *) rec_start; - sprintf(s2, "%#.16g", *doublevalp++); + snprintf(s2, sizeof(s2), "%#.16g", *doublevalp++); tztrim(s2); break; default: break; @@ -276,7 +276,7 @@ gen_load_c( strlcat(stmnt, s2, C_MAX_STMNT); strlcat(stmnt,";", C_MAX_STMNT); cline(stmnt); - sprintf(stmnt, + snprintf(stmnt, sizeof(stmnt), " stat = nc_put_var_%s(ncid, %s_id, &%s);", ncstype(vars[varnum].type), vars[varnum].lname, @@ -345,14 +345,14 @@ f_var_init( int ival; /* load variable with data values */ - sprintf(stmnt, "data %s /",vars[varnum].lname); + snprintf(stmnt, sizeof(stmnt), "data %s /",vars[varnum].lname); stmnt_len = strlen(stmnt); switch (vars[varnum].type) { case NC_BYTE: charvalp = (char *) rec_start; for (ival = 0; ival < var_len-1; ival++) { val_string = fstring(NC_BYTE,(void *)charvalp++,0); - sprintf(s2, "%s, ", val_string); + snprintf(s2, sizeof(s2), "%s, ", val_string); fstrcat(stmnt, s2, &stmnt_len); free(val_string); } @@ -363,40 +363,40 @@ f_var_init( case NC_SHORT: shortvalp = (short *) rec_start; for (ival = 0; ival < var_len-1; ival++) { - sprintf(s2, "%d, ", *shortvalp++); + snprintf(s2, sizeof(s2), "%d, ", *shortvalp++); fstrcat(stmnt, s2, &stmnt_len); } - sprintf(s2, "%d", *shortvalp); + snprintf(s2, sizeof(s2), "%d", *shortvalp); fstrcat(stmnt, s2, &stmnt_len); break; case NC_INT: intvalp = (int *) rec_start; for (ival = 0; ival < var_len-1; ival++) { - sprintf(s2, "%ld, ", (long)*intvalp++); + snprintf(s2, sizeof(s2), "%ld, ", (long)*intvalp++); fstrcat(stmnt, s2, &stmnt_len); } - sprintf(s2, "%ld", (long)*intvalp); + snprintf(s2, sizeof(s2), "%ld", (long)*intvalp); fstrcat(stmnt, s2, &stmnt_len); break; case NC_FLOAT: floatvalp = (float *) rec_start; for (ival = 0; ival < var_len-1; ival++) { - sprintf(s2, "%.8g, ", *floatvalp++); + snprintf(s2, sizeof(s2), "%.8g, ", *floatvalp++); fstrcat(stmnt, s2, &stmnt_len); } - sprintf(s2, "%.8g", *floatvalp); + snprintf(s2, sizeof(s2), "%.8g", *floatvalp); fstrcat(stmnt, s2, &stmnt_len); break; case NC_DOUBLE: doublevalp = (double *) rec_start; for (ival = 0; ival < var_len-1; ival++) { - sprintf(s2, "%#.16g", *doublevalp++); + snprintf(s2, sizeof(s2), "%#.16g", *doublevalp++); tztrim(s2); expe2d(s2); /* change 'e' to 'd' in exponent */ fstrcat(s2, ", ", &stmnt_len); fstrcat(stmnt, s2, &stmnt_len); } - sprintf(s2, "%#.16g", *doublevalp++); + snprintf(s2, sizeof(s2), "%#.16g", *doublevalp++); tztrim(s2); expe2d(s2); fstrcat(stmnt, s2, &stmnt_len); @@ -432,7 +432,7 @@ gen_load_fortran( return; if (v->ndims == 0 || v->dims[0] != rec_dim) { - sprintf(stmnt, "* store %s", v->name); + snprintf(stmnt, sizeof(stmnt), "* store %s", v->name); fline(stmnt); } @@ -447,7 +447,7 @@ gen_load_fortran( return; } if (v->type != NC_CHAR) { - sprintf(stmnt, "iret = nf_put_var_%s(ncid, %s_id, %s)", + snprintf(stmnt, sizeof(stmnt), "iret = nf_put_var_%s(ncid, %s_id, %s)", nfftype(v->type), v->lname, v->lname); } else { char *char_expr = fstrstr(rec_start, valnum); @@ -459,7 +459,7 @@ gen_load_fortran( v->lname); exit(9); } - sprintf(stmnt, "iret = nf_put_var_%s(ncid, %s_id, %s)", + snprintf(stmnt, sizeof(stmnt), "iret = nf_put_var_%s(ncid, %s_id, %s)", nfftype(v->type), v->lname, char_expr); free(char_expr); } diff --git a/ncgen3/ncgen.l b/ncgen3/ncgen.l index 42490f72bd..9c304f4038 100644 --- a/ncgen3/ncgen.l +++ b/ncgen3/ncgen.l @@ -181,12 +181,12 @@ FloatInf|-?Inff { /* missing value (pre-2.4 backward compatibility) */ [+-]?[0-9]*[0-9][Bb] { int ii; if (sscanf((char*)yytext, "%d", &ii) != 1) { - sprintf(errstr,"bad byte constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad byte constant: %s",(char*)yytext); yyerror(errstr); } byte_val = ii; if (ii != (int)byte_val) { - sprintf(errstr,"byte constant out of range (-128,127): %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"byte constant out of range (-128,127): %s",(char*)yytext); yyerror(errstr); } return (BYTE_CONST); @@ -194,14 +194,14 @@ FloatInf|-?Inff { /* missing value (pre-2.4 backward compatibility) */ [+-]?[0-9]*\.[0-9]*{exp}?[LlDd]?|[+-]?[0-9]*{exp}[LlDd]? { if (sscanf((char*)yytext, "%le", &double_val) != 1) { - sprintf(errstr,"bad long or double constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad long or double constant: %s",(char*)yytext); yyerror(errstr); } return (DOUBLE_CONST); } [+-]?[0-9]*\.[0-9]*{exp}?[Ff]|[+-]?[0-9]*{exp}[Ff] { if (sscanf((char*)yytext, "%e", &float_val) != 1) { - sprintf(errstr,"bad float constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad float constant: %s",(char*)yytext); yyerror(errstr); } return (FLOAT_CONST); @@ -209,7 +209,7 @@ FloatInf|-?Inff { /* missing value (pre-2.4 backward compatibility) */ [+-]?[0-9]+[sS]|0[xX][0-9a-fA-F]+[sS] { int tmp = 0; if (sscanf((char*)yytext, "%d", &tmp) != 1) { - sprintf(errstr,"bad short constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad short constant: %s",(char*)yytext); yyerror(errstr); } short_val = (short)tmp; @@ -220,7 +220,7 @@ FloatInf|-?Inff { /* missing value (pre-2.4 backward compatibility) */ errno = 0; double_val = strtod((char*)yytext, &ptr); if (errno != 0 && double_val == 0.0) { - sprintf(errstr,"bad numerical constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad numerical constant: %s",(char*)yytext); yyerror(errstr); } if (double_val < XDR_INT_MIN ||double_val > XDR_INT_MAX) { @@ -236,7 +236,7 @@ FloatInf|-?Inff { /* missing value (pre-2.4 backward compatibility) */ errno = 0; long_val = strtol((char*)yytext, &ptr, 0); if (errno != 0) { - sprintf(errstr,"bad long constant: %s",(char*)yytext); + snprintf(errstr, sizeof(errstr),"bad long constant: %s",(char*)yytext); yyerror(errstr); } if (long_val < XDR_INT_MIN || long_val > XDR_INT_MAX) { diff --git a/ncgen3/ncgenl.c b/ncgen3/ncgenl.c index 6178f25d0c..1db8ff1d69 100644 --- a/ncgen3/ncgenl.c +++ b/ncgen3/ncgenl.c @@ -1347,12 +1347,12 @@ YY_RULE_SETUP { int ii; if (sscanf((char*)ncgtext, "%d", &ii) != 1) { - sprintf(errstr,"bad byte constant: %s",(char*)ncgtext); + snprintf(errstr,sizeof(errstr),"bad byte constant: %s",(char*)ncgtext); yyerror(errstr); } byte_val = ii; if (ii != (int)byte_val) { - sprintf(errstr,"byte constant out of range (-128,127): %s",(char*)ncgtext); + snprintf(errstr,sizeof(errstr),"byte constant out of range (-128,127): %s",(char*)ncgtext); yyerror(errstr); } return (BYTE_CONST); @@ -1363,7 +1363,7 @@ YY_RULE_SETUP #line 195 "ncgen.l" { if (sscanf((char*)ncgtext, "%le", &double_val) != 1) { - sprintf(errstr,"bad long or double constant: %s",(char*)ncgtext); + snprintf(errstr,sizeof(errstr),"bad long or double constant: %s",(char*)ncgtext); yyerror(errstr); } return (DOUBLE_CONST); @@ -1374,7 +1374,7 @@ YY_RULE_SETUP #line 202 "ncgen.l" { if (sscanf((char*)ncgtext, "%e", &float_val) != 1) { - sprintf(errstr,"bad float constant: %s",(char*)ncgtext); + snprintf(errstr,sizeof(errstr),"bad float constant: %s",(char*)ncgtext); yyerror(errstr); } return (FLOAT_CONST); @@ -1386,7 +1386,7 @@ YY_RULE_SETUP { int tmp = 0; if (sscanf((char*)ncgtext, "%d", &tmp) != 1) { - sprintf(errstr,"bad short constant: %s",(char*)ncgtext); + snprintf(errstr,sizeof(errstr),"bad short constant: %s",(char*)ncgtext); yyerror(errstr); } short_val = (short)tmp; @@ -1401,7 +1401,7 @@ YY_RULE_SETUP errno = 0; double_val = strtod((char*)ncgtext, &ptr); if (errno != 0 && double_val == 0.0) { - sprintf(errstr,"bad numerical constant: %s",(char*)ncgtext); + snprintf(errstr,sizeof(errstr),"bad numerical constant: %s",(char*)ncgtext); yyerror(errstr); } if (double_val < XDR_INT_MIN ||double_val > XDR_INT_MAX) { @@ -1421,7 +1421,7 @@ YY_RULE_SETUP errno = 0; long_val = strtol((char*)ncgtext, &ptr, 0); if (errno != 0) { - sprintf(errstr,"bad long constant: %s",(char*)ncgtext); + snprintf(errstr,sizeof(errstr),"bad long constant: %s",(char*)ncgtext); yyerror(errstr); } if (long_val < XDR_INT_MIN || long_val > XDR_INT_MAX) { diff --git a/ncgen3/ncgeny.c b/ncgen3/ncgeny.c index 494a626429..2e87109d08 100644 --- a/ncgen3/ncgeny.c +++ b/ncgen3/ncgeny.c @@ -1082,7 +1082,7 @@ yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg, return 1; } - /* Avoid sprintf, as that infringes on the user's name space. + /* Avoid snprintf, as that infringes on the user's name space. Don't have undefined behavior even if the translation produced a string with the wrong number of "%s"s. */ { diff --git a/nctest/driver.c b/nctest/driver.c index 716a178092..20c68b1039 100644 --- a/nctest/driver.c +++ b/nctest/driver.c @@ -86,7 +86,7 @@ main(int argc, char **argv) continue; /* Come up with a test file name. */ - sprintf(testfile, "nctest_%s.nc", format_name[i]); + snprintf(testfile, sizeof(testfile), "nctest_%s.nc", format_name[i]); printf("Testing %s with file %s.\n", format_name[i], testfile); /* Set the default format. */ diff --git a/nczarr_test/bm_chunks3.c b/nczarr_test/bm_chunks3.c index 9a34bda331..66d0b64a0d 100644 --- a/nczarr_test/bm_chunks3.c +++ b/nczarr_test/bm_chunks3.c @@ -348,10 +348,10 @@ test(Tag tag, int reading, int ncid, int varid, int rank, int index, size_t* dim count[index] = 1; if(chunks != NULL) - sprintf(time_mess,"%s %s %3ld %3ld %3ld %3ld %3ld %3ld", tagnames[tag], rwnames[reading], + snprintf(time_mess, sizeof(time_mess),"%s %s %3ld %3ld %3ld %3ld %3ld %3ld", tagnames[tag], rwnames[reading], count[0], count[1], count[2], chunks[0], chunks[1], chunks[2]); else - sprintf(time_mess,"%s %s %3ld %3ld %3ld", tagnames[tag], rwnames[reading], + snprintf(time_mess, sizeof(time_mess),"%s %s %3ld %3ld %3ld", tagnames[tag], rwnames[reading], count[0], count[1], count[2]); TIMING_START ; diff --git a/nczarr_test/test_zchunks.c b/nczarr_test/test_zchunks.c index 7307910aff..5e7cf6213a 100644 --- a/nczarr_test/test_zchunks.c +++ b/nczarr_test/test_zchunks.c @@ -110,7 +110,7 @@ main(int argc, char **argv) for (d = 0; d < NUM_DIM; d++) { - sprintf(dim_name, "dim_%d", dim_len[d]); + snprintf(dim_name, sizeof(dim_name), "dim_%d", dim_len[d]); #ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE printf("creating dim[%d] %s = %d\n", d, dim_name, dim_len[d]); #endif @@ -119,7 +119,7 @@ main(int argc, char **argv) for (t = 0; t < NUM_TYPE; t++) { - sprintf(var_name, "var_%d", type_id[t]); + snprintf(var_name, sizeof(var_name), "var_%d", type_id[t]); if (nc_def_var(ncid, var_name, type_id[t], NUM_DIM, dimid, &varid[t])) ERR; if (nc_inq_var_chunking(ncid, varid[t], &storage, chunksize_in)) ERR; #ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE @@ -142,7 +142,7 @@ main(int argc, char **argv) for (t = 0; t < NUM_TYPE; t++) { - sprintf(var_name, "var_%d", type_id[t]); + snprintf(var_name, sizeof(var_name), "var_%d", type_id[t]); if (nc_inq_var_chunking(ncid, varid[t], &storage, chunksize_in)) ERR; if (storage) ERR; #ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE @@ -242,7 +242,7 @@ main(int argc, char **argv) /* Oh that tricky Cardinal Richelieu, he had many plans! */ for (i = 0; i < NUM_PLANS; i++) { - sprintf(plan_name, "Richelieu_sneaky_plan_%d", i); + snprintf(plan_name, sizeof(plan_name), "Richelieu_sneaky_plan_%d", i); if (nc_def_var(ncid, plan_name, i % (NC_STRING - 1) + 1, NDIMS_3, dimids, &varid[i])) ERR; if (nc_def_var_chunking(ncid, varid[i], 0, chunksize)) ERR; diff --git a/nczarr_test/test_zchunks2.c b/nczarr_test/test_zchunks2.c index 54963761c8..271756d138 100644 --- a/nczarr_test/test_zchunks2.c +++ b/nczarr_test/test_zchunks2.c @@ -165,7 +165,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -203,7 +203,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -241,7 +241,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -282,7 +282,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -322,7 +322,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -364,7 +364,7 @@ main(int argc, char **argv) for (d = 0; d < NDIMS3; d++) { dim_len[d] = rand(); - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -403,7 +403,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } @@ -442,7 +442,7 @@ main(int argc, char **argv) /* Create a few dimensions. */ for (d = 0; d < NDIMS3; d++) { - sprintf(dim_name, "dim_%d", d); + snprintf(dim_name, sizeof(dim_name), "dim_%d", d); if (nc_def_dim(ncid, dim_name, dim_len[d], &dimids[d])) ERR; } diff --git a/oc2/dapparse.c b/oc2/dapparse.c index 8d039ad557..9a4517f794 100644 --- a/oc2/dapparse.c +++ b/oc2/dapparse.c @@ -104,7 +104,7 @@ dap_unrecognizedresponse(DAPparsestate* state) int i; char iv[32]; (void)sscanf(state->lexstate->input,"%u ",&httperr); - sprintf(iv,"%u",httperr); + snprintf(iv,sizeof(iv),"%u",httperr); state->lexstate->next = state->lexstate->input; /* Limit the amount of input to prevent runaway */ for(i=0;i<4096;i++) {if(state->lexstate->input[i] == '\0') break;} @@ -256,7 +256,7 @@ char* dimnameanon(char* basename, unsigned int index) { char name[64]; - sprintf(name,"%s_%d",basename,index); + snprintf(name,sizeof(name),"%s_%d",basename,index); return strdup(name); } diff --git a/oc2/dapy.c b/oc2/dapy.c index fc7c45d1f2..c9883f9802 100644 --- a/oc2/dapy.c +++ b/oc2/dapy.c @@ -1110,7 +1110,7 @@ yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg, return 1; } - /* Avoid sprintf, as that infringes on the user's name space. + /* Avoid snprintf, as that infringes on the user's name space. Don't have undefined behavior even if the translation produced a string with the wrong number of "%s"s. */ { diff --git a/oc2/ocdump.c b/oc2/ocdump.c index cde4df3fd3..a87dca9722 100644 --- a/oc2/ocdump.c +++ b/oc2/ocdump.c @@ -259,27 +259,27 @@ dumpfield(size_t index, char* n8, int isxdr) line[0] = '\0'; /* offset */ - sprintf(tmp,"%6zd",index); + snprintf(tmp,sizeof(tmp),"%6zd",index); addfield(tmp,sizeof(line),line,5); memcpy(form.cv,n8,4); /* straight hex*/ - sprintf(tmp,"%08x",form.uv); + snprintf(tmp,sizeof(tmp),"%08x",form.uv); addfield(tmp,sizeof(line),line,8); if(isxdr) {swapinline32(&form.uv);} /* unsigned integer */ - sprintf(tmp,"%12u",form.uv); + snprintf(tmp,sizeof(tmp),"%12u",form.uv); addfield(tmp,sizeof(line),line,12); /* signed integer */ - sprintf(tmp,"%12d",form.sv); + snprintf(tmp,sizeof(tmp),"%12d",form.sv); addfield(tmp,sizeof(line),line,12); /* float */ - sprintf(tmp,"%#g",form.fv); + snprintf(tmp,sizeof(tmp),"%#g",form.fv); addfield(tmp,sizeof(line),line,12); /* char[4] */ @@ -303,7 +303,7 @@ dumpfield(size_t index, char* n8, int isxdr) /* double */ memcpy(dform.cv,n8,(size_t)(2*XDRUNIT)); if(isxdr) xxdrntohdouble(dform.cv,&dform.d); - sprintf(tmp,"%#g",dform.d); + snprintf(tmp,sizeof(tmp),"%#g",dform.d); addfield(tmp,sizeof(line),line,12); fprintf(stdout,"%s\n",line); @@ -378,11 +378,11 @@ simplememorydump(char* memory, size_t len, int fromxdr) unsigned int v = vx; if(!xxdr_network_order) swapinline32(&v); line[0] = '\0'; - sprintf(tmp,"%6d",i); + snprintf(tmp,sizeof(tmp),"%6d",i); addfield(tmp,sizeof(line),line,6); - sprintf(tmp,"%08x",vx); + snprintf(tmp,sizeof(tmp),"%08x",vx); addfield(tmp,sizeof(line),line,9); - sprintf(tmp,"%08x",v); + snprintf(tmp,sizeof(tmp),"%08x",v); addfield(tmp,sizeof(line),line,10); fprintf(stdout,"%s\n",line); } From adc4dc143514fb498cfd92c74fc00e18ada74c16 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Sun, 30 Apr 2023 18:36:39 -0400 Subject: [PATCH 2/2] Replaced some sprintf with snprintf with aid of new variable containing size One case required slightly complicated accounting of how much space is left in the buffer. --- libhdf5/hdf5var.c | 7 ++--- libnczarr/zvar.c | 7 ++--- nc_test/tst_def_var_fill.c | 5 ++-- ncdump/dumplib.c | 6 +++-- ncgen/util.c | 10 +++++--- ncgen3/genlib.c | 52 +++++++++++++++++++++++--------------- 6 files changed, 53 insertions(+), 34 deletions(-) diff --git a/libhdf5/hdf5var.c b/libhdf5/hdf5var.c index 6d37fe9099..6c2303ec45 100644 --- a/libhdf5/hdf5var.c +++ b/libhdf5/hdf5var.c @@ -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; } diff --git a/libnczarr/zvar.c b/libnczarr/zvar.c index 27e30a9f19..5076ff740a 100644 --- a/libnczarr/zvar.c +++ b/libnczarr/zvar.c @@ -239,11 +239,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; } diff --git a/nc_test/tst_def_var_fill.c b/nc_test/tst_def_var_fill.c index 5c32485420..425d563c8a 100644 --- a/nc_test/tst_def_var_fill.c +++ b/nc_test/tst_def_var_fill.c @@ -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); diff --git a/ncdump/dumplib.c b/ncdump/dumplib.c index 03fe3b278e..b157937d16 100644 --- a/ncdump/dumplib.c +++ b/ncdump/dumplib.c @@ -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++ = '"'; @@ -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; diff --git a/ncgen/util.c b/ncgen/util.c index 108fff0440..589582c611 100644 --- a/ncgen/util.c +++ b/ncgen/util.c @@ -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; } @@ -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; } diff --git a/ncgen3/genlib.c b/ncgen3/genlib.c index ead98b08df..9c9f987c6f 100644 --- a/ncgen3/genlib.c +++ b/ncgen3/genlib.c @@ -129,6 +129,7 @@ cstring( int *intp; float *floatp; double *doublep; + size_t cp_size; switch (type) { case NC_CHAR: @@ -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: @@ -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;