diff --git a/.gitignore b/.gitignore index 9abf05757..14eed1c50 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ test-driver ##### ### 'Normal' gitignore files. +docs/auth.html .vscode nug.tag netcdf-c.tag diff --git a/CMakeLists.txt b/CMakeLists.txt index 189f6d87c..a925a88d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1160,7 +1160,8 @@ set_std_filter(Szip) SET(HAVE_SZ ${Szip_FOUND}) set_std_filter(Blosc) IF(Zstd_FOUND) -set_std_filter(Zstd) + set_std_filter(Zstd) + SET(HAVE_ZSTD ON) ENDIF() IF(Bz2_FOUND) set_std_filter(Bz2) @@ -1168,7 +1169,7 @@ ELSE() # The reason we use a local version is to support a more comples test case MESSAGE("libbz2 not found using built-in version") SET(HAVE_LOCAL_BZ2 ON) - SET(HAVE_BZ2 ON) + SET(HAVE_BZ2 ON CACHE BOOL "") set(STD_FILTERS "${STD_FILTERS} bz2") ENDIF() @@ -1507,6 +1508,7 @@ ENDIF() # Enable Parallel IO with netCDF-4/HDF5 files using HDF5 parallel I/O. SET(STATUS_PARALLEL "OFF") +set(IMPORT_MPI "") OPTION(ENABLE_PARALLEL4 "Build netCDF-4 with parallel IO" "${HDF5_PARALLEL}") IF(ENABLE_PARALLEL4 AND ENABLE_HDF5) IF(NOT HDF5_PARALLEL) @@ -1528,6 +1530,7 @@ IF(ENABLE_PARALLEL4 AND ENABLE_HDF5) FILE(COPY "${netCDF_BINARY_DIR}/tmp/run_par_tests.sh" DESTINATION ${netCDF_BINARY_DIR}/h5_test FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + set(IMPORT_MPI "include(CMakeFindDependencyMacro)\nfind_dependency(MPI COMPONENTS C)") ENDIF() ENDIF() diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 8ec6c93b7..5dd940304 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -7,6 +7,7 @@ This file contains a high-level description of this package's evolution. Release ## 4.9.3 - TBD +* Obviate a number of irrelevant warnings. See [Github #2781](https://github.com/Unidata/netcdf-c/pull/2781). * Improve the speed and data quantity for DAP4 queries. See [Github #2765](https://github.com/Unidata/netcdf-c/pull/2765). * Remove the use of execinfo to programmatically dump the stack; it never worked. See [Github #2789](https://github.com/Unidata/netcdf-c/pull/2789). * Update the internal copy of tinyxml2 to latest code. See [Github #2771](https://github.com/Unidata/netcdf-c/pull/2771). diff --git a/cmake/modules/FindBlosc.cmake b/cmake/modules/FindBlosc.cmake old mode 100755 new mode 100644 diff --git a/cmake/modules/FindSzip.cmake b/cmake/modules/FindSzip.cmake index 4deca3347..c6f56f5bf 100644 --- a/cmake/modules/FindSzip.cmake +++ b/cmake/modules/FindSzip.cmake @@ -2,7 +2,10 @@ # # Szip_FOUND Set to true to indicate the szip library was found # Szip_INCLUDE_DIRS The directory containing the header file szip/szip.h -# Szip_LIBRARIES The libraries needed to use the szip library +# Szip_LIBRARIES The libraries needed to use the szip library with the word "debug" and "optimized" when both are found +# Szip_RELEASE_LIBRARY The path to the Szip release library if available +# Szip_DEBUG_LIBRARY The path to the Szip debug library if available +# Szip_LIBRARY The path to a Szip library, preferentially release but fallback to debug # # To specify an additional directory to search, set Szip_ROOT. # @@ -38,13 +41,17 @@ IF(Szip_INCLUDE_DIRS) PATH_SUFFIXES Release ${CMAKE_LIBRARY_ARCHITECTURE} ${CMAKE_LIBRARY_ARCHITECTURE}/Release PATHS ${Szip_LIBRARY_DIRS} NO_DEFAULT_PATH) - SET(Szip_LIBRARIES ) + SET(Szip_LIBRARIES) + SET(Szip_LIBRARY) IF(Szip_DEBUG_LIBRARY AND Szip_RELEASE_LIBRARY) SET(Szip_LIBRARIES debug ${Szip_DEBUG_LIBRARY} optimized ${Szip_RELEASE_LIBRARY}) + SET(Szip_LIBRARY ${Szip_RELEASE_LIBRARY}) ELSEIF(Szip_DEBUG_LIBRARY) SET(Szip_LIBRARIES ${Szip_DEBUG_LIBRARY}) + SET(Szip_LIBRARY ${Szip_DEBUG_LIBRARY}) ELSEIF(Szip_RELEASE_LIBRARY) SET(Szip_LIBRARIES ${Szip_RELEASE_LIBRARY}) + SET(Szip_LIBRARY ${Szip_RELEASE_LIBRARY}) ENDIF(Szip_DEBUG_LIBRARY AND Szip_RELEASE_LIBRARY) IF(Szip_LIBRARIES) diff --git a/cmake/modules/FindZip.cmake b/cmake/modules/FindZip.cmake old mode 100755 new mode 100644 diff --git a/config.h.cmake.in b/config.h.cmake.in index 233c2f1e2..f10a3a91a 100644 --- a/config.h.cmake.in +++ b/config.h.cmake.in @@ -118,6 +118,12 @@ are set when opening a binary file on Windows. */ /* if true, build byte-range Client */ #cmakedefine ENABLE_BYTERANGE 1 +/* if true, enable ERANGE fill */ +#cmakedefine ENABLE_ERANGE_FILL 1 +#ifdef ENABLE_ERANGE_FILL +#define ERANGE_FILL 1 +#endif + /* if true, use hdf5 S3 virtual file reader */ #cmakedefine ENABLE_HDF5_ROS3 1 diff --git a/docs/Doxyfile.in b/docs/Doxyfile.in index 84a227ceb..663caa93a 100644 --- a/docs/Doxyfile.in +++ b/docs/Doxyfile.in @@ -1269,15 +1269,6 @@ HTML_COLORSTYLE_SAT = 100 HTML_COLORSTYLE_GAMMA = 80 -# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML -# page will contain the date and time when the page was generated. Setting this -# to YES can help to show when doxygen was last run and thus if the -# documentation is up to date. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_TIMESTAMP = NO - # If the HTML_DYNAMIC_MENUS tag is set to YES then the generated HTML # documentation will contain a main index with vertical navigation menus that # are dynamically created via JavaScript. If disabled, the navigation index will @@ -1865,14 +1856,6 @@ LATEX_HIDE_INDICES = NO LATEX_BIB_STYLE = plain -# If the LATEX_TIMESTAMP tag is set to YES then the footer of each generated -# page will contain the date and time when the page was generated. Setting this -# to NO can help when comparing the output of multiple runs. -# The default value is: NO. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -LATEX_TIMESTAMP = NO - # The LATEX_EMOJI_DIRECTORY tag is used to specify the (relative or absolute) # path from which the emoji images will be read. If a relative path is entered, # it will be relative to the LATEX_OUTPUT directory. If left blank the diff --git a/docs/cloud.md b/docs/cloud.md index 23e6a0ae3..22e73c57c 100644 --- a/docs/cloud.md +++ b/docs/cloud.md @@ -67,9 +67,9 @@ https://thredds-test.unidata.ucar.edu/thredds/fileServer/irma/metar/files/METAR_ # References {#nccloud_bib} -[1] [Amazon Simple Storage Service Documentation](https://docs.aws.amazon.com/s3/index.html)
-[2] [Amazon Simple Storage Service Library](https://github.com/aws/aws-sdk-cpp)
-[11] [Conda-forge / packages / aws-sdk-cpp](https://anaconda.org/conda-forge/aws-sdk-cpp)
+[1] [Amazon Simple Storage Service Documentation](https://docs.aws.amazon.com/s3/index.html)
+[2] [Amazon Simple Storage Service Library](https://github.com/aws/aws-sdk-cpp)
+[11] [Conda-forge / packages / aws-sdk-cpp](https://anaconda.org/conda-forge/aws-sdk-cpp)
# Appendix A. S3 Build Support {#nccloud_s3build} diff --git a/docs/dispatch.md b/docs/dispatch.md index cbc6b2ae7..a3a59baab 100644 --- a/docs/dispatch.md +++ b/docs/dispatch.md @@ -520,7 +520,7 @@ Modifying the dispatch version requires two steps: The two should agree in value. -### NC_DISPATCH_VERSION Incompatibility +## NC_DISPATCH_VERSION Incompatibility When dynamically adding a dispatch table -- in nc_def_user_format (see libdispatch/dfile.c) -- diff --git a/docs/filters.md b/docs/filters.md index 995544b82..400ac896a 100644 --- a/docs/filters.md +++ b/docs/filters.md @@ -65,6 +65,28 @@ The concept of a variable-sized type is defined as follows: then that compound type is variable-sized. 4. All other types are fixed-size. +## A Warning on Backward Compatibility {#filters_compatibility} + +The API defined in this document should accurately reflect the +current state of filters in the netCDF-c library. Be aware that +there was a short period in which the filter code was undergoing +some revision and extension. Those extensions have largely been +reverted. Unfortunately, some users may experience some +compilation problems for previously working code because of +these reversions. In that case, please revise your code to +adhere to this document. Apologies are extended for any +inconvenience. + +A user may encounter an incompatibility if any of the following appears in user code. + +* The function *\_nc\_inq\_var\_filter* was returning the error value NC\_ENOFILTER if a variable had no associated filters. + It has been reverted to the previous case where it returns NC\_NOERR and the returned filter id was set to zero if the variable had no filters. +* The function *nc\_inq\_var\_filterids* was renamed to *nc\_inq\_var\_filter\_ids*. +* Some auxilliary functions for parsing textual filter specifications have been moved to the file *netcdf\_aux.h*. See [Appendix A](#filters_appendixa). +* All of the "filterx" functions have been removed. This is unlikely to cause problems because they had limited visibility. + +For additional information, see [Appendix B](#filters_appendixb). + ## Enabling A HDF5 Compression Filter {#filters_enable} HDF5 supports dynamic loading of compression filters using the @@ -611,7 +633,99 @@ As part of its testing, the NetCDF build process creates a number of shared libr If you need a filter from that set, you may be able to set *HDF5\_PLUGIN\_PATH* to point to that directory or you may be able to copy the shared libraries out of that directory to your own location. -## Debugging {#filters_debug} +# Lossy One-Way Filters + +As of NetCDF version 4.8.2, the netcdf-c library supports +bit-grooming filters. +```` +Bit-grooming is a lossy compression algorithm that removes the +bloat due to false-precision, those bits and bytes beyond the +meaningful precision of the data. Bit Grooming is statistically +unbiased, applies to all floating point numbers, and is easy to +use. Bit-Grooming reduces data storage requirements by +25-80%. Unlike its best-known competitor Linear Packing, Bit +Grooming imposes no software overhead on users, and guarantees +its precision throughout the whole floating point range +[https://doi.org/10.5194/gmd-9-3199-2016]. +```` +The generic term "quantize" is used to refer collectively to the various +precision-trimming algorithms. The key thing to note about quantization is that +it occurs at the point of writing of data only. Since its output is +legal data, it does not need to be "de-quantized" when the data is read. +Because of this, quantization is not part of the standard filter +mechanism and has a separate API. + +The API for bit-groom is currently as follows. +```` +int nc_def_var_quantize(int ncid, int varid, int quantize_mode, int nsd); +int nc_inq_var_quantize(int ncid, int varid, int *quantize_modep, int *nsdp); +```` +The *quantize_mode* argument specifies the particular algorithm. +Currently, three are supported: NC_QUANTIZE_BITGROOM, NC_QUANTIZE_GRANULARBR, +and NC_QUANTIZE_BITROUND. In addition quantization can be disabled using +the value NC_NOQUANTIZE. + +The input to ncgen or the output from ncdump supports special attributes +to indicate if quantization was applied to a given variable. +These attributes have the following form. +```` +_QuantizeBitGroomNumberOfSignificantDigits = +or +_QuantizeGranularBitRoundNumberOfSignificantDigits = +or +_QuantizeBitRoundNumberOfSignificantBits = +```` +The value NSD is the number of significant (decimal) digits to keep. +The value NSB is the number of bits to keep in the fraction part of an +IEEE754 floating-point number. Note that NSB of QuantizeBitRound is the same as +"number of explicit mantissa bits" (https://doi.org/10.5194/gmd-9-3199-2016) and same as +the number of "keep-bits" (https://doi.org/10.5194/gmd-14-377-2021), but is not +one less than the number of significant bunary figures: +`_QuantizeBitRoundNumberOfSignificantBits = 0` means one significant binary figure, +`_QuantizeBitRoundNumberOfSignificantBits = 1` means two significant binary figures etc. + +## Distortions introduced by lossy filters + + Any lossy filter introduces distortions to data. + The lossy filters implemented in netcdf-c introduce a distortoin + that can be quantified in terms of a _relative_ error. The magnitude of + distortion introduced to every single value V is guaranteed to be within + a certain fraction of V, expressed as 0.5 * V * 2**{-NSB}: + i.e. it is 0.5V for NSB=0, 0.25V for NSB=1, 0.125V for NSB=2 etc. + + + Two other methods use different definitions of _decimal precision_, though both + are guaranteed to reproduce NSD decimals when printed. + The margin for a relative error introduced by the methods are summarised in the table + + ``` + NSD 1 2 3 4 5 6 7 + + BitGroom + Error Margin 3.1e-2 3.9e-3 4.9e-4 3.1e-5 3.8e-6 4.7e-7 - + + GranularBitRound + Error Margin 1.4e-1 1.9e-2 2.2e-3 1.4e-4 1.8e-5 2.2e-6 - + + ``` + + + If one defines decimal precision as in BitGroom, i.e. the introduced relative + error must not exceed half of the unit at the decimal place NSD in the + worst-case scenario, the following values of NSB should be used for BitRound: + + ``` + NSD 1 2 3 4 5 6 7 + NSB 3 6 9 13 16 19 23 + ``` + + The resulting application of BitRound is as fast as BitGroom, and is free from + artifacts in multipoint statistics introduced by BitGroom + (see https://doi.org/10.5194/gmd-14-377-2021). + + +# Debugging {#filters_debug} + Depending on the debugger one uses, debugging plugins can be very difficult. It may be necessary to use the old printf approach for debugging the filter itself. @@ -625,7 +739,7 @@ This can be accomplished using this command. Since ncdump is not being asked to access the data (the -h flag), it can obtain the filter information without failures. Then it can print out the filter id and the parameters as well as the Codecs (via the -s flag). -### Test Cases {#filters_TestCase} +## Test Cases {#filters_TestCase} Within the netcdf-c source tree, the directory two directories contain test cases for testing dynamic filter operation. diff --git a/docs/internal.md b/docs/internal.md index 2ddf3fb74..7d7ce2fe5 100644 --- a/docs/internal.md +++ b/docs/internal.md @@ -8,13 +8,13 @@ This document attempts to record important information about the internal architecture and operation of the netcdf-c library. It covers the following issues. -* [Including C++ Code in the netcdf-c Library](#intern_c++) +* [Including C++ Code in the netcdf-c Library](#intern_cpp) * [Managing instances of variable-length data types](#intern_vlens) * [Inferring File Types](#intern_infer) * [Adding a Standard Filter](#intern_filters) * [Test Interference](#intern_isolation) -# 1. Including C++ Code in the netcdf-c Library {#intern_c++} +# 1. Including C++ Code in the netcdf-c Library {#intern_cpp} The state of C compiler technology has reached the point where it is possible to include C++ code into the netcdf-c library diff --git a/docs/mainpage.dox b/docs/mainpage.dox index 64c2df84a..0284bd831 100644 --- a/docs/mainpage.dox +++ b/docs/mainpage.dox @@ -13,7 +13,7 @@ The NetCDF homepage may be found at The NetCDF-Fortran Developer's Guide +- The NetCDF-Fortran Developer's Guide \section this_release Learn more about the current NetCDF-C Release diff --git a/docs/nczarr.md b/docs/nczarr.md index 4c2a5595f..6fafa1ee2 100644 --- a/docs/nczarr.md +++ b/docs/nczarr.md @@ -417,7 +417,7 @@ In order to accomodate existing implementations, certain mode tags are provided ## XArray -The Xarray [7] Zarr implementation uses its own mechanism for specifying shared dimensions. +The Xarray [XArray Zarr Encoding Specification](http://xarray.pydata.org/en/latest/internals.html#zarr-encoding-specification) Zarr implementation uses its own mechanism for specifying shared dimensions. It uses a special attribute named ''_ARRAY_DIMENSIONS''. The value of this attribute is a list of dimension names (strings). An example might be ````["time", "lon", "lat"]````. @@ -449,16 +449,16 @@ Here are a couple of examples using the _ncgen_ and _ncdump_ utilities. ``` 4. Create an nczarr file using S3 as storage and keeping to the pure zarr format. ``` - ncgen -4 -lb -o "s3://s3.uswest-1.amazonaws.com/datasetbucket#mode=zarr" dataset.cdl + ncgen -4 -lb -o 's3://s3.uswest-1.amazonaws.com/datasetbucket\#mode=zarr dataset.cdl ``` 5. Create an nczarr file using the s3 protocol with a specific profile ``` - ncgen -4 -lb -o "s3://datasetbucket/rootkey#mode=nczarr,awsprofile=unidata" dataset.cdl + ncgen -4 -lb -o 's3://datasetbucket/rootkey\#mode=nczarr,awsprofile=unidata' dataset.cdl ``` Note that the URL is internally translated to this - ```` - https://s2.<region>.amazonaws.com/datasetbucket/rootkey#mode=nczarr,awsprofile=unidata" dataset.cdl - ```` + ``` + 'https://s2.<region>.amazonaws.com/datasetbucket/rootkey#mode=nczarr,awsprofile=unidata' dataset.cdl + ``` # References {#nczarr_bib} @@ -473,7 +473,7 @@ collections — High-performance dataset datatypes](https://docs.python.org/2/li [8] [Dynamic Filter Loading](https://support.hdfgroup.org/HDF5/doc/Advanced/DynamicallyLoadedFilters/HDF5DynamicallyLoadedFilters.pdf)
[9] [Officially Registered Custom HDF5 Filters](https://portal.hdfgroup.org/display/support/Registered+Filter+Plugins)
[10] [C-Blosc Compressor Implementation](https://github.com/Blosc/c-blosc)
-[11] [Conda-forge / packages / aws-sdk-cpp](https://anaconda.org/conda-forge/aws-sdk-cpp)
+[11] [Conda-forge packages / aws-sdk-cpp](https://anaconda.org/conda-forge/aws-sdk-cpp)
[12] [GDAL Zarr](https://gdal.org/drivers/raster/zarr.html)
# Appendix A. Building NCZarr Support {#nczarr_build} @@ -539,7 +539,7 @@ PATH="$PATH:${AWSSDKBIN}" Then the following options must be specified for cmake. ```` -DAWSSDK_ROOT_DIR=${AWSSDK_ROOT_DIR} --DAWSSDK_DIR=${AWSSDK_ROOT_DIR}/lib/cmake/AWSSDK" +-DAWSSDK_DIR=${AWSSDK_ROOT_DIR}/lib/cmake/AWSSDK ```` # Appendix B. Amazon S3 Imposed Limits {#nczarr_s3limits} @@ -578,7 +578,7 @@ can in fact be any legal JSON expression. This "convention" is currently used routinely to help support various attributes created by other packages where the attribute is a complex JSON expression. An example is the GDAL Driver -convention [12], where the value is a complex +convention [12], where the value is a complex JSON dictionary. In order for NCZarr to be as consistent as possible with Zarr Version 2, diff --git a/docs/testserver.dox b/docs/testserver.dox old mode 100755 new mode 100644 diff --git a/h5_test/tst_h_compounds.c b/h5_test/tst_h_compounds.c index c53032c3f..952e2585a 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 7186778fe..8eeee14c4 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_dimscales2.c b/h5_test/tst_h_dimscales2.c index 07592e206..eb7b2dfb5 100644 --- a/h5_test/tst_h_dimscales2.c +++ b/h5_test/tst_h_dimscales2.c @@ -633,6 +633,7 @@ main() hsize_t h5dimlen[DIMS2], h5dimlenmax[DIMS2], xtend_size[DIMS2] = {1, NUM_VALS}; hsize_t start[DIMS2] = {0, 0}; hsize_t count[DIMS2] = {1, NUM_VALS}; + hsize_t ones[DIMS2] = {1, 1}; double value[NUM_VALS]; int dataset_ndims; int i; @@ -661,7 +662,7 @@ main() /* Set up the file and memory spaces. */ if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) ERR; + start, NULL, ones, count) < 0) ERR; if ((mem_spaceid = H5Screate_simple(DIMS2, count, NULL)) < 0) ERR; /* Write a slice of data. */ @@ -683,7 +684,7 @@ main() /* Set up the file and memory spaces for a second slice. */ start[0]++; if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) ERR; + start, NULL, ones, count) < 0) ERR; if ((mem_spaceid = H5Screate_simple(DIMS2, count, NULL)) < 0) ERR; /* Write a second slice of data. */ diff --git a/h5_test/tst_h_dimscales4.c b/h5_test/tst_h_dimscales4.c index c12ca9624..430960926 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_enums.c b/h5_test/tst_h_enums.c index 352b4dce3..a107023a4 100644 --- a/h5_test/tst_h_enums.c +++ b/h5_test/tst_h_enums.c @@ -154,7 +154,7 @@ main() char lang[NUM_LANG][STR_LEN + 1] = {"C", "Fortran", "C++", "MISSING"}; enum langs {CLANG=0, Fortran=1, CPP=2, MISSING=255}; short the_value, fill_value = MISSING, data_point = CLANG; - hsize_t start[1] = {1}, count[1] = {1}; + hsize_t start[1] = {1}, count[1] = {1}, one[1] = {1}; int num_members; size_t size; hid_t base_hdf_typeid; @@ -197,7 +197,7 @@ main() if ((mem_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR; if ((file_spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR; if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) ERR; + start, NULL, one, count) < 0) ERR; if (H5Dwrite(datasetid, typeid, mem_spaceid, file_spaceid, H5P_DEFAULT, &data_point) < 0) ERR; diff --git a/h5_test/tst_h_files.c b/h5_test/tst_h_files.c index f4bc617f0..51d04ca79 100644 --- a/h5_test/tst_h_files.c +++ b/h5_test/tst_h_files.c @@ -174,7 +174,7 @@ main() #define MILLION 1000000 hid_t fileid, write_spaceid, datasetid, mem_spaceid; - hsize_t start[NDIMS], count[NDIMS]; + hsize_t start[NDIMS], count[NDIMS], ones[NDIMS]; hsize_t dims[1]; int *data; int num_steps; @@ -210,8 +210,9 @@ main() { /* Select hyperslab for write of one slice. */ start[0] = s * SC; + ones[0] = 1; if (H5Sselect_hyperslab(write_spaceid, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) ERR; + start, NULL, ones, count) < 0) ERR; if (H5Dwrite(datasetid, H5T_NATIVE_INT, mem_spaceid, write_spaceid, H5P_DEFAULT, data) < 0) ERR; @@ -242,13 +243,13 @@ main() hid_t mem_spaceid, xfer_plistid, native_typeid; hsize_t *chunksize, dims[1], maxdims[1], *dimsize, *maxdimsize; hsize_t fdims[MAX_DIMS], fmaxdims[MAX_DIMS]; - hsize_t start[MAX_DIMS], count[MAX_DIMS]; + hsize_t start[MAX_DIMS], count[MAX_DIMS], ones[MAX_DIMS]; char file_name[STR_LEN + 1]; char dimscale_wo_var[STR_LEN]; 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 +286,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 +300,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. */ @@ -342,7 +343,8 @@ main() start[1] = 0; count[0] = 1; count[1] = 2097153; - if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL, count, NULL) < 0) ERR; + ones[0] = ones[1] = 1; + if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL, ones, count) < 0) ERR; if ((mem_spaceid = H5Screate_simple(NDIMS2, count, NULL)) < 0) ERR; if ((xfer_plistid = H5Pcreate(H5P_DATASET_XFER)) < 0) ERR; if ((native_typeid = H5Tget_native_type(H5T_NATIVE_SCHAR, H5T_DIR_DEFAULT)) < 0) ERR; diff --git a/h5_test/tst_h_mem.c b/h5_test/tst_h_mem.c index d2b1d4a24..d7b0b9e5d 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_par.c b/h5_test/tst_h_par.c index 2d350a453..e8c9c2291 100644 --- a/h5_test/tst_h_par.c +++ b/h5_test/tst_h_par.c @@ -66,7 +66,7 @@ main(int argc, char **argv) printf("*** Creating file for parallel I/O read, and rereading it..."); { hid_t fapl_id, fileid, whole_spaceid, dsid, slice_spaceid, whole_spaceid1, xferid; - hsize_t start[NDIMS], count[NDIMS]; + hsize_t start[NDIMS], count[NDIMS], ones[NDIMS]; hsize_t dims[1]; int data[SC1], data_in[SC1]; int num_steps; @@ -126,8 +126,9 @@ main(int argc, char **argv) /* Select hyperslab for write of one slice. */ start[0] = s * SC1 * p + my_rank * SC1; count[0] = SC1; + ones[0] = 1; if (H5Sselect_hyperslab(whole_spaceid, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) ERR; + start, NULL, ones, count) < 0) ERR; if (H5Dwrite(dsid, H5T_NATIVE_INT, slice_spaceid, whole_spaceid, xferid, data) < 0) ERR; @@ -185,8 +186,9 @@ main(int argc, char **argv) /* Select hyperslab for read of one slice. */ start[0] = s * SC1 * p + my_rank * SC1; count[0] = SC1; + ones[0] = 1; if (H5Sselect_hyperslab(whole_spaceid1, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) + start, NULL, ones, count) < 0) { ERR; return 2; diff --git a/h5_test/tst_h_par_compress.c b/h5_test/tst_h_par_compress.c index 6a15e6a81..e63475803 100644 --- a/h5_test/tst_h_par_compress.c +++ b/h5_test/tst_h_par_compress.c @@ -51,7 +51,7 @@ main(int argc, char **argv) { hid_t fapl_id, fileid, whole_spaceid, dsid, slice_spaceid, whole_spaceid1, xferid; hid_t plistid; - hsize_t start[NDIMS], count[NDIMS]; + hsize_t start[NDIMS], count[NDIMS], ones[NDIMS]; hsize_t dims[1], chunksize = SC1; int data[SC1], data_in[SC1]; int num_steps; @@ -120,8 +120,9 @@ main(int argc, char **argv) /* Select hyperslab for write of one slice. */ start[0] = s * SC1 * p + my_rank * SC1; count[0] = SC1; + ones[0] = 1; if (H5Sselect_hyperslab(whole_spaceid, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) ERR; + start, NULL, ones, count) < 0) ERR; if (H5Dwrite(dsid, H5T_NATIVE_INT, slice_spaceid, whole_spaceid, xferid, data) < 0) ERR; @@ -160,8 +161,9 @@ main(int argc, char **argv) /* Select hyperslab for read of one slice. */ start[0] = s * SC1 * p + my_rank * SC1; count[0] = SC1; + ones[0] = 1; if (H5Sselect_hyperslab(whole_spaceid1, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) + start, NULL, ones, count) < 0) { ERR; return 2; diff --git a/h5_test/tst_h_strings2.c b/h5_test/tst_h_strings2.c index 1d03a983c..a451a6ac6 100644 --- a/h5_test/tst_h_strings2.c +++ b/h5_test/tst_h_strings2.c @@ -30,6 +30,7 @@ main() hid_t file_spaceid, mem_spaceid; hsize_t dims[1] = {0}, max_dims[1] = {H5S_UNLIMITED}, chunk_dims[1] = {1}; hsize_t xtend_size[NDIMS] = {2}, start[NDIMS] = {1}, count[NDIMS] = {1}; + hsize_t ones[NDIMS] = {1}; /* void *fillp;*/ char *data = "A man who carries a cat by the tail learns " "something he can learn in no other way."; @@ -91,7 +92,7 @@ main() /* Select space in file to write a record. */ if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) ERR; + start, NULL, ones, count) < 0) ERR; /* Select space in memory to read from. */ if ((mem_spaceid = H5Screate_simple(NDIMS, count, NULL)) < 0) ERR; @@ -126,7 +127,7 @@ main() hid_t typeid, datasetid, plistid; hid_t file_spaceid, mem_spaceid; hsize_t dims[1] = {2}, chunk_dims[1] = {1}; - hsize_t start[NDIMS] = {1}, count[NDIMS] = {1}; + hsize_t start[NDIMS] = {1}, count[NDIMS] = {1}, ones[NDIMS] = {1}; /* void *fillp;*/ char *data = "A man who carries a cat by the tail learns " "something he can learn in no other way."; @@ -179,7 +180,7 @@ To be good is noble; but to show others how to be good is nobler and no trouble. /* Select space in file to write a record. */ if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) ERR; + start, NULL, ones, count) < 0) ERR; /* Select space in memory to read from. */ if ((mem_spaceid = H5Screate_simple(NDIMS, count, NULL)) < 0) diff --git a/h5_test/tst_h_vars.c b/h5_test/tst_h_vars.c index 77be6abd4..a74ab5047 100644 --- a/h5_test/tst_h_vars.c +++ b/h5_test/tst_h_vars.c @@ -69,7 +69,7 @@ main() float float_data_out[LAT_LEN][LON_LEN]; hsize_t dims[NDIMS], max_dims[NDIMS]; hsize_t dims_in[NDIMS], max_dims_in[NDIMS]; - hsize_t start[MAX_DIMS], count[MAX_DIMS]; + hsize_t start[MAX_DIMS], count[MAX_DIMS], ones[MAX_DIMS]; int lat, lon; /* Set up some phoney data, 1 record's worth. In C, first @@ -153,8 +153,9 @@ main() start[0] = 1; start[1] = 0; start[2] = 0; + ones[0] = ones[1] = ones[2] = 1; if (H5Sselect_hyperslab(write_spaceid, H5S_SELECT_SET, - start, NULL, count, NULL) < 0) ERR; + start, NULL, ones, count) < 0) ERR; /* Write second record of data to each dataset. */ if (H5Dwrite(pres_dsid, H5T_IEEE_F32LE, mem_spaceid, write_spaceid, @@ -517,7 +518,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 400be6bb4..e53b67752 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/h5_test/tst_h_vl.c b/h5_test/tst_h_vl.c index 14bf68fba..16ac9e3db 100644 --- a/h5_test/tst_h_vl.c +++ b/h5_test/tst_h_vl.c @@ -7,6 +7,7 @@ #include "h5_err_macros.h" #include +#include #define FILE_NAME "tst_h_vl.h5" #define DIM1_LEN 3 @@ -23,7 +24,7 @@ main() hsize_t dims[1] = {DIM1_LEN}; hvl_t data[DIM1_LEN], data_in[DIM1_LEN]; int *phoney; - int i, j; + size_t i, j; size_t size; /* Create some phoney data, an array of struct s1, which holds a diff --git a/hdf4_test/tst_interops2.c b/hdf4_test/tst_interops2.c index 2b7ff2681..7951dc5e0 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/include/nc4internal.h b/include/nc4internal.h index f9925b1de..9182c6980 100644 --- a/include/nc4internal.h +++ b/include/nc4internal.h @@ -131,7 +131,7 @@ typedef struct NC_OBJ { NC_SORT sort; /**< Type of object. */ char* name; /**< Name, assumed to be null terminated. */ - size_t id; /**< This objects ID. */ + int id; /**< This objects ID. */ } NC_OBJ; /** diff --git a/include/ncpathmgr.h b/include/ncpathmgr.h index 3f0bcd0ec..91dd38ff0 100644 --- a/include/ncpathmgr.h +++ b/include/ncpathmgr.h @@ -14,6 +14,9 @@ #ifdef HAVE_DIRENT_H #include #endif +#ifdef HAVE_SYS_TYPES_H +#include +#endif #ifdef HAVE_SYS_STAT_H #include #endif diff --git a/include/ncs3sdk.h b/include/ncs3sdk.h index 771faa666..013710ccf 100644 --- a/include/ncs3sdk.h +++ b/include/ncs3sdk.h @@ -6,6 +6,9 @@ #ifndef NCS3SDK_H #define NCS3SDK_H 1 +#define AWSHOST ".amazonaws.com" +#define GOOGLEHOST "storage.googleapis.com" + /* Track the server type, if known */ typedef enum NCS3SVC {NCS3UNK=0, /* unknown */ NCS3=1, /* s3.amazon.aws */ diff --git a/include/nctestserver.h b/include/nctestserver.h index 472d942ed..033d24e65 100644 --- a/include/nctestserver.h +++ b/include/nctestserver.h @@ -52,7 +52,7 @@ parseServers(const char* remotetestservers) /* Keep LGTM quiet */ if(rtslen > MAXREMOTETESTSERVERS) goto done; - list = (char**)malloc(sizeof(char*) * (int)(rtslen/2)); + list = (char**)malloc(sizeof(char*) * (rtslen/2)); if(list == NULL) return NULL; rts = strdup(remotetestservers); if(rts == NULL) goto done; diff --git a/include/nctime.h b/include/nctime.h index bbab97228..04419efe8 100644 --- a/include/nctime.h +++ b/include/nctime.h @@ -150,14 +150,14 @@ typedef struct timeinfo_t { # define MSC_NCTIME_EXTRA __declspec(dllimport) # endif -MSC_NCTIME_EXTRA extern void cdRel2Iso(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime); +MSC_NCTIME_EXTRA extern void cdRel2Iso(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime, size_t chartime_size); MSC_NCTIME_EXTRA extern void cdChar2Comp(cdCalenType timetype, char* chartime, cdCompTime* comptime); MSC_NCTIME_EXTRA extern void Cdh2e(CdTime *htime, double *etime); MSC_NCTIME_EXTRA extern void Cde2h(double etime, CdTimeType timeType, long baseYear, CdTime *htime); MSC_NCTIME_EXTRA extern int cdParseRelunits(cdCalenType timetype, char* relunits, cdUnitTime* unit, cdCompTime* base_comptime); MSC_NCTIME_EXTRA extern int cdSetErrOpts(int opts); #else -extern void cdRel2Iso(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime); +extern void cdRel2Iso(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime, size_t chartime_size); extern void cdChar2Comp(cdCalenType timetype, char* chartime, cdCompTime* comptime); extern void Cdh2e(CdTime *htime, double *etime); extern void Cde2h(double etime, CdTimeType timeType, long baseYear, CdTime *htime); diff --git a/libdap2/dapdump.c b/libdap2/dapdump.c index 611491490..1d77f6ef7 100644 --- a/libdap2/dapdump.c +++ b/libdap2/dapdump.c @@ -38,7 +38,7 @@ dumpmetadata(int ncid, NChdr** hdrp) fprintf(stdout,"ncid=%d ngatts=%d ndims=%d nvars=%d unlimid=%d\n", hdr->ncid,hdr->ngatts,hdr->ndims,hdr->nvars,hdr->unlimid); #endif - hdr->gatts = (NCattribute*)calloc(1,hdr->ngatts*sizeof(NCattribute)); + hdr->gatts = (NCattribute*)calloc(1, (size_t)hdr->ngatts*sizeof(NCattribute)); MEMCHECK(hdr->gatts,NC_ENOMEM); if(hdr->ngatts > 0) fprintf(stdout,"global attributes:\n"); @@ -81,7 +81,7 @@ dumpmetadata(int ncid, NChdr** hdrp) fprintf(stdout,"\n"); } - hdr->dims = (Dim*)malloc(hdr->ndims*sizeof(Dim)); + hdr->dims = (Dim*)malloc((size_t)hdr->ndims*sizeof(Dim)); MEMCHECK(hdr->dims,NC_ENOMEM); for(i=0;indims;i++) { hdr->dims[i].dimid = i; @@ -93,7 +93,7 @@ dumpmetadata(int ncid, NChdr** hdrp) fprintf(stdout,"dim[%d]: name=%s size=%lu\n", i,hdr->dims[i].name,(unsigned long)hdr->dims[i].size); } - hdr->vars = (Var*)malloc(hdr->nvars*sizeof(Var)); + hdr->vars = (Var*)malloc((size_t)hdr->nvars*sizeof(Var)); MEMCHECK(hdr->vars,NC_ENOMEM); for(i=0;invars;i++) { Var* var = &hdr->vars[i]; @@ -118,7 +118,7 @@ dumpmetadata(int ncid, NChdr** hdrp) fprintf(stdout," %d",var->dimids[j]); } fprintf(stdout,"}\n"); - var->atts = (NCattribute*)malloc(var->natts*sizeof(NCattribute)); + var->atts = (NCattribute*)malloc((size_t)var->natts*sizeof(NCattribute)); MEMCHECK(var->atts,NC_ENOMEM); for(j=0;jnatts;j++) { NCattribute* att = &var->atts[j]; diff --git a/libdap2/dapodom.c b/libdap2/dapodom.c index 2c6611a98..c8d4a2239 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/daputil.c b/libdap2/daputil.c index bdf533d45..67a472d3e 100644 --- a/libdap2/daputil.c +++ b/libdap2/daputil.c @@ -4,6 +4,7 @@ *********************************************************************/ #include "config.h" +#include #ifdef HAVE_SYS_TIME_H #include @@ -763,7 +764,7 @@ repairname(const char* name, const char* badchars) const char *p; char *q; int c; - int nnlen = 0; + size_t nnlen = 0; if(name == NULL) return NULL; nnlen = (3*strlen(name)); /* max needed */ diff --git a/libdap2/dcetab.c b/libdap2/dcetab.c index 60a602449..4b4adf465 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/d4file.c b/libdap4/d4file.c index 46c1a0fdb..a3da5d8ce 100644 --- a/libdap4/d4file.c +++ b/libdap4/d4file.c @@ -7,6 +7,7 @@ #include "ncd4dispatch.h" #include "d4includes.h" #include "d4curlfunctions.h" +#include #ifdef _MSC_VER #include @@ -301,7 +302,7 @@ set_curl_properties(NCD4INFO* d4info) /* If no cookie file was defined, define a default */ char* path = NULL; char* newpath = NULL; - int len; + size_t len; errno = 0; NCglobalstate* globalstate = NC_getglobalstate(); diff --git a/libdap4/d4meta.c b/libdap4/d4meta.c index 1cea62f61..3c19eed40 100644 --- a/libdap4/d4meta.c +++ b/libdap4/d4meta.c @@ -5,6 +5,7 @@ #include "d4includes.h" #include +#include #include "nc4internal.h" #include "ncoffsets.h" @@ -603,8 +604,8 @@ savevarbyid(NCD4node* group, NCD4node* var) { if(group->group.varbyid == NULL) group->group.varbyid = nclistnew(); - nclistsetalloc(group->group.varbyid,var->meta.id); - nclistinsert(group->group.varbyid,var->meta.id,var); + nclistsetalloc(group->group.varbyid, (size_t)var->meta.id); + nclistinsert(group->group.varbyid, (size_t)var->meta.id,var); } /* Collect FQN path from var node up to and including @@ -730,7 +731,7 @@ compileAttrValues(NCD4meta* builder, NCD4node* attr, void** memoryp, NClist* blo NCD4node* container = attr->container; NCD4node* basetype = attr->basetype; NClist* values = attr->attr.values; - int count = nclistlength(values); + size_t count = nclistlength(values); memset((void*)&converter,0,sizeof(converter)); diff --git a/libdap4/d4odom.c b/libdap4/d4odom.c index 1b7f65081..ed7d4639f 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/CMakeLists.txt b/libdispatch/CMakeLists.txt index 98b90bc96..1d867369b 100644 --- a/libdispatch/CMakeLists.txt +++ b/libdispatch/CMakeLists.txt @@ -51,7 +51,9 @@ IF(ENABLE_S3) ENDIF() ENDIF() -BUILD_BIN_TEST(ncrandom) +IF(ENABLE_TESTS) + BUILD_BIN_TEST(ncrandom) +ENDIF() FILE(GLOB CUR_EXTRA_DIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.c) SET(CUR_EXTRA_DIST ${CUR_EXTRA_DIST} CMakeLists.txt Makefile.am) diff --git a/libdispatch/dcopy.c b/libdispatch/dcopy.c index 2491a92b6..1f0ff034a 100644 --- a/libdispatch/dcopy.c +++ b/libdispatch/dcopy.c @@ -694,7 +694,7 @@ searchgrouptree(int ncid1, int tid1, int grp, int* tid2) int gid; uintptr_t id; - id = grp; + id = (uintptr_t)grp; nclistpush(queue,(void*)id); /* prime the queue */ while(nclistlength(queue) > 0) { id = (uintptr_t)nclistremove(queue,0); @@ -712,7 +712,7 @@ searchgrouptree(int ncid1, int tid1, int grp, int* tid2) goto done; /* push onto the end of the queue */ for(i=0;ientries = (NCexentry*)calloc(map->leaflen,sizeof(NCexentry))) == NULL) + if((leaf->entries = (NCexentry*)calloc((size_t)map->leaflen, sizeof(NCexentry))) == NULL) {stat = NC_ENOMEM; goto done;} leaf->active = 0; @@ -588,7 +588,7 @@ exhashnewleaf(NCexhashmap* map, NCexleaf** leafp) if((leaf = calloc(1,sizeof(NCexleaf))) == NULL) goto done; assert(map->leaflen > 0); - if((leaf->entries = calloc(map->leaflen,sizeof(NCexentry))) == NULL) + if((leaf->entries = calloc((size_t)map->leaflen, sizeof(NCexentry))) == NULL) goto done; leaf->uid = map->uid++; *leafp = leaf; leaf = NULL; diff --git a/libdispatch/nch5s3comms.c b/libdispatch/nch5s3comms.c index ba09b931b..e31eeb9e9 100644 --- a/libdispatch/nch5s3comms.c +++ b/libdispatch/nch5s3comms.c @@ -1152,7 +1152,7 @@ NCH5_s3comms_s3r_open(const char* root, NCS3SVC svc, const char *region, const c /* Compute the signing key */ if (SUCCEED != NCH5_s3comms_signing_key(&signing_key, access_key, region, iso8601now)) HGOTO_ERROR(H5E_ARGS, NC_EINVAL, NULL, "problem in NCH5_s3comms_s3comms_signing_key."); - if (nulllen(signing_key)==0) + if (signing_key == NULL) HGOTO_ERROR(H5E_ARGS, NC_EAUTH, NULL, "signing key cannot be null."); handle->signing_key = signing_key; signing_key = NULL; @@ -2033,7 +2033,7 @@ NCH5_s3comms_signing_key(unsigned char **mdp, const char *secret, const char *re if ((size_t)ret != (AWS4_secret_len - 1)) HGOTO_ERRORVA(H5E_ARGS, NC_EINVAL, FAIL, "problem writing AWS4+secret `%s`", secret); - if((md = (unsigned char*)malloc(SHA256_DIGEST_LENGTH))==NULL) + if((md = (unsigned char*)calloc(1,SHA256_DIGEST_LENGTH))==NULL) HGOTO_ERROR(H5E_ARGS, NC_ENOMEM, NULL, "could not malloc space for signing key ."); /* hash_func, key, len(key), msg, len(msg), digest_dest, digest_len_dest diff --git a/libdispatch/ncjson.c b/libdispatch/ncjson.c index 8c193eb4a..acbe3e92b 100644 --- a/libdispatch/ncjson.c +++ b/libdispatch/ncjson.c @@ -798,9 +798,9 @@ listappend(struct NCjlist* list, NCjson* json) list->contents[0] = json; list->len++; } else { - if((newcontents = (NCjson**)calloc((2*list->len)+1,sizeof(NCjson*)))==NULL) + if((newcontents = (NCjson**)calloc((size_t)(2*list->len)+1,sizeof(NCjson*)))==NULL) {stat = NCJTHROW(NCJ_ERR); goto done;} - memcpy(newcontents,list->contents,list->len*sizeof(NCjson*)); + memcpy(newcontents,list->contents, (size_t)list->len*sizeof(NCjson*)); newcontents[list->len] = json; list->len++; free(list->contents); diff --git a/libdispatch/nclist.c b/libdispatch/nclist.c index f2c3f4d47..5ed21753e 100644 --- a/libdispatch/nclist.c +++ b/libdispatch/nclist.c @@ -31,7 +31,7 @@ NClist* nclistnew(void) ncinitialized = 1; } */ - l = (NClist*)malloc(sizeof(NClist)); + l = (NClist*)calloc(1,sizeof(NClist)); if(l) { l->alloc=0; l->length=0; @@ -286,10 +286,13 @@ done: void* nclistextract(NClist* l) { - void* result = l->content; + void* result = NULL; + if(l) { + result = l->content; l->alloc = 0; l->length = 0; l->content = NULL; + } return result; } diff --git a/libdispatch/nctime.c b/libdispatch/nctime.c index e265ba1f4..b92eb4b5d 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); } } @@ -1085,7 +1085,7 @@ cdRel2Comp(cdCalenType timetype, char* relunits, double reltime, cdCompTime* com /* rkr: output as ISO 8601 strings */ static void -cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time) +cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time, size_t time_size) { double dtmp, sec; int ihr, imin, isec; @@ -1121,23 +1121,23 @@ cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time) if(timetype & cdStandardCal){ switch (nskip) { case 0: /* sec != 0 && (int)sec != sec */ - sprintf(time,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d:%lf", + snprintf(time,time_size,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d:%lf", comptime.year,comptime.month,comptime.day,separator,ihr,imin,sec); break; case 1: - sprintf(time,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d:%2.2d", + snprintf(time,time_size,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d:%2.2d", comptime.year,comptime.month,comptime.day,separator,ihr,imin,isec); break; case 2: - sprintf(time,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d", + snprintf(time,time_size,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d", comptime.year,comptime.month,comptime.day,separator,ihr,imin); break; case 3: - sprintf(time,"%4.4ld-%2.2hd-%2.2hd%c%2.2d", + snprintf(time,time_size,"%4.4ld-%2.2hd-%2.2hd%c%2.2d", comptime.year,comptime.month,comptime.day,separator,ihr); break; case 4: - sprintf(time,"%4.4ld-%2.2hd-%2.2hd", + snprintf(time,time_size,"%4.4ld-%2.2hd-%2.2hd", comptime.year,comptime.month,comptime.day); break; } @@ -1145,23 +1145,23 @@ cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time) else { /* Climatological */ switch (nskip) { case 0: /* sec != 0 && (int)sec != sec */ - sprintf(time,"%2.2hd-%2.2hd%c%2.2d:%2.2d:%lf", + snprintf(time,time_size,"%2.2hd-%2.2hd%c%2.2d:%2.2d:%lf", comptime.month,comptime.day,separator,ihr,imin,sec); break; case 1: - sprintf(time,"%2.2hd-%2.2hd%c%2.2d:%2.2d:%2.2d", + snprintf(time,time_size,"%2.2hd-%2.2hd%c%2.2d:%2.2d:%2.2d", comptime.month,comptime.day,separator,ihr,imin,isec); break; case 2: - sprintf(time,"%2.2hd-%2.2hd%c%2.2d:%2.2d", + snprintf(time,time_size,"%2.2hd-%2.2hd%c%2.2d:%2.2d", comptime.month,comptime.day,separator,ihr,imin); break; case 3: - sprintf(time,"%2.2hd-%2.2hd%c%2.2d", + snprintf(time,time_size,"%2.2hd-%2.2hd%c%2.2d", comptime.month,comptime.day,separator,ihr); break; case 4: - sprintf(time,"%2.2hd-%2.2hd", + snprintf(time,time_size,"%2.2hd-%2.2hd", comptime.month,comptime.day); break; } @@ -1171,12 +1171,12 @@ cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time) /* rkr: added for output closer to ISO 8601 */ void -cdRel2Iso(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime) +cdRel2Iso(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime, size_t chartime_size) { cdCompTime comptime; cdRel2Comp(timetype, relunits, reltime, &comptime); - cdComp2Iso(timetype, separator, comptime, chartime); + cdComp2Iso(timetype, separator, comptime, chartime, chartime_size); return; } diff --git a/libdispatch/utf8proc.c b/libdispatch/utf8proc.c index 68d8dd4a2..d0b037ade 100644 --- a/libdispatch/utf8proc.c +++ b/libdispatch/utf8proc.c @@ -713,7 +713,7 @@ static nc_utf8proc_ssize_t nc_seqindex_write_char_decomposed(nc_utf8proc_uint16_ *dstptr = NULL; result = nc_utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data); if (result < 0) return result; - buffer = (nc_utf8proc_int32_t *) malloc(result * sizeof(nc_utf8proc_int32_t) + 1); + buffer = (nc_utf8proc_int32_t *) malloc((size_t)result * sizeof(nc_utf8proc_int32_t) + 1); if (!buffer) return UTF8PROC_ERROR_NOMEM; result = nc_utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data); if (result < 0) { diff --git a/libhdf5/H5FDhttp.c b/libhdf5/H5FDhttp.c index c6658f46f..d48aeb8c3 100644 --- a/libhdf5/H5FDhttp.c +++ b/libhdf5/H5FDhttp.c @@ -239,6 +239,31 @@ H5FD_http_init(void) return H5FD_HTTP_g; } /* end H5FD_http_init() */ + +/*------------------------------------------------------------------------- + * Function: H5FD_http_finalize + * + * Purpose: Free this driver by unregistering the driver with the + * library. + * + * Returns: Non-negative on success or negative on failure + * + * Programmer: John Donoghue + * Tuesday, December 12, 2023 + * + *------------------------------------------------------------------------- + */ +EXTERNL hid_t +H5FD_http_finalize(void) +{ + /* Reset VFL ID */ + if (H5FD_HTTP_g) + H5FDunregister(H5FD_HTTP_g); + H5FD_HTTP_g = 0; + + return H5FD_HTTP_g; +} /* end H5FD_http_finalize() */ + /*--------------------------------------------------------------------------- * Function: H5FD_http_term @@ -256,9 +281,6 @@ H5FD_http_init(void) static herr_t H5FD_http_term(void) { - /* Reset VFL ID */ - H5FD_HTTP_g = 0; - return 0; } /* end H5FD_http_term() */ #endif diff --git a/libhdf5/H5FDhttp.h b/libhdf5/H5FDhttp.h index f3c46dca5..ee3d28370 100644 --- a/libhdf5/H5FDhttp.h +++ b/libhdf5/H5FDhttp.h @@ -44,9 +44,11 @@ extern "C" { #if 0 H5_DLL hid_t H5FD_http_init(void); +H5_DLL hid_t H5FD_http_finalize(void); H5_DLL herr_t H5Pset_fapl_http(hid_t fapl_id); #else EXTERNL hid_t H5FD_http_init(void); +EXTERNL hid_t H5FD_http_finalize(void); EXTERNL herr_t H5Pset_fapl_http(hid_t fapl_id); #endif diff --git a/libhdf5/hdf5dispatch.c b/libhdf5/hdf5dispatch.c index 130f0f076..1e8f980da 100644 --- a/libhdf5/hdf5dispatch.c +++ b/libhdf5/hdf5dispatch.c @@ -142,6 +142,9 @@ NC_HDF5_initialize(void) int NC_HDF5_finalize(void) { +#ifdef ENABLE_BYTERANGE + (void)H5FD_http_finalize(); +#endif (void)nc4_hdf5_finalize(); return NC_NOERR; } diff --git a/libhdf5/hdf5internal.c b/libhdf5/hdf5internal.c index 5532ed0c1..2f4e5792a 100644 --- a/libhdf5/hdf5internal.c +++ b/libhdf5/hdf5internal.c @@ -150,9 +150,9 @@ find_var_dim_max_length(NC_GRP_INFO_T *grp, int varid, int dimid, BAIL(NC_EHDFERR); if (dataset_ndims != var->ndims) BAIL(NC_EHDFERR); - if (!(h5dimlen = malloc(dataset_ndims * sizeof(hsize_t)))) + if (!(h5dimlen = malloc((size_t)dataset_ndims * sizeof(hsize_t)))) BAIL(NC_ENOMEM); - if (!(h5dimlenmax = malloc(dataset_ndims * sizeof(hsize_t)))) + if (!(h5dimlenmax = malloc((size_t)dataset_ndims * sizeof(hsize_t)))) BAIL(NC_ENOMEM); if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid, h5dimlen, h5dimlenmax)) < 0) diff --git a/libhdf5/hdf5open.c b/libhdf5/hdf5open.c index 0c2f5b523..4459fda30 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. */ @@ -1365,9 +1365,9 @@ get_attached_info(NC_VAR_INFO_T *var, NC_HDF5_VAR_INFO_T *hdf5_var, int ndims, * attached for each dimension, and the HDF5 object IDs of the * scale(s). */ assert(!hdf5_var->dimscale_hdf5_objids); - if (!(hdf5_var->dimscale_attached = calloc(ndims, sizeof(nc_bool_t)))) + if (!(hdf5_var->dimscale_attached = calloc((size_t)ndims, sizeof(nc_bool_t)))) return NC_ENOMEM; - if (!(hdf5_var->dimscale_hdf5_objids = malloc(ndims * + if (!(hdf5_var->dimscale_hdf5_objids = malloc((size_t)ndims * sizeof(struct hdf5_objid)))) return NC_ENOMEM; @@ -1886,7 +1886,7 @@ read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att) &type_size))) return retval; { - if (!(att->data = malloc((unsigned int)(att->len * type_size)))) + if (!(att->data = malloc((unsigned int)((size_t)att->len * type_size)))) BAIL(NC_ENOMEM); /* For a fixed length HDF5 string, the read requires @@ -1907,7 +1907,7 @@ read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att) char** dst = NULL; /* Alloc space for the contiguous memory read. */ - if (!(contig_buf = malloc(att->len * fixed_size * sizeof(char)))) + if (!(contig_buf = malloc((size_t)att->len * fixed_size * sizeof(char)))) BAIL(NC_ENOMEM); /* Read the fixed-len strings as one big block. */ @@ -2088,7 +2088,7 @@ read_type(NC_GRP_INFO_T *grp, hid_t hdf_typeid, char *type_name) return NC_EHDFERR; LOG((5, "compound type has %d members", nmembers)); type->u.c.field = nclistnew(); - nclistsetalloc(type->u.c.field,nmembers); + nclistsetalloc(type->u.c.field, (size_t)nmembers); for (m = 0; m < nmembers; m++) { @@ -2253,7 +2253,7 @@ read_type(NC_GRP_INFO_T *grp, hid_t hdf_typeid, char *type_name) if ((nmembers = H5Tget_nmembers(hdf_typeid)) < 0) return NC_EHDFERR; type->u.e.enum_member = nclistnew(); - nclistsetalloc(type->u.e.enum_member,nmembers); + nclistsetalloc(type->u.e.enum_member, (size_t)nmembers); /* Allocate space for one value. */ if (!(value = calloc(1, type_size))) diff --git a/libhdf5/hdf5var.c b/libhdf5/hdf5var.c index 6d37fe909..2b44ee28e 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; } @@ -433,7 +434,7 @@ NC4_def_var(int ncid, const char *name, nc_type xtype, int ndims, * remember whether dimension scales have been attached to each * dimension. */ if (!hdf5_var->dimscale && ndims) - if (!(hdf5_var->dimscale_attached = calloc(ndims, sizeof(nc_bool_t)))) + if (!(hdf5_var->dimscale_attached = calloc((size_t)ndims, sizeof(nc_bool_t)))) BAIL(NC_ENOMEM); /* Return the varid. */ @@ -1541,11 +1542,12 @@ NC4_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, NC_VAR_INFO_T *var; NC_DIM_INFO_T *dim; NC_HDF5_VAR_INFO_T *hdf5_var; + herr_t herr; hid_t file_spaceid = 0, mem_spaceid = 0, xfer_plistid = 0; long long unsigned xtend_size[NC_MAX_VAR_DIMS]; hsize_t fdims[NC_MAX_VAR_DIMS], fmaxdims[NC_MAX_VAR_DIMS]; hsize_t start[NC_MAX_VAR_DIMS], count[NC_MAX_VAR_DIMS]; - hsize_t stride[NC_MAX_VAR_DIMS]; + hsize_t stride[NC_MAX_VAR_DIMS], ones[NC_MAX_VAR_DIMS]; int need_to_extend = 0; #ifdef USE_PARALLEL4 int extend_possible = 0; @@ -1596,6 +1598,7 @@ NC4_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, start[i] = startp[i]; count[i] = countp ? countp[i] : var->dim[i]->len; stride[i] = stridep ? stridep[i] : 1; + ones[i] = 1; LOG((4, "start[%d] %ld count[%d] %ld stride[%d] %ld", i, start[i], i, count[i], i, stride[i])); /* Check to see if any counts are zero. */ @@ -1646,8 +1649,13 @@ NC4_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, } else { - if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, stride, - count, NULL) < 0) + if (stridep == NULL) + herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, + NULL, ones, count); + else + herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, + stride, count, NULL); + if (herr < 0) BAIL(NC_EHDFERR); /* Create a space for the memory, just big enough to hold the slab @@ -1772,8 +1780,14 @@ NC4_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, BAIL2(NC_EHDFERR); if ((file_spaceid = H5Dget_space(hdf5_var->hdf_datasetid)) < 0) BAIL(NC_EHDFERR); - if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, - start, stride, count, NULL) < 0) + + if (stridep == NULL) + herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, + start, NULL, ones, count); + else + herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, + start, stride, count, NULL); + if (herr < 0) BAIL(NC_EHDFERR); } } @@ -1872,7 +1886,7 @@ NC4_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp, hsize_t count[NC_MAX_VAR_DIMS]; hsize_t fdims[NC_MAX_VAR_DIMS], fmaxdims[NC_MAX_VAR_DIMS]; hsize_t start[NC_MAX_VAR_DIMS]; - hsize_t stride[NC_MAX_VAR_DIMS]; + hsize_t stride[NC_MAX_VAR_DIMS], ones[NC_MAX_VAR_DIMS]; void *fillvalue = NULL; int no_read = 0, provide_fill = 0; hssize_t fill_value_size[NC_MAX_VAR_DIMS]; @@ -1923,6 +1937,7 @@ NC4_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp, start[i] = startp[i]; count[i] = countp[i]; stride[i] = stridep ? stridep[i] : 1; + ones[i] = 1; /* if any of the count values are zero don't actually read. */ if (count[i] == 0) @@ -2052,9 +2067,16 @@ NC4_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp, } else { - if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, - start, stride, count, NULL) < 0) + herr_t herr; + if (stridep == NULL) + herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, + start, NULL, ones, count); + else + herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, + start, stride, count, NULL); + if (herr < 0) BAIL(NC_EHDFERR); + /* Create a space for the memory, just big enough to hold the slab we want. */ if ((mem_spaceid = H5Screate_simple(var->ndims, count, NULL)) < 0) diff --git a/libhdf5/nc4hdf.c b/libhdf5/nc4hdf.c index a84abd022..89cc5f973 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/liblib/CMakeLists.txt b/liblib/CMakeLists.txt index 1a1a9ef6d..4e54e0081 100644 --- a/liblib/CMakeLists.txt +++ b/liblib/CMakeLists.txt @@ -57,6 +57,10 @@ IF(STATUS_PARALLEL) target_link_libraries(netcdf MPI::MPI_C) ENDIF(STATUS_PARALLEL) +IF(TARGET MPI::MPI_C) + target_link_libraries(netcdf MPI::MPI_C) +ENDIF(TARGET MPI::MPI_C) + IF(MOD_NETCDF_NAME) SET_TARGET_PROPERTIES(netcdf PROPERTIES LIBRARY_OUTPUT_NAME ${NETCDF_LIB_NAME}) SET_TARGET_PROPERTIES(netcdf PROPERTIES ARCHIVE_OUTPUT_NAME ${NETCDF_LIB_NAME}) @@ -96,7 +100,7 @@ IF(Bz2_FOUND) SET(TLL_LIBS ${TLL_LIBS} ${Bz2_LIBRARIES}) ENDIF() IF(SZIP_FOUND) -SET(TLL_LIBS ${TLL_LIBS} ${SZIP_LIBRARIES}) +SET(TLL_LIBS ${TLL_LIBS} ${Szip_LIBRARY}) ENDIF() IF(HAVE_LIBDL) @@ -121,9 +125,9 @@ IF(USE_HDF5) # builds: # Make sure that HDF5_C_LIBRARY appears *after* # HDF5_HL_LIBRARY. - SET(TLL_LIBS ${HDF5_HL_LIBRARIES} ${HDF5_C_LIBRARIES} ${HDF5_LIBRARIES} ${TLL_LIBS} ${SZIP_LIBRARY}) + SET(TLL_LIBS ${HDF5_HL_LIBRARIES} ${HDF5_C_LIBRARIES} ${HDF5_LIBRARIES} ${TLL_LIBS} ${Szip_LIBRARY}) ELSE() # Windows CMake defines HDF5_LIBRARIES. - SET(TLL_LIBS ${HDF5_LIBRARIES} ${TLL_LIBS} ${SZIP_LIBRARY}) + SET(TLL_LIBS ${HDF5_LIBRARIES} ${TLL_LIBS} ${Szip_LIBRARY}) ENDIF() ENDIF() diff --git a/libncpoco/CMakeLists.txt b/libncpoco/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/libncpoco/COPYRIGHT b/libncpoco/COPYRIGHT old mode 100755 new mode 100644 diff --git a/libncpoco/Makefile.am b/libncpoco/Makefile.am old mode 100755 new mode 100644 diff --git a/libncpoco/cp_test.c b/libncpoco/cp_test.c old mode 100755 new mode 100644 diff --git a/libncpoco/cp_unix.c b/libncpoco/cp_unix.c old mode 100755 new mode 100644 index 3ccea2ed0..7ff01f79e --- a/libncpoco/cp_unix.c +++ b/libncpoco/cp_unix.c @@ -46,7 +46,7 @@ static void ncperr(const char* fcn, NCPSharedLib* lib) { const char* msg = dlerror(); - lib->err.msg[0] = '\0'; + memset(lib->err.msg,0,sizeof(lib->err.msg)); if(msg != NULL) { strlcat(lib->err.msg,fcn,sizeof(lib->err.msg)); strlcat(lib->err.msg,": ",sizeof(lib->err.msg)); diff --git a/libncpoco/cp_win32.c b/libncpoco/cp_win32.c old mode 100755 new mode 100644 index b4284ac10..896344f63 --- a/libncpoco/cp_win32.c +++ b/libncpoco/cp_win32.c @@ -105,10 +105,9 @@ load(NCPSharedLib* lib , const char* path0, int flags) char* msg = NULL; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &msg, 0, NULL); - if(msg) { + memset(lib->err.msg,0,sizeof(lib->err.msg)); + if(msg) strncpy(lib->err.msg,msg,sizeof(lib->err.msg)); - } else - lib->err.msg[0] = '\0'; ret = NC_ENOTFOUND; goto ldone; } diff --git a/libncpoco/cptestlib.c b/libncpoco/cptestlib.c old mode 100755 new mode 100644 diff --git a/libncpoco/ncpoco.c b/libncpoco/ncpoco.c old mode 100755 new mode 100644 index 12d4b41ee..b12b2e0a9 --- a/libncpoco/ncpoco.c +++ b/libncpoco/ncpoco.c @@ -68,6 +68,7 @@ EXTERNL int ncpload(NCPSharedLib* lib, const char* path, int flags) { if(lib == NULL || path == NULL) return NC_EINVAL; + ncpclearerrmsg(lib); return lib->api.load(lib,path,flags); } @@ -75,6 +76,7 @@ EXTERNL int ncpunload(NCPSharedLib* lib) /* Unloads a shared library. */ { if(lib == NULL) return NC_EINVAL; + ncpclearerrmsg(lib); return lib->api.unload(lib); } @@ -93,6 +95,7 @@ EXTERNL void* ncpgetsymbol(NCPSharedLib* lib,const char* name) { if(lib == NULL) return NULL; + ncpclearerrmsg(lib); return lib->api.getsymbol(lib,name); } @@ -113,3 +116,11 @@ ncpgeterrmsg(NCPSharedLib* lib) if(lib == NULL) return NULL; return (lib->err.msg[0] == '\0' ? NULL : lib->err.msg); } + +/* Clear the last err msg. */ +EXTERNL void +ncpclearerrmsg(NCPSharedLib* lib) +{ + if(lib == NULL) return; + memset(lib->err.msg,0,sizeof(lib->err.msg)); +} diff --git a/libncpoco/ncpoco.h b/libncpoco/ncpoco.h old mode 100755 new mode 100644 index 43fb077ba..fc1e993b9 --- a/libncpoco/ncpoco.h +++ b/libncpoco/ncpoco.h @@ -71,6 +71,9 @@ EXTERNL const char* ncpgetpath(NCPSharedLib*); /* Return last err msg */ EXTERNL const char* ncpgeterrmsg(NCPSharedLib* lib); +/* Clear the last err msg. */ +EXTERNL void ncpclearerrmsg(NCPSharedLib* lib); + EXTERNL const char* intstr(int err1); #endif /*NCPOCO_H*/ diff --git a/libncxml/ncxml_xml2.c b/libncxml/ncxml_xml2.c index c32b1cc85..12132a755 100644 --- a/libncxml/ncxml_xml2.c +++ b/libncxml/ncxml_xml2.c @@ -1,5 +1,6 @@ /* Copyright 2018-2018 University Corporation for Atmospheric Research/Unidata. */ +#include #include #include #include @@ -139,7 +140,8 @@ ncxml_attr_pairs(ncxml_t xml0, char*** pairsp) char** pairs = NULL; xmlNode* xml = (xmlNode*)xml0; xmlAttr* attr = NULL; - int i,count = 0; + int i; + size_t count = 0; if(xml == NULL) return 0; /* First count */ diff --git a/libnczarr/zmap.c b/libnczarr/zmap.c index eb808d431..f30c321d0 100644 --- a/libnczarr/zmap.c +++ b/libnczarr/zmap.c @@ -322,8 +322,8 @@ nczm_divide_at(const char* key, int nsegs, char** prefixp, char** suffixp) /* p should point at the presegs+1 start point */ delta = (p-key); if(prefixp) { - prefix = malloc(delta+1); - memcpy(prefix,key,delta); + prefix = malloc((size_t)delta+1); + memcpy(prefix,key,(size_t)delta); prefix[delta] = '\0'; *prefixp = prefix; } @@ -436,7 +436,7 @@ nczm_segment1(const char* path, char** seg1p) q = strchr(p,'/'); if(q == NULL) q = p+strlen(p); /* point to stop character */ delta = (q-p); - if((seg1 = (char*)malloc(delta+1))==NULL) + if((seg1 = (char*)malloc((size_t)delta+1))==NULL) {ret = NC_ENOMEM; goto done;} memcpy(seg1,p,delta); seg1[delta] = '\0'; @@ -489,9 +489,9 @@ nczm_basename(const char* path, char** basep) p = strrchr(last,'.'); if(p == NULL) p = last+strlen(last); delta = (p - last); - if((base = (char*)malloc(delta+1))==NULL) + if((base = (char*)malloc((size_t)delta+1))==NULL) {stat = NC_ENOMEM; goto done;} - memcpy(base,last,delta); + memcpy(base,last,(size_t)delta); base[delta] = '\0'; if(basep) {*basep = base; base = NULL;} done: diff --git a/libnczarr/zmap_file.c b/libnczarr/zmap_file.c old mode 100755 new mode 100644 diff --git a/libnczarr/zmap_zip.c b/libnczarr/zmap_zip.c old mode 100755 new mode 100644 diff --git a/libnczarr/zodom.c b/libnczarr/zodom.c index 7cde2834e..21e920412 100644 --- a/libnczarr/zodom.c +++ b/libnczarr/zodom.c @@ -131,11 +131,11 @@ buildodom(int rank, NCZOdometer** odomp) if((odom = calloc(1,sizeof(NCZOdometer))) == NULL) goto done; odom->rank = rank; - if((odom->start=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem; - if((odom->stop=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem; - if((odom->stride=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem; - if((odom->len=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem; - if((odom->index=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem; + if((odom->start=calloc(1,(sizeof(size64_t)*(size_t)rank)))==NULL) goto nomem; + if((odom->stop=calloc(1,(sizeof(size64_t)*(size_t)rank)))==NULL) goto nomem; + if((odom->stride=calloc(1,(sizeof(size64_t)*(size_t)rank)))==NULL) goto nomem; + if((odom->len=calloc(1,(sizeof(size64_t)*(size_t)rank)))==NULL) goto nomem; + if((odom->index=calloc(1,(sizeof(size64_t)*(size_t)rank)))==NULL) goto nomem; *odomp = odom; odom = NULL; } done: diff --git a/libnczarr/zsync.c b/libnczarr/zsync.c index 8a14cdc69..44ed00e28 100644 --- a/libnczarr/zsync.c +++ b/libnczarr/zsync.c @@ -1676,7 +1676,7 @@ define_vars(NC_FILE_INFO_T* file, NC_GRP_INFO_T* grp, NClist* varnames) /* Save the rank of the variable */ if((stat = nc4_var_set_ndims(var, rank))) goto done; /* extract the shapes */ - if((shapes = (size64_t*)malloc(sizeof(size64_t)*zarr_rank)) == NULL) + if((shapes = (size64_t*)malloc(sizeof(size64_t)*(size_t)zarr_rank)) == NULL) {stat = (THROW(NC_ENOMEM)); goto done;} if((stat = decodeints(jvalue, shapes))) goto done; } @@ -1702,7 +1702,7 @@ define_vars(NC_FILE_INFO_T* file, NC_GRP_INFO_T* grp, NClist* varnames) var->storage = NC_CHUNKED; if(var->ndims != rank) {stat = (THROW(NC_ENCZARR)); goto done;} - if((var->chunksizes = malloc(sizeof(size_t)*zarr_rank)) == NULL) + if((var->chunksizes = malloc(sizeof(size_t)*(size_t)zarr_rank)) == NULL) {stat = NC_ENOMEM; goto done;} if((stat = decodeints(jvalue, chunks))) goto done; /* validate the chunk sizes */ diff --git a/libnczarr/zutil.c b/libnczarr/zutil.c index 5e10ffaf9..0fc26580a 100644 --- a/libnczarr/zutil.c +++ b/libnczarr/zutil.c @@ -801,8 +801,8 @@ NCZ_comma_parse(const char* s, NClist* list) endp = strchr(p,','); if(endp == NULL) endp = p + strlen(p); slen = (endp - p); - if((s = malloc(slen+1)) == NULL) {stat = NC_ENOMEM; goto done;} - memcpy(s,p,slen); + if((s = malloc((size_t)slen+1)) == NULL) {stat = NC_ENOMEM; goto done;} + memcpy(s,p,(size_t)slen); s[slen] = '\0'; if(nclistmatch(list,s,0)) { nullfree(s); /* duplicate */ @@ -1013,9 +1013,9 @@ NCZ_fixed2char(const void* fixed, char** charp, size_t count, int maxstrlen) if(p[0] == '\0') { sp = NULL; } else { - if((sp = (unsigned char*)malloc(maxstrlen+1))==NULL) /* ensure null terminated */ + if((sp = (unsigned char*)malloc((size_t)maxstrlen+1))==NULL) /* ensure null terminated */ return NC_ENOMEM; - memcpy(sp,p,maxstrlen); + memcpy(sp,p,(size_t)maxstrlen); sp[maxstrlen] = '\0'; } charp[i] = (char*)sp; diff --git a/libnczarr/zvar.c b/libnczarr/zvar.c index 27e30a9f1..607a35185 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; } @@ -1545,7 +1546,7 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, #endif size64_t fdims[NC_MAX_VAR_DIMS]; size64_t start[NC_MAX_VAR_DIMS], count[NC_MAX_VAR_DIMS]; - size64_t stride[NC_MAX_VAR_DIMS]; + size64_t stride[NC_MAX_VAR_DIMS], ones[NC_MAX_VAR_DIMS]; int retval, range_error = 0, i, d2; void *bufr = NULL; int bufrd = 0; /* 1 => we allocated bufr */ @@ -1590,6 +1591,7 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, start[0] = 0; count[0] = 1; stride[0] = 1; + ones[0] = 1; } else { for (i = 0; i < var->ndims; i++) { @@ -1601,6 +1603,7 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, start[i] = startp[i]; count[i] = countp ? countp[i] : fdims[i]; stride[i] = stridep ? stridep[i] : 1; + ones[i] = 1; /* Check to see if any counts are zero. */ if (!count[i]) @@ -1632,6 +1635,31 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, } } + +#ifdef LOOK + /* Now you would think that no one would be crazy enough to write + a scalar dataspace with one of the array function calls, but you + would be wrong. So let's check to see if the dataset is + scalar. If it is, we won't try to set up a hyperslab. */ + if (H5Sget_simple_extent_type(file_spaceid) == H5S_SCALAR) + { + if ((mem_spaceid = H5Screate(H5S_SCALAR)) < 0) + BAIL(NC_EHDFERR); + } + else + { + if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, stride, + ones, count) < 0) + BAIL(NC_EHDFERR); + + /* Create a space for the memory, just big enough to hold the slab + we want. */ + if ((mem_spaceid = H5Screate_simple(var->ndims, count, NULL)) < 0) + BAIL(NC_EHDFERR); + } +#endif + + /* Are we going to convert any data? (No converting of compound or * opaque or vlen types.) We also need to call this code if we are doing * quantization. */ @@ -1675,6 +1703,7 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, do no harm to reextend it to that size. */ if (var->ndims) { + for (d2 = 0; d2 < var->ndims; d2++) { size64_t endindex = start[d2] + stride[d2] * (count[d2] - 1); /* last index written */ @@ -1698,6 +1727,30 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, } } } + + + +#ifdef LOOK + /* If we need to extend it, we also need a new file_spaceid + to reflect the new size of the space. */ + if (need_to_extend) + { + LOG((4, "extending dataset")); + /* Convert xtend_size back to hsize_t for use with + * H5Dset_extent. */ + for (d2 = 0; d2 < var->ndims; d2++) + fdims[d2] = (size64_t)xtend_size[d2]; + if (H5Dset_extent(ncz_var->hdf_datasetid, fdims) < 0) + BAIL(NC_EHDFERR); + if (file_spaceid > 0 && H5Sclose(file_spaceid) < 0) + BAIL2(NC_EHDFERR); + if ((file_spaceid = H5Dget_space(ncz_var->hdf_datasetid)) < 0) + BAIL(NC_EHDFERR); + if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, + start, stride, ones, count) < 0) + BAIL(NC_EHDFERR); + } +#endif } /* Do we need to convert the data? */ @@ -1800,8 +1853,8 @@ NCZ_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp, size64_t fdims[NC_MAX_VAR_DIMS]; /* size of the dimensions */ size64_t start[NC_MAX_VAR_DIMS]; size64_t stride[NC_MAX_VAR_DIMS]; - int no_read = 0; - int provide_fill = 0; + size64_t ones[NC_MAX_VAR_DIMS]; + int no_read = 0, provide_fill = 0; int fill_value_size[NC_MAX_VAR_DIMS]; int retval, range_error = 0, i, d2; void *bufr = NULL; @@ -1833,6 +1886,7 @@ NCZ_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp, start[0] = 0; count[0] = 1; stride[0] = 1; + ones[0] = 1; } else { for (i = 0; i < var->ndims; i++) { @@ -1842,6 +1896,12 @@ NCZ_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp, start[i] = startp[i]; count[i] = countp[i]; stride[i] = stridep ? stridep[i] : 1; + + ones[i] = 1; + /* if any of the count values are zero don't actually read. */ + if (count[i] == 0) + no_read++; + /* Get dimension sizes also */ fdims[i] = var->dim[i]->len; /* if any of the counts are zero don't actually read. */ @@ -1959,7 +2019,7 @@ NCZ_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp, else { if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, - start, stride, count, NULL) < 0) + start, stride, ones, count) < 0) BAIL(NC_EHDFERR); /* Create a space for the memory, just big enough to hold the slab we want. */ diff --git a/libnczarr/zwalk.c b/libnczarr/zwalk.c index 1a7faba2d..21f9ed943 100644 --- a/libnczarr/zwalk.c +++ b/libnczarr/zwalk.c @@ -586,7 +586,7 @@ NCZ_projectslices(struct Common* common, size64_t stride[NC_MAX_VAR_DIMS]; size64_t len[NC_MAX_VAR_DIMS]; - if((allprojections = calloc(common->rank,sizeof(NCZSliceProjections))) == NULL) + if((allprojections = calloc((size_t)common->rank, sizeof(NCZSliceProjections))) == NULL) {stat = NC_ENOMEM; goto done;} memset(ranges,0,sizeof(ranges)); diff --git a/libnczarr/zxcache.c b/libnczarr/zxcache.c index 7a469587a..d861d86d3 100644 --- a/libnczarr/zxcache.c +++ b/libnczarr/zxcache.c @@ -14,6 +14,7 @@ #include "zcache.h" #include "ncxcache.h" #include "zfilter.h" +#include #undef DEBUG @@ -662,7 +663,7 @@ put_chunk(NCZChunkCache* cache, NCZCacheEntry* entry) /* Convert from char* to char[strlen] format */ int maxstrlen = NCZ_get_maxstrlen((NC_OBJ*)cache->var); assert(maxstrlen > 0); - if((strchunk = malloc(cache->chunkcount*maxstrlen))==NULL) {stat = NC_ENOMEM; goto done;} + if((strchunk = malloc((size_t)cache->chunkcount * (size_t)maxstrlen))==NULL) {stat = NC_ENOMEM; goto done;} /* copy char* to char[] format */ if((stat = NCZ_char2fixed((const char**)entry->data,strchunk,cache->chunkcount,maxstrlen))) goto done; /* Reclaim the old chunk */ diff --git a/libsrc/ffio.c b/libsrc/ffio.c index 80bc6cb93..a59246e9a 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/ncx.m4 b/libsrc/ncx.m4 index a9fb11f41..176ca27cc 100644 --- a/libsrc/ncx.m4 +++ b/libsrc/ncx.m4 @@ -39,7 +39,7 @@ ifdef(`PNETCDF',` `#'if HAVE_CONFIG_H `#'include `#'endif') - +#include #include #include #include diff --git a/libsrc/pstdint.h b/libsrc/pstdint.h index c0b4addb3..98936cc33 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/libsrc/v1hpg.c b/libsrc/v1hpg.c index f93b219bd..d1c668446 100644 --- a/libsrc/v1hpg.c +++ b/libsrc/v1hpg.c @@ -1254,7 +1254,7 @@ NC_computeshapes(NC3_INFO* ncp) for( /*NADA*/; vpp < end; vpp++) { status = NC_var_shape(*vpp, &ncp->dims); - if(status != NC_NOERR) + if(status != NC_NOERR) return(status); if(IS_RECVAR(*vpp)) @@ -1265,13 +1265,13 @@ NC_computeshapes(NC3_INFO* ncp) } else { - if(first_var == NULL) + if(first_var == NULL) first_var = *vpp; - /* - * Overwritten each time thru. - * Usually overwritten in first_rec != NULL clause below. - */ - ncp->begin_rec = (*vpp)->begin + (off_t)(*vpp)->len; + /* + * Overwritten each time thru. + * Usually overwritten in first_rec != NULL clause below. + */ + ncp->begin_rec = (*vpp)->begin + (off_t)(*vpp)->len; } } } diff --git a/libsrc4/nc4attr.c b/libsrc4/nc4attr.c index fed497614..2cb5e5946 100644 --- a/libsrc4/nc4attr.c +++ b/libsrc4/nc4attr.c @@ -111,7 +111,7 @@ nc4_get_att_ptrs(NC_FILE_INFO_T *h5, NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, !(mem_type == NC_CHAR && (att->nc_typeid == NC_UBYTE || att->nc_typeid == NC_BYTE))) { - if (!(bufr = malloc((size_t)(att->len * type_size)))) + if (!(bufr = malloc((size_t)(att->len) * type_size))) BAIL(NC_ENOMEM); need_to_convert++; if ((retval = nc4_convert_type(att->data, bufr, att->nc_typeid, diff --git a/libsrc4/nc4grp.c b/libsrc4/nc4grp.c index f4a757baa..7e97ad9fd 100644 --- a/libsrc4/nc4grp.c +++ b/libsrc4/nc4grp.c @@ -177,9 +177,9 @@ NC4_inq_grpname_full(int ncid, size_t *lenp, char *full_name) ; /* Allocate storage. */ - if (!(name = malloc((g + 1) * (NC_MAX_NAME + 1) + 1))) + if (!(name = malloc((size_t)(g + 1) * (NC_MAX_NAME + 1) + 1))) return NC_ENOMEM; - if (!(gid = malloc((g + 1) * sizeof(int)))) + if (!(gid = malloc((size_t)(g + 1) * sizeof(int)))) { free(name); return NC_ENOMEM; diff --git a/libsrc4/nc4internal.c b/libsrc4/nc4internal.c index 2c6e64d1b..10f090a46 100644 --- a/libsrc4/nc4internal.c +++ b/libsrc4/nc4internal.c @@ -24,6 +24,7 @@ #include "ncdispatch.h" /* from libdispatch */ #include "ncutf8.h" #include +#include #include "ncrc.h" /** @internal Number of reserved attributes. These attributes are @@ -788,14 +789,14 @@ nc4_var_set_ndims(NC_VAR_INFO_T *var, int ndims) /* Allocate space for dimension information. */ if (ndims) { - if (!(var->dim = calloc(ndims, sizeof(NC_DIM_INFO_T *)))) + if (!(var->dim = calloc((size_t)ndims, sizeof(NC_DIM_INFO_T *)))) return NC_ENOMEM; - if (!(var->dimids = calloc(ndims, sizeof(int)))) + if (!(var->dimids = calloc((size_t)ndims, sizeof(int)))) return NC_ENOMEM; /* Initialize dimids to illegal values (-1). See the comment in nc4_rec_match_dimscales(). */ - memset(var->dimids, -1, ndims * sizeof(int)); + memset(var->dimids, -1, (size_t)ndims * sizeof(int)); } return NC_NOERR; @@ -1159,7 +1160,7 @@ nc4_field_list_add(NC_TYPE_INFO_T *parent, const char *name, if (ndims) { int i; - if (!(field->dim_size = malloc(ndims * sizeof(int)))) + if (!(field->dim_size = malloc((size_t)ndims * sizeof(int)))) { free(field->hdr.name); free(field); @@ -1742,7 +1743,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 +1876,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-config.cmake.in b/nc-config.cmake.in index 8588665d2..15ea967d4 100644 --- a/nc-config.cmake.in +++ b/nc-config.cmake.in @@ -118,7 +118,7 @@ fi has_multifilters="yes" -has_zstd="@HAS_ZSTD" +has_zstd="@HAVE_ZSTD@" if [ -z "$has_zstd" -o "$has_zstd" = "OFF" -o "$has_zstd" = "FALSE" ]; then has_zstd="no" else diff --git a/nc_perf/bm_file.c b/nc_perf/bm_file.c index ee04cfdd7..ffdd50654 100644 --- a/nc_perf/bm_file.c +++ b/nc_perf/bm_file.c @@ -462,7 +462,7 @@ int copy_file(char *file_name_in, char *file_name_out, int cmode_out, size_t *data_read_us, int *data_write_us, int *in_format, int use_par, int par_access, float *num_bytes, int p, int my_rank, int slow_count, int verbose, int use_scs, int endianness, - int convert_unlim) + int convert_unlim, int zstandard, int nsd) { int ncid_in, ncid_out; int natts, nvars, ndims, unlimdimid; @@ -578,6 +578,9 @@ int copy_file(char *file_name_in, char *file_name_out, int cmode_out, /* Create the output var. */ if (nc_def_var(ncid_out, name, xtype, ndims, dimids, &varid_out)) ERR; + if (nsd) + if (nc_def_var_quantize(ncid_out, varid_out, NC_QUANTIZE_GRANULARBR, nsd)) ERR; + /* Set the output endianness. For simplicity in this program, * all vars get the same endianness. But there's no reason why * this couldn't be varied from var to var, though it is hard to @@ -598,7 +601,20 @@ int copy_file(char *file_name_in, char *file_name_out, int cmode_out, if (nc_def_var_chunking(ncid_out, v, 1, NULL)) ERR; } if (vo[o1].deflate_num != -1) - if (nc_def_var_deflate(ncid_out, v, vo[o1].shuffle, 1, vo[o1].deflate_num)) ERR; + { + if (zstandard) + { + int ret; + if (nc_def_var_deflate(ncid_out, v, vo[o1].shuffle, 0, 0)) ERR; + if ((ret = nc_def_var_zstandard(ncid_out, v, vo[o1].deflate_num))) + { + printf("ret %d\n", ret); + ERR; + } + } + else + if (nc_def_var_deflate(ncid_out, v, vo[o1].shuffle, 1, vo[o1].deflate_num)) ERR; + } break; } @@ -780,7 +796,6 @@ int copy_file(char *file_name_in, char *file_name_out, int cmode_out, #define NDIMS 3 #define MAX_DEFLATE 9 -#define INPUT_FILE "/upc/share/testdata/nssl/mosaic3d_nc/tile1/20070803-2300.netcdf" #define COLON ":" #define COMMA "," @@ -800,13 +815,15 @@ int copy_file(char *file_name_in, char *file_name_out, int cmode_out, [-i] Use MPIIO (only relevant for parallel builds).\n\ [-l] Convert unlimited dimensions to fixed dimensions.\n\ [-e 1|2] Set the endianness of output (1=little 2=big).\n\ + [-y] Use zstandard compression instead of zlib.\n\ + [-q nsd] Quantize to NSD.\n\ file Name of netCDF file\n" static void usage(void) { fprintf(stderr, "bm_file -v [-s N]|[-t V:S:S:S -u V:C:C:C -r V:I:I:I] -o file_out -f N -h" - " -c V:C:C,V:C:C:C -d -m -p -i -e 1|2 -l file\n%s", USAGE); + " -c V:C:C,V:C:C:C -d -m -p -i -e 1|2 -l -y -q N file\n%s", USAGE); } int @@ -819,6 +836,7 @@ main(int argc, char **argv) char file_in[NC_MAX_NAME + 1], file_out[NC_MAX_NAME + 1] = {""}; char file_out_2[NC_MAX_NAME + 10 + 1]; /* extra 10 to silence warning */ int out_format, in_format, header = 0, doublecheck = 0; + int zstandard = 0; int convert_unlim = 0; char *str1, *str2, *token, *subtoken; char *saveptr1, *saveptr2; @@ -839,6 +857,7 @@ main(int argc, char **argv) float read_rate, write_rate, reread_rate; int slow_count = 10, use_scs = 0; int endianness = 0; + int nsd = 0; float num_bytes = 0; int p = 1, my_rank = 0; int c; @@ -857,7 +876,7 @@ main(int argc, char **argv) for (i = 0; i < MAX_DIMS; i++) vo[o1].chunksize[i] = 0; - while ((c = getopt(argc, argv, "vo:f:hc:dpms:it:u:r:e:l")) != EOF) + while ((c = getopt(argc, argv, "vo:f:hc:dpms:it:u:r:e:l:yq:")) != EOF) switch(c) { case 'v': @@ -1000,6 +1019,12 @@ main(int argc, char **argv) case 'l': convert_unlim++; break; + case 'y': + zstandard++; + break; + case 'q': + sscanf(optarg, "%d", &nsd); + break; case '?': usage(); return 1; @@ -1060,7 +1085,7 @@ main(int argc, char **argv) if ((ret = copy_file(file_in, file_out, cmode, num_vo, vo, &meta_read_us, &meta_write_us, &data_read_us, &data_write_us, &in_format, use_par, par_access, &num_bytes, p, my_rank, slow_count, verbose, use_scs, endianness, - convert_unlim))) + convert_unlim, zstandard, nsd))) return ret; /* If the user wants a double check, make sure the data in the new @@ -1075,8 +1100,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, @@ -1133,7 +1158,7 @@ main(int argc, char **argv) if (use_par) printf("num_proc, "); printf("deflate, shuffle, chunksize[0], chunksize[1], chunksize[2], " - "chunksize[3]\n"); + "chunksize[3], zstandard, nsd\n"); } printf("%d, %d, %ld, %ld, %d, %d, %ld, %d, %d, ", in_format, out_format, file_size(file_in), @@ -1148,8 +1173,8 @@ main(int argc, char **argv) printf("%d, ", p); for (o1 = 0; o1 < num_vo; o1++) { - printf("%d, %d, %d, %d, %d, %d ", vo[o1].deflate_num, vo[o1].shuffle, - (int)vo[o1].chunksize[0], (int)vo[o1].chunksize[1], (int)vo[o1].chunksize[2], (int)vo[o1].chunksize[3]); + printf("%d, %d, %d, %d, %d, %d, %d, %d", vo[o1].deflate_num, vo[o1].shuffle, + (int)vo[o1].chunksize[0], (int)vo[o1].chunksize[1], (int)vo[o1].chunksize[2], (int)vo[o1].chunksize[3], zstandard, nsd); if (o1 >= MAX_VO_PRINTED) break; if (o1 != num_vo - 1) diff --git a/nc_perf/bm_many_atts.c b/nc_perf/bm_many_atts.c index 7cccf6563..07334110c 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 defc65a14..43e4f49da 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_ar4_4d.c b/nc_perf/tst_ar4_4d.c index 398686e60..85e1db4cd 100644 --- a/nc_perf/tst_ar4_4d.c +++ b/nc_perf/tst_ar4_4d.c @@ -150,8 +150,7 @@ variables: RESULT. Return 1 if the difference is negative, otherwise 0. This function from the GNU documentation. */ static int -timeval_subtract (result, x, y) - struct timeval *result, *x, *y; +timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) { /* Perform the carry for the later subtraction by updating Y. */ if (x->tv_usec < y->tv_usec) { diff --git a/nc_perf/tst_attsperf.c b/nc_perf/tst_attsperf.c index 26a2b4844..d9911ea39 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 05dfd216b..825c49af2 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 701a2c4ba..1cc916846 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 1495ff484..bd3a816cc 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 39b769c96..c38b494b1 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 c3fdd8746..753b3979a 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 ca9c8211a..ccc8580ed 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_files3.c b/nc_perf/tst_files3.c index ab04f7ec0..f4404e053 100644 --- a/nc_perf/tst_files3.c +++ b/nc_perf/tst_files3.c @@ -100,6 +100,7 @@ int dump_hdf_file(const float *data, int docompression) hsize_t dims[NDIMS] = {X_LEN, Y_LEN, Z_LEN}; hsize_t start[NDIMS] = {0, 0, 0}; hsize_t count[NDIMS] = {1, 1, Z_LEN}; + hsize_t ones[NDIMS] = {1, 1, 1}; /* create file */ file_id = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, @@ -138,7 +139,7 @@ int dump_hdf_file(const float *data, int docompression) for (start[1] = 0; start[1] < Y_LEN; start[1]++) { if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL, - count, NULL) < 0) ERR_RET; + ones, count) < 0) ERR_RET; if (H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, mem_spaceid, file_spaceid, xfer_plistid, data) < 0) ERR_RET; } diff --git a/nc_perf/tst_h_many_atts.c b/nc_perf/tst_h_many_atts.c index f484c4310..f5cd1f412 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_utils.c b/nc_perf/tst_utils.c index 96a16294e..b27f43373 100644 --- a/nc_perf/tst_utils.c +++ b/nc_perf/tst_utils.c @@ -21,8 +21,7 @@ See \ref copyright file for more info. RESULT. Return 1 if the difference is negative, otherwise 0. This function from the GNU documentation. */ int -nc4_timeval_subtract (result, x, y) - struct timeval *result, *x, *y; +nc4_timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) { /* Perform the carry for the later subtraction by updating Y. */ if (x->tv_usec < y->tv_usec) { diff --git a/nc_perf/tst_wrf_reads.c b/nc_perf/tst_wrf_reads.c index e38805fb2..34d998f03 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 114fa3d2f..ec4f1f088 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 756a6faa6..7d2e385de 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 3f97c3f29..b4f0a8a87 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 bde8bba25..01d6100b3 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 dd885120a..5df0079f7 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 98dc8dfc6..d5734ab07 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 f3c2cf316..78c36bbfb 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 b237a5031..ad85ede91 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 f5162ab78..68ebf9306 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_def_var_fill.c b/nc_test/tst_def_var_fill.c index 5c3248542..425d563c8 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/nc_test/tst_diskless3.c b/nc_test/tst_diskless3.c index 2c428f33c..31765fa95 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 be667fe74..8519f87b9 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 +#include #include "err_macros.h" #define FILE_NAME_BASE "tst_formats" @@ -115,7 +116,7 @@ main(int argc, char **argv) int expected_mode; int expected_extended_format; - sprintf(file_name, "%s_%d.nc", FILE_NAME_BASE, format[f]); + snprintf(file_name, sizeof(file_name), "%s_%d.nc", FILE_NAME_BASE, format[f]); /* Set up test. */ switch (format[f]) { @@ -179,7 +180,7 @@ main(int argc, char **argv) size_t nfilters; /* Try to set fill mode after data have been written. */ - sprintf(file_name, "%s_%d_%d_%d_elatefill.nc", FILE_NAME_BASE, format[f], d, a); + snprintf(file_name, sizeof(file_name), "%s_%d_%d_%d_elatefill.nc", FILE_NAME_BASE, format[f], d, a); if (nc_set_default_format(format[f], NULL)) ERR; if (nc_create(file_name, 0, &ncid)) ERR; if (nc_def_dim(ncid, DIM_NAME, DIM_LEN, &dimid)) ERR; @@ -281,7 +282,7 @@ main(int argc, char **argv) char var_name[NC_MAX_NAME + 1]; int dimid[NDIM2]; int xtype[NTYPE] = {NC_BYTE, NC_CHAR, NC_SHORT, NC_INT, NC_FLOAT, NC_DOUBLE}; - int type_size[NTYPE] = {1, 1, 2, 4, 4, 8}; + size_t type_size[NTYPE] = {1, 1, 2, 4, 4, 8}; int varid[NTYPE]; size_t start[NDIM2] = {0, 0}; size_t count[NDIM2] = {2, 2}; @@ -295,14 +296,14 @@ main(int argc, char **argv) int t; /* Create the test file. */ - sprintf(file_name, "%s_%d_null_strides.nc", FILE_NAME_BASE, format[f]); + snprintf(file_name, sizeof(file_name), "%s_%d_null_strides.nc", FILE_NAME_BASE, format[f]); if (nc_set_default_format(format[f], NULL)) ERR; if (nc_create(file_name, 0, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM_LEN, &dimid[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIM_LEN, &dimid[1])) ERR; for (t = 0; t < NTYPE; t++) { - sprintf(var_name, "var_%d", xtype[t]); + snprintf(var_name, sizeof(var_name), "var_%d", xtype[t]); if (nc_def_var(ncid, var_name, xtype[t], NDIM2, dimid, &varid[t])) ERR; } if (nc_enddef(ncid)) ERR; diff --git a/nc_test/tst_global_fillval.c b/nc_test/tst_global_fillval.c index 51415d75a..38e4e60f6 100644 --- a/nc_test/tst_global_fillval.c +++ b/nc_test/tst_global_fillval.c @@ -16,6 +16,7 @@ #include "config.h" #include +#include #include "err_macros.h" #define FILE_NAME "tst_global_fillval.nc" @@ -28,8 +29,8 @@ main(int argc, char **argv) { int n = 0; - int i; - int num_formats = 2; + size_t i; + size_t num_formats = 2; int *formats = NULL; /* Determine how many formats are in use. */ diff --git a/nc_test/tst_large.c b/nc_test/tst_large.c index f9f643f3a..8d136800a 100644 --- a/nc_test/tst_large.c +++ b/nc_test/tst_large.c @@ -146,7 +146,7 @@ main(int argc, char **argv) { int i; char testfile[NC_MAX_NAME + 1]; - sprintf(testfile, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(testfile, sizeof(testfile), "%s/%s", TEMP_LARGE, FILE_NAME); printf("\n*** Testing fix for 3.6.2 large file bug in %s.\n", testfile); diff --git a/nc_test/tst_large_cdf5.c b/nc_test/tst_large_cdf5.c index 8ae0b2a2e..e2a1b351d 100644 --- a/nc_test/tst_large_cdf5.c +++ b/nc_test/tst_large_cdf5.c @@ -149,7 +149,7 @@ main(int argc, char **argv) { int i; char testfile[NC_MAX_NAME + 1]; - sprintf(testfile, "%s/%s", TEMP_LARGE, FILE_NAME); + snprintf(testfile, sizeof(testfile), "%s/%s", TEMP_LARGE, FILE_NAME); printf("\n*** Testing fix for 3.6.2 large file bug in %s.\n", testfile); diff --git a/nc_test/tst_nofill.c b/nc_test/tst_nofill.c index b6cb12d6f..3d2b67885 100644 --- a/nc_test/tst_nofill.c +++ b/nc_test/tst_nofill.c @@ -421,7 +421,7 @@ main(int argc, char **argv) char varname2[NC_MAX_NAME]; /* How many values in this variable to compare? */ if (nc_inq_varndims(ncid1, varid, &ndims)) ERR; - dimids = malloc((ndims + 1) * sizeof(int)); + dimids = malloc((size_t)(ndims + 1) * sizeof(int)); if (!dimids) ERR; if (nc_inq_vardimid (ncid1, varid, dimids)) ERR; nvals = 1; diff --git a/nc_test/tst_parallel2.c b/nc_test/tst_parallel2.c index a9c52f4af..3c6bfde36 100644 --- a/nc_test/tst_parallel2.c +++ b/nc_test/tst_parallel2.c @@ -119,7 +119,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); #ifdef DEBUG fprintf(stderr,"create: file_name=%s\n",file_name); #endif diff --git a/nc_test/tst_pnetcdf.c b/nc_test/tst_pnetcdf.c index 5cb7afabe..c711f9310 100644 --- a/nc_test/tst_pnetcdf.c +++ b/nc_test/tst_pnetcdf.c @@ -64,7 +64,7 @@ int main(int argc, char* argv[]) #endif cmode = NC_CLOBBER; - sprintf(file_name, "%s/%s", TEMP_LARGE, FILENAME); + snprintf(file_name, sizeof(file_name), "%s/%s", TEMP_LARGE, FILENAME); st = nc_create_par(file_name, cmode, comm, info, &ncid); #ifdef USE_PNETCDF @@ -81,11 +81,11 @@ int main(int argc, char* argv[]) /* Odd numbers are fixed variables, even numbers are record variables */ for (i=0; i 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/h5testszip.c b/nc_test4/h5testszip.c old mode 100755 new mode 100644 diff --git a/nc_test4/ref_fixedstring.h5 b/nc_test4/ref_fixedstring.h5 old mode 100755 new mode 100644 diff --git a/nc_test4/tst_atts2.c b/nc_test4/tst_atts2.c index eb3201511..700e34ff6 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_charvlenbug.c b/nc_test4/tst_charvlenbug.c old mode 100755 new mode 100644 diff --git a/nc_test4/tst_chunks.c b/nc_test4/tst_chunks.c index 1391f1eab..280c64402 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 0499d6f39..4e95db953 100644 --- a/nc_test4/tst_chunks2.c +++ b/nc_test4/tst_chunks2.c @@ -24,7 +24,7 @@ calculate_waste(int ndims, size_t *dimlen, size_t *chunksize, float *waste) size_t chunk_size = 1; assert(waste && dimlen && chunksize && ndims); - if (!(num_chunks = calloc(ndims, sizeof(size_t)))) ERR; + if (!(num_chunks = calloc((size_t)ndims, sizeof(size_t)))) ERR; #ifdef PRINT_CHUNK_WASTE_REPORT printf("\n"); @@ -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 731ed8bc4..a2c6da7de 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 d388658bb..ce4a495d4 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 5237d29c7..0af77d72b 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 f2ad31bed..2a8417166 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_fillonly.c b/nc_test4/tst_fillonly.c old mode 100755 new mode 100644 diff --git a/nc_test4/tst_grps.c b/nc_test4/tst_grps.c index 9224c96df..30f60de15 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_h_strbug.c b/nc_test4/tst_h_strbug.c index 91b3ce36e..3d573c5df 100644 --- a/nc_test4/tst_h_strbug.c +++ b/nc_test4/tst_h_strbug.c @@ -14,6 +14,7 @@ #include #include #include +#include #define FILE_NAME "tst_h_strbug.h5" #define VS_ATT_NAME "vsatt" @@ -173,7 +174,7 @@ main() if (type != NC_STRING) ERR; if (nc_inq_varndims(ncid, varid, &ndims )) ERR; if (ndims != RANK) ERR; - if (!(dimids = malloc(ndims * sizeof(int)))) ERR; + if (!(dimids = malloc((size_t)ndims * sizeof(int)))) ERR; if (nc_inq_vardimid(ncid, varid, dimids)) ERR; if (nc_inq_dimlen(ncid, dimids[0], &nstrings)) ERR; if (!(data_in = (char **)malloc(nstrings * sizeof(char *)))) ERR; diff --git a/nc_test4/tst_interops4.c b/nc_test4/tst_interops4.c index 535a80e4f..22fa42951 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 d696eb09a..39ec19520 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 55fea5ec9..2c0c849c2 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 296f68d32..5bea1478a 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 2b3eaf824..20ff0447c 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 af39e2094..ded16c908 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 ced8b7051..25cf4a55f 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 17f5e0bde..4d711bc9b 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 b2e8df264..7ea3c78d4 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 6e99234e8..f60a7e067 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 eb43c3043..c2694e7a9 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 9aad7c0a9..efc0d87c8 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 a86f7ffb1..c366363fa 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 6f8c95ae6..e17af6634 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 bf6637df6..b91d2a383 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 301ca6cd7..ed78351a5 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 d4d82c30f..f565dda33 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 f94fc0dcb..0124291e9 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 19d51de13..f0420bccd 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/nc_test4/tst_virtual_datasets.c b/nc_test4/tst_virtual_datasets.c index 1a4577fd0..ff278a3dd 100644 --- a/nc_test4/tst_virtual_datasets.c +++ b/nc_test4/tst_virtual_datasets.c @@ -55,6 +55,7 @@ int read_back_contents(const char* filename) { size_t i; float data[VARIABLE_SIZE]; + char var_name[NC_MAX_NAME+1]; int dimids_var[1], var_type, natts; diff --git a/nc_test4/tst_vl.c b/nc_test4/tst_vl.c index cde9927d8..5aef801a6 100644 --- a/nc_test4/tst_vl.c +++ b/nc_test4/tst_vl.c @@ -8,6 +8,7 @@ */ #include +#include #include "err_macros.h" #include "netcdf.h" @@ -38,7 +39,7 @@ main(int argc, char **argv) char name_in[NC_MAX_NAME + 1]; int *phony, class_in; size_t len_in; - int i, j; + size_t i, j; /* Create phony data. */ for (i=0; i #include #include #include @@ -34,7 +35,7 @@ checkErrorCode(int status, const char* message) void -writeVariable(int dimlength, int ncid, nc_type vlen_typeID) +writeVariable(size_t dimlength, int ncid, nc_type vlen_typeID) { int retval = NC_NOERR; int dimid; @@ -99,12 +100,12 @@ createFruitsData(int elemno, int vlensize, int* stringIndexp, nc_vlen_t* data) } void -writeAttribute(int len, int ncid, nc_type vlen_typeID) +writeAttribute(size_t len, int ncid, nc_type vlen_typeID) { int retval = NC_NOERR; /* Setup data */ - nc_vlen_t* data = calloc(sizeof(nc_vlen_t),len); + nc_vlen_t* data = calloc(sizeof(nc_vlen_t), len); /* create six variable length arrays of strings */ int stringIndex = 0; diff --git a/ncdap_test/manyurls.h b/ncdap_test/manyurls.h old mode 100755 new mode 100644 diff --git a/ncdap_test/test_nstride_cached.c b/ncdap_test/test_nstride_cached.c index c539ec90c..cd45bc9f5 100644 --- a/ncdap_test/test_nstride_cached.c +++ b/ncdap_test/test_nstride_cached.c @@ -99,7 +99,7 @@ main() int pass = 1; int nelems = XSIZE*YSIZE; int idim, ndim; - float *dat = (float*)malloc(sizeof(float)*nelems); + float *dat = (float*)malloc(sizeof(float)*(size_t)nelems); float sdat[10]; char* svc; diff --git a/ncdap_test/test_vara.c b/ncdap_test/test_vara.c index aee2d2865..bc1e582b2 100644 --- a/ncdap_test/test_vara.c +++ b/ncdap_test/test_vara.c @@ -158,10 +158,10 @@ static Odom* odom_create(int rank) Odom* odom = (Odom*)malloc(sizeof(Odom)); /* Init the odometer */ odom->rank = rank; - odom->index = (size_t*)calloc(sizeof(size_t)*rank,1); - odom->stop = (size_t*)calloc(sizeof(size_t)*rank,1); - odom->start = (size_t*)calloc(sizeof(size_t)*rank,1); - odom->count = (size_t*)calloc(sizeof(size_t)*rank,1); + odom->index = (size_t*)calloc(sizeof(size_t)*(size_t)rank,1); + odom->stop = (size_t*)calloc(sizeof(size_t)*(size_t)rank,1); + odom->start = (size_t*)calloc(sizeof(size_t)*(size_t)rank,1); + odom->count = (size_t*)calloc(sizeof(size_t)*(size_t)rank,1); return odom; } diff --git a/ncdump/chunkspec.c b/ncdump/chunkspec.c index 8029530bc..909e3ee07 100644 --- a/ncdump/chunkspec.c +++ b/ncdump/chunkspec.c @@ -136,7 +136,7 @@ dimchunkspec_parse(int igrp, const char *spec) goto done; } /* extract dimension name */ - dimname = (char *) emalloc(pp - np + 1); + dimname = (char *) emalloc((size_t)(pp - np + 1)); dp = dimname; while(np < pp) { *dp++ = *np++; diff --git a/ncdump/dumplib.c b/ncdump/dumplib.c index 03fe3b278..07090d17d 100644 --- a/ncdump/dumplib.c +++ b/ncdump/dumplib.c @@ -415,7 +415,7 @@ count_udtypes(int ncid) { /* Get number of types in this group */ NC_CHECK( nc_inq_typeids(ncid, &ntypes, NULL) ) ; NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) ) ; - ncids = (int *) emalloc(sizeof(int) * (numgrps + 1)); + ncids = (int *) emalloc(sizeof(int) * (size_t)(numgrps + 1)); NC_CHECK( nc_inq_grps(ncid, NULL, ncids) ) ; /* Add number of types in each subgroup, if any */ for (i=0; i < numgrps; i++) { @@ -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; @@ -1270,7 +1272,7 @@ nctime_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) { oldopts = cdSetErrOpts(0); newopts = oldopts | CU_VERBOSE; cdSetErrOpts(newopts); - cdRel2Iso(varp->timeinfo->calendar, varp->timeinfo->units, separator, vv, &sout[1]); + cdRel2Iso(varp->timeinfo->calendar, varp->timeinfo->units, separator, vv, &sout[1], sizeof(sout) - 1); cdSetErrOpts(oldopts); res = strlen(sout); sout[res++] = '"'; @@ -1520,7 +1522,7 @@ init_types(int ncid) { if (max_type == 0) { /* if called for first time */ int maxtype = max_typeid(ncid); int i; - nctypes = (nctype_t **) emalloc((maxtype + 2) * sizeof(nctype_t *)); + nctypes = (nctype_t **) emalloc((size_t)(maxtype + 2) * sizeof(nctype_t *)); for(i=0; i < maxtype+1; i++) nctypes[i] = NULL; /* so can later skip over unused type slots */ init_prim_types(ncid); @@ -1532,7 +1534,7 @@ init_types(int ncid) { if (ntypes) { int t; - int *typeids = emalloc((ntypes + 1) * sizeof(int)); + int *typeids = emalloc((size_t)(ntypes + 1) * sizeof(int)); NC_CHECK( nc_inq_typeids(ncid, NULL, typeids) ); for (t = 0; t < ntypes; t++) { nctype_t *tinfo; /* details about the type */ @@ -1587,14 +1589,14 @@ init_types(int ncid) { NC_CHECK( nc_inq_compound_field(ncid, tinfo->tid, fidx, NULL, &offset, &ftype, &rank, sides) ); - if(rank > 0) sides = (int *) emalloc(rank * sizeof(int)); + if(rank > 0) sides = (int *) emalloc((size_t)rank * sizeof(int)); NC_CHECK( nc_inq_compound_field(ncid, tinfo->tid, fidx, NULL, NULL, NULL, NULL, sides) ); tinfo->fids[fidx] = ftype; tinfo->offsets[fidx] = offset; tinfo->ranks[fidx] = rank; if (rank > 0) - tinfo->sides[fidx] = (int *) emalloc(rank * sizeof(int)); + tinfo->sides[fidx] = (int *) emalloc((size_t)rank * sizeof(int)); tinfo->nvals[fidx] = 1; for(i = 0; i < rank; i++) { tinfo->sides[fidx][i] = sides[i]; @@ -1629,7 +1631,7 @@ init_types(int ncid) { /* See how many groups there are. */ NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) ); if (numgrps > 0) { - ncids = (int *) emalloc(numgrps * sizeof(int)); + ncids = (int *) emalloc((size_t)numgrps * sizeof(int)); /* Get the list of group ids. */ NC_CHECK( nc_inq_grps(ncid, NULL, ncids) ); /* Call this function for each group. */ @@ -1670,10 +1672,10 @@ iscoordvar(int ncid, int varid) #endif if (dims) free(dims); - dims = (ncdim_t *) emalloc((ndims + 1) * sizeof(ncdim_t)); + dims = (ncdim_t *) emalloc((size_t)(ndims + 1) * sizeof(ncdim_t)); if (dimids) free(dimids); - dimids = (int *) emalloc((ndims + 1) * sizeof(int)); + dimids = (int *) emalloc((size_t)(ndims + 1) * sizeof(int)); #ifdef USE_NETCDF4 NC_CHECK( nc_inq_dimids(ncid, &ndims1, dimids, include_parents ) ); #else @@ -1767,7 +1769,7 @@ print_type_name(int locid, int typeid) { curlocid = parent_groupid; NC_CHECK( nc_inq_typeids(curlocid, &ntypes, NULL) ); if(ntypes > 0) { - int *typeids = (int *) emalloc((ntypes + 1) * sizeof(int)); + int *typeids = (int *) emalloc((size_t)(ntypes + 1) * sizeof(int)); int i; NC_CHECK( nc_inq_typeids(curlocid, &ntypes, typeids) ); for(i = 0; i < ntypes; i++) { @@ -1822,7 +1824,7 @@ init_is_unlim(int ncid, int **is_unlim_p) return NC_EBADGRPID; /* Now ncid is root group. Get total number of groups and their ids */ NC_CHECK( nc_inq_grps_full(ncid, &num_grps, NULL) ); - grpids = emalloc((num_grps + 1) * sizeof(int)); + grpids = emalloc((size_t)(num_grps + 1) * sizeof(int)); NC_CHECK( nc_inq_grps_full(ncid, &num_grps, grpids) ); #define DONT_INCLUDE_PARENTS 0 /* Get all dimensions in groups and info about which ones are unlimited */ @@ -1835,18 +1837,18 @@ init_is_unlim(int ncid, int **is_unlim_p) grpid = grpids[igrp]; NC_CHECK( nc_inq_dimids(grpid, &ndims, NULL, DONT_INCLUDE_PARENTS) ); num_dims += ndims; - dimids = (int*)emalloc(ndims*sizeof(int)); + dimids = (int*)emalloc((size_t)ndims*sizeof(int)); NC_CHECK( nc_inq_dimids(grpid, &ndims, dimids, DONT_INCLUDE_PARENTS) ); for(i=0;i max_dimid) max_dimid = dimids[i];} free(dimids); } assert(max_dimid >= 0); - *is_unlim_p = emalloc((max_dimid + 1 + 1) * sizeof(int)); + *is_unlim_p = emalloc((size_t)(max_dimid + 1 + 1) * sizeof(int)); for(igrp = 0; igrp < num_grps; igrp++) { int ndims, idim, *dimids, nundims; grpid = grpids[igrp]; NC_CHECK( nc_inq_dimids(grpid, &ndims, NULL, DONT_INCLUDE_PARENTS) ); - dimids = emalloc((ndims + 1) * sizeof(int)); + dimids = emalloc((size_t)(ndims + 1) * sizeof(int)); NC_CHECK( nc_inq_dimids(grpid, &ndims, dimids, DONT_INCLUDE_PARENTS) ); /* mark all dims in this group as fixed-size */ for(idim = 0; idim < ndims; idim++) { diff --git a/ncdump/nc4printer.c b/ncdump/nc4printer.c index 10de3e785..f17584a1b 100644 --- a/ncdump/nc4printer.c +++ b/ncdump/nc4printer.c @@ -657,18 +657,18 @@ record(NC4printer* out, NCID* node) switch (node->sort) { case DIM: if(nclistlength(out->dims) <= node->id) { - nclistsetalloc(out->dims,node->id+1); - nclistsetlength(out->dims,node->id+1); + nclistsetalloc(out->dims, (size_t)node->id+1); + nclistsetlength(out->dims, (size_t)node->id+1); } - nclistset(out->dims,node->id,node); + nclistset(out->dims, (size_t)node->id, node); break; case ATOMTYPE: case USERTYPE: if(nclistlength(out->types) <= node->id) { - nclistsetalloc(out->types,node->id+1); - nclistsetlength(out->types,node->id+1); + nclistsetalloc(out->types, (size_t)node->id+1); + nclistsetlength(out->types, (size_t)node->id+1); } - nclistset(out->types,node->id,node); + nclistset(out->types, (size_t)node->id, node); break; default: break; } diff --git a/ncdump/nccopy.c b/ncdump/nccopy.c index ae041547e..190696d72 100644 --- a/ncdump/nccopy.c +++ b/ncdump/nccopy.c @@ -432,7 +432,7 @@ inq_var_chunksize(int igrp, int varid, size_t* chunksizep) { NC_CHECK(nc_inq_type(igrp, vartype, NULL, &value_size)); prod = value_size; NC_CHECK(nc_inq_varndims(igrp, varid, &ndims)); - chunksizes = (size_t *) emalloc((ndims + 1) * sizeof(size_t)); + chunksizes = (size_t *) emalloc((size_t)(ndims + 1) * sizeof(size_t)); contig = NC_CHUNKED; NC_CHECK(nc_inq_var_chunking(igrp, varid, &contig, NULL)); if(contig != NC_CHUNKED) { @@ -493,7 +493,7 @@ inq_var_chunking_params(int igrp, int ivarid, int ogrp, int ovarid, return stat; } - ichunksizes = (size_t *) emalloc((ndims + 1) * sizeof(size_t)); + ichunksizes = (size_t *) emalloc((size_t)(ndims + 1) * sizeof(size_t)); if(icontig != NC_CHUNKED) { /* if input contiguous|compact, treat as if chunked on * first dimension */ ichunksizes[0] = 1; @@ -505,7 +505,7 @@ inq_var_chunking_params(int igrp, int ivarid, int ogrp, int ovarid, } /* now can pretend chunking in both input and output */ - ochunksizes = (size_t *) emalloc((ndims + 1) * sizeof(size_t)); + ochunksizes = (size_t *) emalloc((size_t)(ndims + 1) * sizeof(size_t)); NC_CHECK(nc_inq_var_chunking(ogrp, ovarid, &ocontig, ochunksizes)); nelems = 1; @@ -632,7 +632,7 @@ copy_compound_type(int igrp, nc_type itype, int ogrp) NC_CHECK(nc_insert_compound(ogrp, otype, fname, foff, oftype)); } else { /* field is array type */ int *fdimsizes; - fdimsizes = (int *) emalloc((fndims + 1) * sizeof(int)); + fdimsizes = (int *) emalloc((size_t)(fndims + 1) * sizeof(int)); stat = nc_inq_compound_field(igrp, itype, fid, NULL, NULL, NULL, NULL, fdimsizes); NC_CHECK(nc_insert_array_compound(ogrp, otype, fname, foff, oftype, fndims, fdimsizes)); @@ -688,7 +688,7 @@ copy_groups(int iroot, int oroot) /* get total number of groups and their ids, including all descendants */ NC_CHECK(nc_inq_grps_full(iroot, &numgrps, NULL)); if(numgrps > 1) { /* there's always 1 root group */ - grpids = emalloc(numgrps * sizeof(int)); + grpids = emalloc((size_t)numgrps * sizeof(int)); NC_CHECK(nc_inq_grps_full(iroot, NULL, grpids)); /* create corresponding new groups in ogrp, except for root group */ for(i = 1; i < numgrps; i++) { @@ -741,7 +741,7 @@ copy_types(int igrp, int ogrp) NC_CHECK(nc_inq_typeids(igrp, &ntypes, NULL)); if(ntypes > 0) { - types = (nc_type *) emalloc(ntypes * sizeof(nc_type)); + types = (nc_type *) emalloc((size_t)ntypes * sizeof(nc_type)); NC_CHECK(nc_inq_typeids(igrp, &ntypes, types)); for (i = 0; i < ntypes; i++) { NC_CHECK(copy_type(igrp, types[i], ogrp)); @@ -752,7 +752,7 @@ copy_types(int igrp, int ogrp) /* Copy types from subgroups */ NC_CHECK(nc_inq_grps(igrp, &numgrps, NULL)); if(numgrps > 0) { - grpids = (int *)emalloc(sizeof(int) * numgrps); + grpids = (int *)emalloc(sizeof(int) * (size_t)numgrps); NC_CHECK(nc_inq_grps(igrp, &numgrps, grpids)); for(i = 0; i < numgrps; i++) { if (option_grpstruct || group_wanted(grpids[i], option_nlgrps, option_grpids)) { @@ -1329,11 +1329,11 @@ copy_dims(int igrp, int ogrp) * may be defined in various groups, and we are only looking at one * group at a time. */ /* Find the dimension ids in this group, don't include parents. */ - dimids = (int *) emalloc((ndims + 1) * sizeof(int)); + dimids = (int *) emalloc((size_t)(ndims + 1) * sizeof(int)); NC_CHECK(nc_inq_dimids(igrp, NULL, dimids, 0)); /* Find the number of unlimited dimensions and get their IDs */ NC_CHECK(nc_inq_unlimdims(igrp, &nunlims, NULL)); - unlimids = (int *) emalloc((nunlims + 1) * sizeof(int)); + unlimids = (int *) emalloc((size_t)(nunlims + 1) * sizeof(int)); NC_CHECK(nc_inq_unlimdims(igrp, NULL, unlimids)); #else NC_CHECK(nc_inq_unlimdim(igrp, &unlimid)); @@ -1426,7 +1426,7 @@ copy_var(int igrp, int varid, int ogrp) int o_varid; NC_CHECK(nc_inq_varndims(igrp, varid, &ndims)); - idimids = (int *) emalloc((ndims + 1) * sizeof(int)); + idimids = (int *) emalloc((size_t)(ndims + 1) * sizeof(int)); NC_CHECK(nc_inq_var(igrp, varid, name, &typeid, NULL, idimids, &natts)); o_typeid = typeid; #ifdef USE_NETCDF4 @@ -1441,7 +1441,7 @@ copy_var(int igrp, int varid, int ogrp) #endif /* USE_NETCDF4 */ /* get the corresponding dimids in the output file */ - odimids = (int *) emalloc((ndims + 1) * sizeof(int)); + odimids = (int *) emalloc((size_t)(ndims + 1) * sizeof(int)); for(i = 0; i < ndims; i++) { odimids[i] = dimmap_odimid(idimids[i]); if(odimids[i] == -1) { @@ -1550,7 +1550,7 @@ copy_schema(int igrp, int ogrp) int i; /* Copy schema from subgroups */ stat = nc_inq_grps(igrp, &numgrps, NULL); - grpids = (int *)emalloc((numgrps + 1) * sizeof(int)); + grpids = (int *)emalloc((size_t)(numgrps + 1) * sizeof(int)); NC_CHECK(nc_inq_grps(igrp, &numgrps, grpids)); for(i = 0; i < numgrps; i++) { @@ -1574,7 +1574,7 @@ inq_nvals(int igrp, int varid, long long *nvalsp) { long long nvals = 1; NC_CHECK(nc_inq_varndims(igrp, varid, &ndims)); - dimids = (int *) emalloc((ndims + 1) * sizeof(int)); + dimids = (int *) emalloc((size_t)(ndims + 1) * sizeof(int)); NC_CHECK(nc_inq_vardimid (igrp, varid, dimids)); for(dim = 0; dim < ndims; dim++) { size_t len; @@ -1673,8 +1673,8 @@ copy_var_data(int igrp, int varid, int ogrp) /* initialize variable iteration */ NC_CHECK(nc_get_iter(igrp, varid, option_copy_buffer_size, &iterp)); - start = (size_t *) emalloc((iterp->rank + 1) * sizeof(size_t)); - count = (size_t *) emalloc((iterp->rank + 1) * sizeof(size_t)); + start = (size_t *) emalloc((size_t)(iterp->rank + 1) * sizeof(size_t)); + count = (size_t *) emalloc((size_t)(iterp->rank + 1) * sizeof(size_t)); /* nc_next_iter() initializes start and count on first call, * changes start and count to iterate through whole variable on * subsequent calls. */ @@ -1757,7 +1757,7 @@ copy_data(int igrp, int ogrp) #ifdef USE_NETCDF4 /* Copy data from subgroups */ stat = nc_inq_grps(igrp, &numgrps, NULL); - grpids = (int *)emalloc((numgrps + 1) * sizeof(int)); + grpids = (int *)emalloc((size_t)(numgrps + 1) * sizeof(int)); NC_CHECK(nc_inq_grps(igrp, &numgrps, grpids)); for(i = 0; i < numgrps; i++) { @@ -1786,7 +1786,7 @@ count_dims(int ncid) { NC_CHECK(nc_inq_grps(ncid, &numgrps, NULL)); if(numgrps > 0) { int igrp; - int *grpids = emalloc(numgrps * sizeof(int)); + int *grpids = emalloc((size_t)numgrps * sizeof(int)); NC_CHECK(nc_inq_grps(ncid, &numgrps, grpids)); for(igrp = 0; igrp < numgrps; igrp++) { ndims += count_dims(grpids[igrp]); @@ -1820,7 +1820,7 @@ nc3_special_case(int ncid, int kind) { NC_CHECK( nc_inq_varndims(ncid, varid, &ndims) ); if (ndims > 0) { int dimids0; - dimids = (int *) emalloc((ndims + 1) * sizeof(int)); + dimids = (int *) emalloc((size_t)(ndims + 1) * sizeof(int)); NC_CHECK( nc_inq_vardimid(ncid, varid, dimids) ); dimids0 = dimids[0]; free(dimids); @@ -1850,9 +1850,9 @@ classify_vars( int nvars; NC_CHECK(nc_inq_nvars(ncid, &nvars)); *nf = 0; - *fvars = (int *) emalloc(nvars * sizeof(int)); + *fvars = (int *) emalloc((size_t)nvars * sizeof(int)); *nr = 0; - *rvars = (int *) emalloc(nvars * sizeof(int)); + *rvars = (int *) emalloc((size_t)nvars * sizeof(int)); if(option_nlvars > 0) { for (varindex = 0; varindex < option_nlvars; varindex++) { @@ -1940,9 +1940,9 @@ copy_record_data(int ncid, int ogrp, size_t nrec_vars, int *rec_varids) { char varname[NC_MAX_NAME]; varid = rec_varids[ivar]; NC_CHECK(nc_inq_varndims(ncid, varid, &ndims)); - dimids = (int *) emalloc((1 + ndims) * sizeof(int)); - start[ivar] = (size_t *) emalloc(ndims * sizeof(size_t)); - count[ivar] = (size_t *) emalloc(ndims * sizeof(size_t)); + dimids = (int *) emalloc((size_t)(1 + ndims) * sizeof(int)); + start[ivar] = (size_t *) emalloc((size_t)ndims * sizeof(size_t)); + count[ivar] = (size_t *) emalloc((size_t)ndims * sizeof(size_t)); NC_CHECK(nc_inq_vardimid (ncid, varid, dimids)); value_size = val_size(ncid, varid); nvals = 1; diff --git a/ncdump/ncdump.c b/ncdump/ncdump.c index abaa20e38..c86ecb824 100644 --- a/ncdump/ncdump.c +++ b/ncdump/ncdump.c @@ -4,6 +4,7 @@ Copyright 2018 University Corporation for Atmospheric Research/Unidata. See \ref copyright file for more info. */ #include "config.h" +#include #include #ifdef HAVE_GETOPT_H #include @@ -997,7 +998,7 @@ pr_att_specials( int i; pr_att_name(ncid, varp->name, NC_ATT_STORAGE); printf(" = \"chunked\" ;\n"); - chunkp = (size_t *) emalloc(sizeof(size_t) * (varp->ndims + 1) ); + chunkp = (size_t *) emalloc(sizeof(size_t) * (size_t)(varp->ndims + 1) ); NC_CHECK( nc_inq_var_chunking(ncid, varid, NULL, chunkp) ); /* print chunking, even if it is default */ pr_att_name(ncid, varp->name, NC_ATT_CHUNKING); @@ -1224,7 +1225,7 @@ pr_attx( { ncatt_t att; /* attribute */ char *attvals = NULL; - int attvalslen = 0; + size_t attvalslen = 0; NC_CHECK( nc_inq_attname(ncid, varid, ia, att.name) ); #ifdef USE_NETCDF4 @@ -1308,7 +1309,7 @@ static void pr_shape(ncvar_t* varp, ncdim_t *dims) { char *shape; - int shapelen = 0; + size_t shapelen = 0; int id; if (varp->ndims == 0) @@ -1472,7 +1473,7 @@ print_ud_type(int ncid, nc_type typeid) { printf(" "); print_name(field_name); if (field_ndims > 0) { - int *field_dim_sizes = (int *) emalloc((field_ndims + 1) * sizeof(int)); + int *field_dim_sizes = (int *) emalloc((size_t)(field_ndims + 1) * sizeof(int)); NC_CHECK( nc_inq_compound_field(ncid, typeid, f, NULL, NULL, NULL, NULL, field_dim_sizes) ); @@ -1647,7 +1648,7 @@ do_ncdump_rec(int ncid, const char *path) { int t; - typeids = emalloc((ntypes + 1) * sizeof(int)); + typeids = emalloc((size_t)(ntypes + 1) * sizeof(int)); NC_CHECK( nc_inq_typeids(ncid, &ntypes, typeids) ); indent_out(); printf("types:\n"); @@ -1667,7 +1668,7 @@ do_ncdump_rec(int ncid, const char *path) */ NC_CHECK( nc_inq(ncid, &ndims, &nvars, &ngatts, &xdimid) ); /* get dimension info */ - dims = (ncdim_t *) emalloc((ndims + 1) * sizeof(ncdim_t)); + dims = (ncdim_t *) emalloc((size_t)(ndims + 1) * sizeof(ncdim_t)); if (ndims > 0) { indent_out(); printf ("dimensions:\n"); @@ -1680,14 +1681,14 @@ do_ncdump_rec(int ncid, const char *path) /* Find the number of dimids defined in this group. */ NC_CHECK( nc_inq_ndims(ncid, &ndims_grp) ); - dimids_grp = (int *)emalloc((ndims_grp + 1) * sizeof(int)); + dimids_grp = (int *)emalloc((size_t)(ndims_grp + 1) * sizeof(int)); /* Find the dimension ids in this group. */ NC_CHECK( nc_inq_dimids(ncid, 0, dimids_grp, 0) ); /* Find the number of unlimited dimensions and get their IDs */ NC_CHECK( nc_inq_unlimdims(ncid, &nunlim, NULL) ); - unlimids = (int *)emalloc((nunlim + 1) * sizeof(int)); + unlimids = (int *)emalloc((size_t)(nunlim + 1) * sizeof(int)); NC_CHECK( nc_inq_unlimdims(ncid, &nunlim, unlimids) ); /* For each dimension defined in this group, get and print out info. */ @@ -1767,7 +1768,7 @@ do_ncdump_rec(int ncid, const char *path) for (varid = 0; varid < nvars; varid++) { NC_CHECK( nc_inq_varndims(ncid, varid, &var.ndims) ); - var.dims = (int *) emalloc((var.ndims + 1) * sizeof(int)); + var.dims = (int *) emalloc((size_t)(var.ndims + 1) * sizeof(int)); NC_CHECK( nc_inq_var(ncid, varid, var.name, &var.type, 0, var.dims, &var.natts) ); /* TODO: don't bother if type name not needed here */ @@ -1928,7 +1929,7 @@ do_ncdump_rec(int ncid, const char *path) continue; NC_CHECK( nc_inq_varndims(ncid, varid, &var.ndims) ); if(var.dims != NULL) {free(var.dims); var.dims = NULL;} - var.dims = (int *) emalloc((var.ndims + 1) * sizeof(int)); + var.dims = (int *) emalloc((size_t)(var.ndims + 1) * sizeof(int)); NC_CHECK( nc_inq_var(ncid, varid, var.name, &var.type, 0, var.dims, &var.natts) ); var.tinfo = get_typeinfo(var.type); @@ -1942,7 +1943,7 @@ do_ncdump_rec(int ncid, const char *path) free(vdims); vdims = 0; } - vdims = (size_t *) emalloc((var.ndims + 1) * SIZEOF_SIZE_T); + vdims = (size_t *) emalloc((size_t)(var.ndims + 1) * SIZEOF_SIZE_T); no_data = 0; for (id = 0; id < var.ndims; id++) { size_t len; @@ -1995,7 +1996,7 @@ do_ncdump_rec(int ncid, const char *path) NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) ); /* Allocate memory to hold the list of group ids. */ - ncids = emalloc((numgrps + 1) * sizeof(int)); + ncids = emalloc((size_t)(numgrps + 1) * sizeof(int)); /* Get the list of group ids. */ NC_CHECK( nc_inq_grps(ncid, NULL, ncids) ); @@ -2095,7 +2096,7 @@ do_ncdumpx(int ncid, const char *path) /* TODO: print names with XML-ish escapes fopr special chars */ NC_CHECK( nc_inq(ncid, &ndims, &nvars, &ngatts, &xdimid) ); /* get dimension info */ - dims = (ncdim_t *) emalloc((ndims + 1) * sizeof(ncdim_t)); + dims = (ncdim_t *) emalloc((size_t)(ndims + 1) * sizeof(ncdim_t)); for (dimid = 0; dimid < ndims; dimid++) { NC_CHECK( nc_inq_dim(ncid, dimid, dims[dimid].name, &dims[dimid].size) ); if (dimid == xdimid) @@ -2115,7 +2116,7 @@ do_ncdumpx(int ncid, const char *path) for (varid = 0; varid < nvars; varid++) { NC_CHECK( nc_inq_varndims(ncid, varid, &var.ndims) ); if(var.dims != NULL) free(var.dims); - var.dims = (int *) emalloc((var.ndims + 1) * sizeof(int)); + var.dims = (int *) emalloc((size_t)(var.ndims + 1) * sizeof(int)); NC_CHECK( nc_inq_var(ncid, varid, var.name, &var.type, 0, var.dims, &var.natts) ); printf (" #include #include #include @@ -193,10 +194,10 @@ nc_get_iter(int ncid, NC_CHECK(nc_inq_varndims(ncid, varid, &ndims)); - dimids = (int *) emalloc((ndims + 1) * sizeof(int)); + dimids = (int *) emalloc((size_t)(ndims + 1) * sizeof(int)); - iterp->dimsizes = (size_t *) emalloc((ndims + 1) * sizeof(size_t)); - iterp->chunksizes = (size_t *) emalloc((ndims + 1) * sizeof(size_t)); + iterp->dimsizes = (size_t *) emalloc((size_t)(ndims + 1) * sizeof(size_t)); + iterp->chunksizes = (size_t *) emalloc((size_t)(ndims + 1) * sizeof(size_t)); NC_CHECK(nc_inq_vardimid (ncid, varid, dimids)); for(dim = 0; dim < ndims; dim++) { diff --git a/ncdump/ncpathcvt.c b/ncdump/ncpathcvt.c old mode 100755 new mode 100644 diff --git a/ncdump/ncvalidator.c b/ncdump/ncvalidator.c index f3661269c..7e86fb64d 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); @@ -1622,9 +1622,9 @@ val_new_NC_var(char *name, int ndims) if (varp == NULL) return NULL; if (ndims > 0) { - varp->shape = (long long*)calloc(ndims, sizeof(long long)); - varp->dsizes = (long long*)calloc(ndims, sizeof(long long)); - varp->dimids = (int *) calloc(ndims, sizeof(int)); + varp->shape = (long long*)calloc((size_t)ndims, sizeof(long long)); + varp->dsizes = (long long*)calloc((size_t)ndims, sizeof(long long)); + varp->dimids = (int *) calloc((size_t)ndims, sizeof(int)); } varp->name = name; @@ -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 old mode 100755 new mode 100644 index 1e25f2daf..7c0abc47d --- 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/ncdump/tst_create_files.c b/ncdump/tst_create_files.c index e4cee78e4..f0b2b7109 100644 --- a/ncdump/tst_create_files.c +++ b/ncdump/tst_create_files.c @@ -11,6 +11,7 @@ #include #include "err_macros.h" #include +#include #include #define FILE_NAME_1 "tst_solar_1.nc" @@ -188,7 +189,7 @@ main(int argc, char **argv) #define ATT_NAME3 "for_testing_unsigned_short_attribute_bug" { int ncid; - int i, j; + size_t i, j; nc_type typeid; nc_vlen_t data[DIM_LEN]; int *phoney; diff --git a/ncdump/tst_fillbug.c b/ncdump/tst_fillbug.c index 4f9aafd15..793aa98b9 100644 --- a/ncdump/tst_fillbug.c +++ b/ncdump/tst_fillbug.c @@ -191,7 +191,7 @@ count_udtypes(int ncid) { /* Get number of types in this group */ if( nc_inq_typeids(ncid, &ntypes, NULL) ) ERR; if( nc_inq_grps(ncid, &numgrps, NULL) ) ERR; - ncids = (int *) malloc(sizeof(int) * numgrps); + ncids = (int *) malloc(sizeof(int) * (size_t)numgrps); if( nc_inq_grps(ncid, NULL, ncids) ) ERR; /* Add number of types in each subgroup, if any */ for (i=0; i < numgrps; i++) { diff --git a/ncdump/tst_rcapi.c b/ncdump/tst_rcapi.c old mode 100755 new mode 100644 diff --git a/ncdump/tst_vlen_data.c b/ncdump/tst_vlen_data.c index f11b3932b..5e17bfdf8 100644 --- a/ncdump/tst_vlen_data.c +++ b/ncdump/tst_vlen_data.c @@ -88,7 +88,7 @@ main(int argc, char **argv) if(array == NULL) ERR; for (i = 0; i < NROWS; i++) { int ncolumns = NROWS - i; - array[i] = (float *) malloc(ncolumns * sizeof(float)); + array[i] = (float *) malloc((size_t)ncolumns * sizeof(float)); if(array[i] == NULL) ERR; for (j = 0; j < ncolumns; j++) { array[i][j] = 10.0f * (float)(i + 1) + (float)j; diff --git a/ncdump/utils.c b/ncdump/utils.c index 263ae9f0a..c32d49b95 100644 --- a/ncdump/utils.c +++ b/ncdump/utils.c @@ -350,10 +350,10 @@ isrecvar(int ncid, int varid) int nunlimdims; int *recdimids; int dim, recdim; - dimids = (int *) emalloc((ndims + 1) * sizeof(int)); + dimids = (int *) emalloc((size_t)(ndims + 1) * sizeof(int)); NC_CHECK( nc_inq_vardimid(ncid, varid, dimids) ); NC_CHECK( nc_inq_unlimdims(ncid, &nunlimdims, NULL) ); - recdimids = (int *) emalloc((nunlimdims + 1) * sizeof(int)); + recdimids = (int *) emalloc((size_t)(nunlimdims + 1) * sizeof(int)); NC_CHECK( nc_inq_unlimdims(ncid, NULL, recdimids) ); for (dim = 0; dim < ndims && is_recvar == 0; dim++) { for(recdim = 0; recdim < nunlimdims; recdim++) { @@ -504,7 +504,7 @@ nc_inq_grpname_count(int ncid, int igrp, char **lgrps, idnode_t *grpids) { NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) ); if(numgrps > 0) { /* Allocate memory to hold the list of group ids. */ - ncids = emalloc(numgrps * sizeof(int)); + ncids = emalloc((size_t)numgrps * sizeof(int)); /* Get the list of group ids. */ NC_CHECK( nc_inq_grps(ncid, NULL, ncids) ); /* Call this function recursively for each group. */ @@ -649,7 +649,7 @@ nc_inq_varname_count(int ncid, char *varname) { NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) ); /* Allocate memory to hold the list of group ids. */ - ncids = emalloc((numgrps + 1) * sizeof(int)); + ncids = emalloc((size_t)(numgrps + 1) * sizeof(int)); /* Get the list of group ids. */ NC_CHECK( nc_inq_grps(ncid, NULL, ncids) ); @@ -691,7 +691,7 @@ make_lvars(char *optarg, int *nlvarsp, char ***lvarsp) if (*cp == ',') nvars++; *nlvarsp = nvars; - *lvarsp = (char **) emalloc(nvars * sizeof(char*)); + *lvarsp = (char **) emalloc((size_t)nvars * sizeof(char*)); cpp = *lvarsp; /* copy variable names into list */ for (cp = strtok(optarg, ","); cp != NULL; cp = strtok((char *) NULL, ",")) { @@ -712,7 +712,7 @@ make_lgrps(char *optarg, int *nlgrps, char ***lgrpsp, idnode_t **grpidsp) if (*cp == ',') ngrps++; *nlgrps = ngrps; - *lgrpsp = (char **) emalloc(ngrps * sizeof(char*)); + *lgrpsp = (char **) emalloc((size_t)ngrps * sizeof(char*)); cpp = *lgrpsp; /* copy group names into list */ for (cp = strtok(optarg, ","); cp != NULL; cp = strtok((char *) NULL, ",")) { @@ -852,7 +852,7 @@ nc_next_giter(ncgiter_t *iterp, int *grpidp) { *grpidp = gs_pop(iterp); NC_CHECK(nc_inq_grps2(*grpidp, &numgrps, NULL)); if(numgrps > 0) { - grpids = (int *)emalloc(sizeof(int) * numgrps); + grpids = (int *)emalloc(sizeof(int) * (size_t)numgrps); NC_CHECK(nc_inq_grps2(*grpidp, &numgrps, grpids)); for(i = numgrps - 1; i >= 0; i--) { /* push ids on stack in reverse order */ gs_push(iterp, grpids[i]); diff --git a/ncdump/vardata.c b/ncdump/vardata.c index 968d0dd10..313b7977f 100644 --- a/ncdump/vardata.c +++ b/ncdump/vardata.c @@ -451,8 +451,8 @@ print_rows( marks_pending++; /* matching "}"s to emit after last "row" */ } if(rank - level > 1) { /* this level is just d0 next levels */ - size_t *local_cor = emalloc((rank + 1) * sizeof(size_t)); - size_t *local_edg = emalloc((rank + 1) * sizeof(size_t)); + size_t *local_cor = emalloc((size_t)(rank + 1) * sizeof(size_t)); + size_t *local_edg = emalloc((size_t)(rank + 1) * sizeof(size_t)); for(i = 0; i < rank; i++) { local_cor[i] = cor[i]; local_edg[i] = edg[i]; @@ -546,9 +546,9 @@ vardata( int level = 0; int marks_pending = 0; - cor = (size_t *) emalloc((1 + vrank) * sizeof(size_t)); - edg = (size_t *) emalloc((1 + vrank) * sizeof(size_t)); - add = (size_t *) emalloc((1 + vrank) * sizeof(size_t)); + cor = (size_t *) emalloc((size_t)(1 + vrank) * sizeof(size_t)); + edg = (size_t *) emalloc((size_t)(1 + vrank) * sizeof(size_t)); + add = (size_t *) emalloc((size_t)(1 + vrank) * sizeof(size_t)); nels = 1; if(vrank == 0) { /*scalar*/ @@ -733,9 +733,9 @@ vardatax( size_t nrows; int vrank = vp->ndims; - cor = (size_t *) emalloc((vrank + 1) * sizeof(size_t)); - edg = (size_t *) emalloc((vrank + 1) * sizeof(size_t)); - add = (size_t *) emalloc((vrank + 1) * sizeof(size_t)); + cor = (size_t *) emalloc((size_t)(vrank + 1) * sizeof(size_t)); + edg = (size_t *) emalloc((size_t)(vrank + 1) * sizeof(size_t)); + add = (size_t *) emalloc((size_t)(vrank + 1) * sizeof(size_t)); nels = 1; for (id = 0; id < vrank; id++) { diff --git a/ncgen/bindata.c b/ncgen/bindata.c index 49ead4dfc..1482176d0 100644 --- a/ncgen/bindata.c +++ b/ncgen/bindata.c @@ -5,6 +5,7 @@ #include "includes.h" #include "nclog.h" +#include #ifdef ENABLE_BINARY @@ -81,7 +82,7 @@ bin_constant(Generator* generator, Symbol* sym, NCConstant* con, Bytebuffer* buf } break; case NC_NIL: case NC_STRING: { - int len = (size_t)con->value.stringv.len; + size_t len = (size_t)con->value.stringv.len; if(len == 0 && con->value.stringv.stringv == NULL) { char* nil = NULL; bbAppendn(buf,(void*)&nil,sizeof(nil)); @@ -351,8 +352,8 @@ bin_generate_data_r(NCConstant* instance, Symbol* tsym, Datalist* fillvalue, Byt NCConstant* tmp = nullconst(); tmp->nctype = NC_STRING; convert1(instance,tmp); - p = emalloc(tmp->value.stringv.len+1); - memcpy(p,tmp->value.stringv.stringv,tmp->value.stringv.len); + p = emalloc((size_t)tmp->value.stringv.len+1); + memcpy(p,tmp->value.stringv.stringv, (size_t)tmp->value.stringv.len); p[tmp->value.stringv.len] = '\0'; bbAppendn(databuf,&p,sizeof(char*)); reclaimconstant(tmp); diff --git a/ncgen/cdata.c b/ncgen/cdata.c index b9bc6d78e..0b9c2296f 100644 --- a/ncgen/cdata.c +++ b/ncgen/cdata.c @@ -4,6 +4,7 @@ *********************************************************************/ #include "includes.h" +#include #ifdef ENABLE_C @@ -98,8 +99,7 @@ c_constant(Generator* generator, Symbol* sym, NCConstant* con, Bytebuffer* buf,. } break; case NC_OPAQUE: { char* p; - int bslen; - bslen=(4*con->value.opaquev.len); + size_t bslen = (size_t)(4*con->value.opaquev.len); special = poolalloc(bslen+2+1); strcpy(special,"\""); p = con->value.opaquev.stringv; diff --git a/ncgen/cmldata.c b/ncgen/cmldata.c index f2034b495..07931d0dc 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 8224ea6ad..dfd033087 100644 --- a/ncgen/cvt.c +++ b/ncgen/cvt.c @@ -450,66 +450,66 @@ case CASE(NC_STRING,NC_CHAR): case CASE(NC_STRING,NC_STRING): /* Need to watch out for embedded NULs */ tmp.stringv.len = src->value.stringv.len; - tmp.stringv.stringv = (char*)ecalloc(src->value.stringv.len+1); + tmp.stringv.stringv = (char*)ecalloc((size_t)src->value.stringv.len+1); memcpy((void*)tmp.stringv.stringv, (void*)src->value.stringv.stringv, - tmp.stringv.len); + (size_t)tmp.stringv.len); tmp.stringv.stringv[tmp.stringv.len] = '\0'; break; /* 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; @@ -559,8 +559,8 @@ case CASE(NC_OPAQUE,NC_DOUBLE): tmp.doublev = *(double*)bytes; break; case CASE(NC_OPAQUE,NC_OPAQUE): - tmp.opaquev.stringv = (char*)ecalloc(src->value.opaquev.len+1); - memcpy(tmp.opaquev.stringv,src->value.opaquev.stringv,src->value.opaquev.len); + tmp.opaquev.stringv = (char*)ecalloc((size_t)src->value.opaquev.len+1); + memcpy(tmp.opaquev.stringv,src->value.opaquev.stringv, (size_t)src->value.opaquev.len); tmp.opaquev.len = src->value.opaquev.len; tmp.opaquev.stringv[tmp.opaquev.len] = '\0'; break; diff --git a/ncgen/data.c b/ncgen/data.c index b48f69ff5..fdd8db2ea 100644 --- a/ncgen/data.c +++ b/ncgen/data.c @@ -9,6 +9,7 @@ #include "ncoffsets.h" #include "netcdf_aux.h" #include "dump.h" +#include #undef VERIFY #ifndef __MINGW32__ @@ -147,17 +148,17 @@ cloneconstant(NCConstant* con) if(newcon->value.stringv.len == 0) s = NULL; else { - s = (char*)ecalloc(newcon->value.stringv.len+1); + s = (char*)ecalloc((size_t)newcon->value.stringv.len+1); if(newcon->value.stringv.len > 0) - memcpy(s,newcon->value.stringv.stringv,newcon->value.stringv.len); + memcpy(s,newcon->value.stringv.stringv, (size_t)newcon->value.stringv.len); s[newcon->value.stringv.len] = '\0'; } newcon->value.stringv.stringv = s; break; case NC_OPAQUE: - s = (char*)ecalloc(newcon->value.opaquev.len+1); + s = (char*)ecalloc((size_t)newcon->value.opaquev.len+1); if(newcon->value.opaquev.len > 0) - memcpy(s,newcon->value.opaquev.stringv,newcon->value.opaquev.len); + memcpy(s,newcon->value.opaquev.stringv, (size_t)newcon->value.opaquev.len); s[newcon->value.opaquev.len] = '\0'; newcon->value.opaquev.stringv = s; break; @@ -603,8 +604,8 @@ builddatalist(int initial) initial++; /* for header*/ ci = (Datalist*)ecalloc(sizeof(Datalist)); if(ci == NULL) semerror(0,"out of memory\n"); - ci->data = (NCConstant**)ecalloc(sizeof(NCConstant*)*initial); - ci->alloc = initial; + ci->data = (NCConstant**)ecalloc(sizeof(NCConstant*) * (size_t)initial); + ci->alloc = (size_t)initial; ci->length = 0; return ci; } @@ -645,12 +646,12 @@ dlinsert(Datalist* dl, size_t pos, Datalist* insertion) int len1 = datalistlen(dl); int len2 = datalistlen(insertion); int delta = len1 - pos; - dlsetalloc(dl,len2+len1+1); + dlsetalloc(dl, len2+len1+1); /* move contents of dl up to make room for insertion */ if(delta > 0) - memmove(&dl->data[pos+len2],&dl->data[pos],delta*sizeof(NCConstant*)); + memmove(&dl->data[pos+len2],&dl->data[pos], (size_t)delta*sizeof(NCConstant*)); dl->length += len2; for(i=0;idata[i]; diff --git a/ncgen/dump.c b/ncgen/dump.c index bd4d775b8..024f92c41 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/escapes.c b/ncgen/escapes.c index 94efd0f2f..2b7ff8642 100644 --- a/ncgen/escapes.c +++ b/ncgen/escapes.c @@ -6,6 +6,7 @@ #include "includes.h" #include "ncutf8.h" +#include #define HEXCHARS "0123456789abcdefABCDEF" #define OCTCHARS "01234567" @@ -155,7 +156,7 @@ static struct { {'/', "_SLASH_"}, }; static int idtlen; -static int hexlen; +static size_t hexlen; static Bytebuffer* newname; static void @@ -508,7 +509,7 @@ fqnescape(const char* s) const char* p; char* q; int c; - int l = strlen(s); + size_t l = strlen(s); /* 1234567 @@ -617,8 +618,8 @@ unescape( char* p; int b; - s = (char*)emalloc(yyleng+1); - memcpy(s,yytext,yyleng); + s = (char*)emalloc((size_t)yyleng+1); + memcpy(s,yytext, (size_t)yyleng); s[yyleng] = '\0'; /* translate "\" escapes, e.g. "\t" to tab character */ diff --git a/ncgen/f77data.c b/ncgen/f77data.c index e4863af2a..28521890a 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/genchar.c b/ncgen/genchar.c index 3f5c381a2..25ff0622f 100644 --- a/ncgen/genchar.c +++ b/ncgen/genchar.c @@ -245,8 +245,8 @@ makeconst(int lineno, int len, char* str) con->filled = 0; con->value.stringv.len = len; /* We cannot use strdup because str might have embedded nuls */ - con->value.stringv.stringv = (char*)ecalloc(len+1); - memcpy((void*)con->value.stringv.stringv,(void*)str,len); + con->value.stringv.stringv = (char*)ecalloc((size_t)len+1); + memcpy((void*)con->value.stringv.stringv,(void*)str, (size_t)len); con->value.stringv.stringv[len] = '\0'; return con; } diff --git a/ncgen/gencml.c b/ncgen/gencml.c index b88b08017..912cc4975 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/generate.c b/ncgen/generate.c index c08f488be..339993233 100644 --- a/ncgen/generate.c +++ b/ncgen/generate.c @@ -6,6 +6,7 @@ #include "includes.h" #include "ncoffsets.h" #include "netcdf_aux.h" +#include /**************************************************/ /* Code for generating data lists*/ @@ -251,22 +252,22 @@ generate_fieldarray(Symbol* basetype, NCConstant* con, Dimset* dimset, static void normalizeopaquelength(NCConstant* prim, unsigned long nbytes) { - int nnibs = 2*nbytes; + size_t nnibs = 2*nbytes; ASSERT(prim->nctype==NC_OPAQUE); if(prim->value.opaquev.len == nnibs) { /* do nothing*/ } else if(prim->value.opaquev.len > nnibs) { /* truncate*/ prim->value.opaquev.stringv[nnibs] = '\0'; - prim->value.opaquev.len = nnibs; + prim->value.opaquev.len = (int)nnibs; } else {/* prim->value.opaquev.len < nnibs => expand*/ char* s; s = (char*)ecalloc(nnibs+1); memset(s,'0',nnibs); /* Fill with '0' characters */ - memcpy(s,prim->value.opaquev.stringv,prim->value.opaquev.len); + memcpy(s,prim->value.opaquev.stringv, (size_t)prim->value.opaquev.len); s[nnibs] = '\0'; efree(prim->value.opaquev.stringv); prim->value.opaquev.stringv=s; - prim->value.opaquev.len = nnibs; + prim->value.opaquev.len = (int)nnibs; } } diff --git a/ncgen/genjjni.c b/ncgen/genjjni.c index 86c8682b4..03e0166f7 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 f5edb8d7e..9faf6ae4f 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 c46fb3863..963ecefcc 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 10f3e36ea..c8c2d5ac6 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 6ea3a2270..af03296e1 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 105a221b4..a6ecf191a 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; @@ -3318,13 +3318,12 @@ makeconstdata(nc_type nctype) #ifdef USE_NETCDF4 case NC_OPAQUE: { char* s; - int len; - len = bbLength(lextext); + size_t len = bbLength(lextext); s = (char*)ecalloc(len+1); strncpy(s,bbContents(lextext),len); s[len] = '\0'; con->value.opaquev.stringv = s; - con->value.opaquev.len = len; + con->value.opaquev.len = (int)len; } break; case NC_NIL: diff --git a/ncgen/odom.c b/ncgen/odom.c index 1f96828d6..93e9a34ec 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/ncgen/util.c b/ncgen/util.c index 108fff044..e5c73362d 100644 --- a/ncgen/util.c +++ b/ncgen/util.c @@ -5,6 +5,7 @@ *********************************************************************/ #include "includes.h" +#include /* Track primitive symbol instances (initialized in ncgen.y) */ Symbol* primsymbols[PRIMNO]; @@ -12,7 +13,7 @@ Symbol* primsymbols[PRIMNO]; char* append(const char* s1, const char* s2) { - int len = (s1?strlen(s1):0)+(s2?strlen(s2):0); + size_t len = (s1?strlen(s1):0)+(s2?strlen(s2):0); char* result = (char*)ecalloc(len+1); result[0] = '\0'; if(s1) strcat(result,s1); @@ -174,8 +175,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 +197,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; } @@ -369,7 +372,7 @@ collectpath(Symbol* grp, List* grpstack) char* prefixtostring(List* prefix, char* separator) { - int slen=0; + size_t slen=0; int plen; int i; char* result; @@ -439,7 +442,7 @@ prefixdup(List* prefix) int i; if(prefix == NULL) return listnew(); dupseq = listnew(); - listsetalloc(dupseq,listlength(prefix)); + listsetalloc(dupseq, (size_t)listlength(prefix)); for(i=0;i #include #include #include @@ -129,6 +130,7 @@ cstring( int *intp; float *floatp; double *doublep; + size_t cp_size; switch (type) { case NC_CHAR: @@ -162,34 +164,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: @@ -225,7 +232,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 +244,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 +252,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 +272,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 +288,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 +309,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 +325,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 +354,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 +368,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 +376,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 +384,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 +403,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 +416,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 +425,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 +617,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 +648,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 +666,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 +690,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 +726,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 +737,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 +757,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 +772,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 +787,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 +805,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 +818,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 +826,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, @@ -1046,36 +1053,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; @@ -1206,7 +1219,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 +1252,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 +1359,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 +1385,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 +1407,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 +1426,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 +1439,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 +1456,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 +1472,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 +1520,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 +1534,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); @@ -1756,7 +1769,7 @@ grow_iarray( } if (! pow2(nar)) /* return unless nar is a power of two */ return; - *arpp = (int *) erealloc(*arpp, 2 * nar * sizeof(int)); + *arpp = (int *) erealloc(*arpp, 2 * (size_t)nar * sizeof(int)); } @@ -1782,7 +1795,7 @@ grow_varray( } if (! pow2(nar)) /* return unless nar is a power of two */ return; - *arpp = (struct vars *) erealloc(*arpp, 2 * nar * sizeof(struct vars)); + *arpp = (struct vars *) erealloc(*arpp, 2 * (size_t)nar * sizeof(struct vars)); } @@ -1808,7 +1821,7 @@ grow_darray( } if (! pow2(nar)) /* return unless nar is a power of two */ return; - *arpp = (struct dims *) erealloc(*arpp, 2 * nar * sizeof(struct dims)); + *arpp = (struct dims *) erealloc(*arpp, 2 * (size_t)nar * sizeof(struct dims)); } @@ -1834,7 +1847,7 @@ grow_aarray( } if (! pow2(nar)) /* return unless nar is a power of two */ return; - *arpp = (struct atts *) erealloc(*arpp, 2 * nar * sizeof(struct atts)); + *arpp = (struct atts *) erealloc(*arpp, 2 * (size_t)nar * sizeof(struct atts)); } @@ -1899,9 +1912,9 @@ decodify ( /* {'_', "_UNDERSCORE_"} */ }; static int idtlen; - static int hexlen; + static size_t hexlen; int nctable = (sizeof(ctable))/(sizeof(ctable[0])); - int newlen; + size_t newlen; idtlen = strlen("DIGIT_n_"); /* initial digit template */ hexlen = 1+strlen("_XHH"); /* template for hex of non-ASCII bytes */ diff --git a/ncgen3/load.c b/ncgen3/load.c index 634751d66..df0db88ee 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 42490f72b..9c304f403 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 6178f25d0..1db8ff1d6 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 494a62642..2e87109d0 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/add.c b/nctest/add.c index 5a6ee58d2..44e971970 100644 --- a/nctest/add.c +++ b/nctest/add.c @@ -23,9 +23,7 @@ struct netcdf test; /* */ void -add_dim (test, idim) /* add the dimension idim to the netcdf test */ - struct netcdf *test; - struct cdfdim *idim; +add_dim (struct netcdf *test, struct cdfdim *idim) /* add the dimension idim to the netcdf test */ { static char pname[] = "add_dim"; @@ -43,9 +41,7 @@ add_dim (test, idim) /* add the dimension idim to the netcdf test */ } void -add_var (test, ivar) /* add the variable ivar to the netcdf test */ - struct netcdf *test; - struct cdfvar *ivar; +add_var (struct netcdf *test, struct cdfvar *ivar) /* add the variable ivar to the netcdf test */ { static char pname[] = "add_var"; int i; @@ -60,7 +56,7 @@ add_var (test, ivar) /* add the variable ivar to the netcdf test */ (void) strcpy(test->vars[test->nvars].name, ivar->name); test->vars[test->nvars].type = ivar->type; test->vars[test->nvars].ndims = ivar->ndims; - test->vars[test->nvars].dims = (int *) emalloc(sizeof(int)*ivar->ndims); + test->vars[test->nvars].dims = (int *) emalloc(sizeof(int)*(size_t)ivar->ndims); for (i = 0; i < ivar->ndims; i++) test->vars[test->nvars].dims[i] = ivar->dims[i]; test->vars[test->nvars].natts = 0; @@ -68,10 +64,7 @@ add_var (test, ivar) /* add the variable ivar to the netcdf test */ } void -add_att (test, varid, iatt) /* add attribute iatt to the netcdf test */ - struct netcdf *test; - int varid; /* variable id */ - struct cdfatt *iatt; +add_att (struct netcdf *test, int varid, struct cdfatt *iatt) /* add attribute iatt to the netcdf test */ { static char pname[] = "add_att"; int ia; /* attribute number */ @@ -108,8 +101,7 @@ add_att (test, varid, iatt) /* add attribute iatt to the netcdf test */ void -add_reset(test) /* reset in-memory netcdf test to empty */ - struct netcdf *test; +add_reset(struct netcdf *test) /* reset in-memory netcdf test to empty */ { test->ndims = 0; test->nvars = 0; @@ -120,10 +112,7 @@ add_reset(test) /* reset in-memory netcdf test to empty */ void -del_att (test, varid, iatt) /* delete attribute iatt in the netcdf test */ - struct netcdf *test; - int varid; /* variable id */ - struct cdfatt *iatt; +del_att (struct netcdf *test, int varid, struct cdfatt *iatt) /* delete attribute iatt in the netcdf test */ { static char pname[] = "del_att"; int ia, ib; /* attribute number */ @@ -153,11 +142,7 @@ del_att (test, varid, iatt) /* delete attribute iatt in the netcdf test */ } void -add_data(test, varid, start, edges) /* keep max record written updated */ - struct netcdf *test; - int varid; - long start[]; - long edges[]; +add_data(struct netcdf *test, int varid, long start[], long edges[]) /* keep max record written updated */ { if (varid != test->xdimid) /* not a record variable */ return; @@ -167,9 +152,7 @@ add_data(test, varid, start, edges) /* keep max record written updated */ void -errvar(cdfp, varp) - struct netcdf *cdfp; - struct cdfvar *varp; +errvar(struct netcdf *cdfp, struct cdfvar *varp) { const char *types; int id; diff --git a/nctest/atttests.c b/nctest/atttests.c index f9d263ffe..4200c4bc5 100644 --- a/nctest/atttests.c +++ b/nctest/atttests.c @@ -41,8 +41,7 @@ * try with bad netCDF handle, check error */ int -test_ncattput(path) - const char *path; /* name of writable netcdf file to open */ +test_ncattput(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncattput"; @@ -125,7 +124,7 @@ test_ncattput(path) ncclose(cdfid); return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type)); + tmp.val = emalloc((size_t)(atts[ia].len * nctypelen(atts[ia].type))); if (ncattget(cdfid, NC_GLOBAL, atts[ia].name, tmp.val) == -1) { error("%s: ncattget of variable attribute failed", pname); ncclose(cdfid); return ++nerrs; @@ -138,7 +137,7 @@ test_ncattput(path) free (tmp.val); } /* add a variable, then variable attributes of every type */ - ww.dims = (int *) emalloc(sizeof(int) * ww.ndims); + ww.dims = (int *) emalloc(sizeof(int) * (size_t)ww.ndims); for (id = 0; id < ww.ndims; id++) ww.dims[id] = id; if ((ww_id = ncvardef(cdfid, @@ -169,7 +168,7 @@ test_ncattput(path) ncclose(cdfid); return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type)); + tmp.val = emalloc((size_t)(atts[ia].len * nctypelen(atts[ia].type))); if (ncattget(cdfid, ww_id, atts[ia].name, tmp.val) == -1) { error("%s: ncattget of variable attribute failed", pname); ncclose(cdfid); return ++nerrs; @@ -213,7 +212,7 @@ test_ncattput(path) ncclose(cdfid); return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type)); + tmp.val = emalloc((size_t)(atts[ia].len * nctypelen(atts[ia].type))); if (ncattget(cdfid, ww_id, atts[ia-1].name, tmp.val) == -1) { error("%s: ncattget of variable attribute failed", pname); ncclose(cdfid); return ++nerrs; @@ -294,7 +293,7 @@ test_ncattput(path) ncclose(cdfid); return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type)); + tmp.val = emalloc((size_t)(atts[ia].len * nctypelen(atts[ia].type))); if (ncattget(cdfid, ww_id, atts[ia+1].name, tmp.val) == -1) { error("%s: ncattget of variable attribute failed in data mode", pname); @@ -341,8 +340,7 @@ test_ncattput(path) * try with bad netCDF handle, check error */ int -test_ncattinq(path) - const char *path; /* name of writable netcdf file to open */ +test_ncattinq(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncattinq"; @@ -386,7 +384,7 @@ test_ncattinq(path) ncclose(cdfid); return ++nerrs; } /* in define mode, add a variable */ - vv.dims = (int *) emalloc(sizeof(int) * vv.ndims); + vv.dims = (int *) emalloc(sizeof(int) * (size_t)vv.ndims); for (id = 0; id < vv.ndims; id++) vv.dims[id] = id; /* assumes vv.ndims <= test.ndims */ if ((vv_id = ncvardef(cdfid, vv.name, vv.type, vv.ndims, vv.dims)) @@ -456,8 +454,7 @@ test_ncattinq(path) * try with bad netCDF handle, check error */ int -test_ncattget(path) - const char *path; /* name of writable netcdf file to open */ +test_ncattget(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; int cdfid; /* netcdf id */ @@ -484,7 +481,7 @@ test_ncattget(path) ncclose(cdfid); return ++nerrs; } /* add a variable */ - uu.dims = (int *) emalloc(sizeof(int) * uu.ndims); + uu.dims = (int *) emalloc(sizeof(int) * (size_t)uu.ndims); for (id = 0; id < uu.ndims; id++) uu.dims[id] = id; if ((uu_id = ncvardef(cdfid, @@ -520,7 +517,7 @@ test_ncattget(path) ncclose(cdfid); return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(tmp.len * nctypelen(tmp.type)); + tmp.val = emalloc((size_t)(tmp.len * nctypelen(tmp.type))); if (ncattget(cdfid, test.atts[ia].var, test.atts[ia].name, tmp.val) == -1) { error("%s: ncattget of variable attribute failed in define mode", @@ -565,7 +562,7 @@ test_ncattget(path) return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(tmp.len * nctypelen(tmp.type)); + tmp.val = emalloc((size_t)(tmp.len * nctypelen(tmp.type))); if (ncattget(cdfid, test.atts[ia].var, test.atts[ia].name, tmp.val) == -1) { error("%s: ncattget of variable attribute failed in data mode", @@ -627,10 +624,10 @@ test_ncattget(path) * try with bad source or target netCDF handles, check error * try with bad source or target variable handle, check error */ +/* path1: name of input netcdf file to open */ +/* path2: name of output netcdf file to create */ int -test_ncattcopy(path1, path2) - const char *path1; /* name of input netcdf file to open */ - const char *path2; /* name of output netcdf file to create */ +test_ncattcopy(const char *path1, const char *path2) { int nerrs = 0; static char pname[] = "test_ncattcopy"; @@ -667,7 +664,7 @@ test_ncattcopy(path1, path2) ncclose(cdfid); return ++nerrs; } add_att(&test, NC_GLOBAL, &att); /* keep in-memory netcdf consistent */ - tt.dims = (int *) emalloc(sizeof(int) * tt.ndims); + tt.dims = (int *) emalloc(sizeof(int) * (size_t)tt.ndims); for (id=0; id < tt.ndims; id++) tt.dims[0] = id; if ((tt_id=ncvardef(cdfid, tt.name, tt.type, tt.ndims, tt.dims)) == -1) { @@ -681,7 +678,7 @@ test_ncattcopy(path1, path2) } add_att(&test, tt_id, &att); /* keep in-memory netcdf consistent */ - tu.dims = (int *) emalloc(sizeof(int) * tu.ndims); + tu.dims = (int *) emalloc(sizeof(int) * (size_t)tu.ndims); for (id = 0; id < tu.ndims; id++) tu.dims[id] = id; if ((tu_id=ncvardef(cdfid, tu.name, tu.type, tu.ndims, tu.dims)) == -1) { @@ -732,7 +729,7 @@ test_ncattcopy(path1, path2) ncclose(cdfid); ncclose(cdfid2); return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(att.len * nctypelen(att.type)); + tmp.val = emalloc((size_t)(att.len * nctypelen(att.type))); if (ncattget(cdfid2, NC_GLOBAL, att.name, tmp.val) == -1) { error("%s: ncattget of variable attribute failed", pname); ncclose(cdfid); ncclose(cdfid2); return ++nerrs; @@ -758,7 +755,7 @@ test_ncattcopy(path1, path2) ncclose(cdfid); ncclose(cdfid2); return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(att.len * nctypelen(att.type)); + tmp.val = emalloc((size_t)(att.len * nctypelen(att.type))); if (ncattget(cdfid2, tu2_id, att.name, tmp.val) == -1) { error("%s: ncattget of variable attribute failed", pname); ncclose(cdfid); ncclose(cdfid2); return ++nerrs; @@ -801,7 +798,7 @@ test_ncattcopy(path1, path2) ncclose(cdfid); ncclose(cdfid2); return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(att2.len * nctypelen(att2.type)); + tmp.val = emalloc((size_t)(att2.len * nctypelen(att2.type))); if (ncattget(cdfid2, tu2_id, att2.name, tmp.val) == -1) { error("%s: ncattget of variable attribute failed", pname); ncclose(cdfid); ncclose(cdfid2); return ++nerrs; @@ -830,7 +827,7 @@ test_ncattcopy(path1, path2) ncclose(cdfid); ncclose(cdfid2); return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(att2.len * nctypelen(att2.type)); + tmp.val = emalloc((size_t)(att2.len * nctypelen(att2.type))); if (ncattget(cdfid, tu_id, att2.name, tmp.val) == -1) { error("%s: ncattget of variable attribute failed", pname); ncclose(cdfid); ncclose(cdfid2); return ++nerrs; @@ -905,8 +902,7 @@ test_ncattcopy(path1, path2) * try with bad attribute number, check error */ int -test_ncattname(path) - const char *path; /* name of writable netcdf file to open */ +test_ncattname(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncattname"; @@ -1091,8 +1087,7 @@ test_ncattname(path) * try with bad netCDF handle, check error */ int -test_ncattrename(path) - const char *path; /* name of writable netcdf file to open */ +test_ncattrename(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncattrename"; @@ -1149,7 +1144,7 @@ test_ncattrename(path) ncclose(cdfid); return ++nerrs; } /* allocate space to hold the attribute value to be retrieved */ - tmp.val = emalloc(atty.len * nctypelen(atty.type)); + tmp.val = emalloc((size_t)(atty.len * nctypelen(atty.type))); if (ncattget(cdfid, NC_GLOBAL, newname, tmp.val) == -1) { error("%s: ncattget of variable attribute failed", pname); ncclose(cdfid); return ++nerrs; @@ -1234,8 +1229,7 @@ test_ncattrename(path) * try in data mode, check error */ int -test_ncattdel(path) - const char *path; /* name of writable netcdf file to open */ +test_ncattdel(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncattdel"; @@ -1273,7 +1267,7 @@ test_ncattdel(path) ncclose(cdfid); return ++nerrs; } add_att(&test, NC_GLOBAL, &yaa); /* keep in-memory netcdf in sync */ - yav.dims = (int *) emalloc(sizeof(int) * yav.ndims); + yav.dims = (int *) emalloc(sizeof(int) * (size_t)yav.ndims); for (id = 0; id < yav.ndims; id++) yav.dims[id] = id; if ((yav_id=ncvardef(cdfid, yav.name, yav.type, yav.ndims, yav.dims)) diff --git a/nctest/cdftests.c b/nctest/cdftests.c index 5f3a637ce..3442cc402 100644 --- a/nctest/cdftests.c +++ b/nctest/cdftests.c @@ -28,8 +28,7 @@ * Uses: nccreate, ncendef, ncclose, ncopen. */ int -test_nccreate(path) - const char *path; /* name of netCDF file to create */ +test_nccreate(const char *path) /* name of netCDF file to create */ { int nerrs = 0; static char pname[] = "test_nccreate"; @@ -94,8 +93,7 @@ test_nccreate(path) #define DATA_LEN 32 #define TEMP_FILE_NAME "temp.tmp" int -test_ncopen(path) - const char *path; /* name of writable netcdf file to open */ +test_ncopen(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncopen"; @@ -240,8 +238,7 @@ test_ncopen(path) * Uses: ncopen, ncredef, ncdimdef, ncvardef, ncattput, ncclose */ int -test_ncredef(path) - const char *path; /* name of writable netcdf file to open */ +test_ncredef(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncredef"; @@ -275,7 +272,7 @@ test_ncredef(path) add_dim(&test, &ii); /* keep in-memory netcdf in sync */ /* dimension added OK, add a variable */ - aa.dims = (int *)emalloc(sizeof(int) * aa.ndims); + aa.dims = (int *)emalloc(sizeof(int) * (size_t)aa.ndims); aa.dims[0] = ii_dim; if ((aa_id = ncvardef(ncid, aa.name, aa.type, aa.ndims, aa.dims)) == -1) { @@ -328,8 +325,7 @@ test_ncredef(path) * Uses: ncopen, ncredef, ncdimdef, ncvardef, ncattput, ncendef, ncclose */ int -test_ncendef(path) - const char *path; /* name of writable netcdf file to open */ +test_ncendef(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncendef"; @@ -367,7 +363,7 @@ test_ncendef(path) add_dim(&test, &kk); /* keep in-memory netcdf in sync */ /* dimensions added OK, add a variable */ - bb.dims = (int *) emalloc(sizeof(int) * bb.ndims); + bb.dims = (int *) emalloc(sizeof(int) * (size_t)bb.ndims); bb.dims[0] = kk_dim; bb.dims[1] = jj_dim; if ((bb_id = ncvardef(ncid, bb.name, bb.type, @@ -422,8 +418,7 @@ test_ncendef(path) * On exit netcdf files are closed. */ int -test_ncclose(path) - const char *path; /* name of writable netcdf file to open */ +test_ncclose(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncclose"; @@ -476,8 +471,7 @@ test_ncclose(path) * On exit netcdf files are closed. */ int -test_ncinquire(path) - const char *path; /* name of writable netcdf file to open */ +test_ncinquire(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncinquire"; @@ -562,7 +556,7 @@ test_ncinquire(path) /* add some record variables */ for (iv = 0; iv < nv; iv++) { - cc[iv].dims = (int *) emalloc(sizeof(int) * cc[iv].ndims); + cc[iv].dims = (int *) emalloc(sizeof(int) * (size_t)cc[iv].ndims); cc[iv].dims[0] = rec_dim; /* first dimension unlimited */ for (id = 1; id < cc[iv].ndims; id++) cc[iv].dims[id] = dimids[id]; @@ -640,8 +634,7 @@ test_ncinquire(path) * On exit netcdf files are closed. */ int -test_ncsync(path) - const char *path; /* name of writable netcdf file to open */ +test_ncsync(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncsync"; @@ -676,7 +669,7 @@ test_ncsync(path) } add_dim(&test, &ll); - dd.dims = (int *) emalloc(sizeof(int) * dd.ndims); + dd.dims = (int *) emalloc(sizeof(int) * (size_t)dd.ndims); dd.dims[0] = ll_dim; if ((dd_id=ncvardef(ncid0, dd.name, dd.type, dd.ndims, dd.dims)) == -1) { error("%s: ncvardef failed", pname); @@ -781,8 +774,7 @@ test_ncsync(path) * On exit netcdf files are closed. */ int -test_ncabort(path) - const char *path; /* name of writable netcdf file to open */ +test_ncabort(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncabort"; diff --git a/nctest/dimtests.c b/nctest/dimtests.c index 26bce98d2..4e3ae4187 100644 --- a/nctest/dimtests.c +++ b/nctest/dimtests.c @@ -25,8 +25,7 @@ * try to define a second unlimited dimension, check error */ int -test_ncdimdef(path) - const char *path; /* name of writable netcdf to open */ +test_ncdimdef(const char *path) /* name of writable netcdf to open */ { int nerrs = 0; static char pname[] = "test_ncdimdef"; @@ -129,8 +128,7 @@ test_ncdimdef(path) * try with bad handle, check error */ int -test_ncdimid(path) - const char *path; /* name of writable netcdf file to open */ +test_ncdimid(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncdimid"; @@ -206,8 +204,7 @@ test_ncdimid(path) * try with bad netCDF handle, check error */ int -test_ncdiminq(path) - const char *path; /* name of writable netcdf file to open */ +test_ncdiminq(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncdiminq"; @@ -303,8 +300,7 @@ test_ncdiminq(path) * try with bad netCDF handle, check error */ int -test_ncdimrename(path) - const char *path; /* name of writable netcdf file to open */ +test_ncdimrename(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncdimrename"; diff --git a/nctest/driver.c b/nctest/driver.c index 716a17809..20c68b103 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/nctest/emalloc.c b/nctest/emalloc.c index 05dc1c51c..570a419a7 100644 --- a/nctest/emalloc.c +++ b/nctest/emalloc.c @@ -12,8 +12,7 @@ #include "emalloc.h" void * -emalloc (size) /* check return from malloc */ - size_t size; +emalloc (size_t size) /* check return from malloc */ { void *p; @@ -32,9 +31,7 @@ emalloc (size) /* check return from malloc */ } void * -erealloc (ptr, size) /* check return from realloc */ - void *ptr; - size_t size; +erealloc (void *ptr, size_t size) /* check return from realloc */ { void *p; diff --git a/nctest/rec.c b/nctest/rec.c index a56c6aafd..224b02326 100644 --- a/nctest/rec.c +++ b/nctest/rec.c @@ -20,9 +20,7 @@ * error. */ static int -numrecvars(ncid, recvarids) - int ncid; - int *recvarids; +numrecvars(int ncid, int *recvarids) { int ndims, iv, nvars; int nrecvars; @@ -52,9 +50,7 @@ numrecvars(ncid, recvarids) * variable id. Returns 0 if not a record variable. Returns -1 on error. */ static long -ncrecsize(ncid,vid) - int ncid; - int vid; +ncrecsize(int ncid,int vid) { int recdimid; nc_type type; @@ -87,11 +83,7 @@ ncrecsize(ncid,vid) * errors better. */ static int -recinq(ncid, nrecvars, recvarids, recsizes) - int ncid; - int *nrecvars; - int *recvarids; - long *recsizes; +recinq(int ncid, int *nrecvars, int *recvarids, long *recsizes) { int iv; int rvarids[MAX_NC_VARS]; @@ -119,8 +111,7 @@ recinq(ncid, nrecvars, recvarids, recsizes) * try with bad netCDF handle, check error */ int -test_ncrecinq(path) - const char *path; /* name of netcdf file to open */ +test_ncrecinq(const char *path) /* name of netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncrecinq"; @@ -225,10 +216,7 @@ test_ncrecinq(path) * an open netCDF file. Returns -1 on error. */ static int -dimsizes(ncid, varid, sizes) - int ncid; - int varid; - long *sizes; +dimsizes(int ncid, int varid, long *sizes) { int ndims; int id; @@ -251,10 +239,7 @@ dimsizes(ncid, varid, sizes) * better. */ static int -recput(ncid, recnum, datap) - int ncid; - long recnum; - void **datap; +recput(int ncid, long recnum, void **datap) { int iv; int rvids[MAX_NC_VARS]; @@ -288,10 +273,7 @@ recput(ncid, recnum, datap) * better. */ static int -recget(ncid, recnum, datap) - int ncid; - long recnum; - void **datap; +recget(int ncid, long recnum, void **datap) { int iv; int rvids[MAX_NC_VARS]; @@ -327,8 +309,7 @@ recget(ncid, recnum, datap) * try with bad netCDF handle, check error */ int -test_ncrecput(path) - const char *path; /* name of writable netcdf file to open */ +test_ncrecput(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncrecput"; @@ -359,8 +340,8 @@ test_ncrecput(path) /* get a block of data of the right type for each record variable */ for (iv = 0; iv < nrvars; iv++) { - datap[iv] = emalloc(rvarsizes[iv]); - datar[iv] = emalloc(rvarsizes[iv]); /* for comparison values */ + datap[iv] = emalloc((size_t)rvarsizes[iv]); + datar[iv] = emalloc((size_t)rvarsizes[iv]); /* for comparison values */ if (ncvarinq(ncid, rvarids[iv], 0, &vartype[iv], 0, 0, 0) == -1) { error("%s: ncvarinq failed", pname); ncclose(ncid); @@ -485,8 +466,7 @@ test_ncrecput(path) * try with bad netCDF handle, check error */ int -test_ncrecget(path) - const char *path; /* name of netcdf file to open */ +test_ncrecget(const char *path) /* name of netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncrecget"; @@ -517,8 +497,8 @@ test_ncrecget(path) /* get a block of data of the right type for each record variable */ for (iv = 0; iv < nrvars; iv++) { - datap[iv] = emalloc(rvarsizes[iv]); - datar[iv] = emalloc(rvarsizes[iv]); /* for comparison values */ + datap[iv] = emalloc((size_t)rvarsizes[iv]); + datar[iv] = emalloc((size_t)rvarsizes[iv]); /* for comparison values */ if (ncvarinq(ncid, rvarids[iv], 0, &vartype[iv], 0, 0, 0) == -1) { error("%s: ncvarinq failed", pname); ncclose(ncid); diff --git a/nctest/slabs.c b/nctest/slabs.c index 48b4d1de7..8bd9a480b 100644 --- a/nctest/slabs.c +++ b/nctest/slabs.c @@ -30,12 +30,12 @@ * * v[ii] = val; */ +/* type: netcdf type of v, NC_BYTE, ..., NC_DOUBLE */ +/* v: array of specified type */ +/* ii: it's v[ii] we want to store into */ +/* val: value to store */ static void -val_stuff(type, v, ii, val) /* v[ii] = val */ - nc_type type; /* netcdf type of v, NC_BYTE, ..., NC_DOUBLE */ - void *v; /* array of specified type */ - int ii; /* it's v[ii] we want to store into */ - long val; /* value to store */ +val_stuff(nc_type type, void *v, int ii, long val) /* v[ii] = val */ { static char pname[] = "val_stuff"; @@ -70,12 +70,12 @@ val_stuff(type, v, ii, val) /* v[ii] = val */ * returns 0 if equal, 1 if not equal */ +/* type: netcdf type of v, NC_BYTE, ..., NC_DOUBLE */ +/* v: array of specified type */ +/* ii: it's v[ii] we want to compare */ +/* val: value to compare with */ static int -val_diff(type, v, ii, val) /* v[ii] != val */ - nc_type type; /* netcdf type of v, NC_BYTE, ..., NC_DOUBLE */ - void *v; /* array of specified type */ - int ii; /* it's v[ii] we want to compare */ - long val; /* value to compare with */ +val_diff(nc_type type, void *v, int ii, long val) /* v[ii] != val */ { static char pname[] = "val_diff"; @@ -109,8 +109,7 @@ val_diff(type, v, ii, val) /* v[ii] != val */ */ int -test_slabs(cdfid) - int cdfid; /* handle of netcdf open and in data mode */ +test_slabs(int cdfid) /* handle of netcdf open and in data mode */ { int nerrs = 0; static char pname[] = "test_slabs"; @@ -157,7 +156,7 @@ test_slabs(cdfid) /* define a multi-dimensional variable of each type */ for (iv = 0; iv < NVARS; iv++) { - va[iv].dims = (int *) emalloc(sizeof(int) * va[iv].ndims); + va[iv].dims = (int *) emalloc(sizeof(int) * (size_t)va[iv].ndims); for (idim = 0; idim < va[iv].ndims; idim++) va[iv].dims[idim] = dimids[idim]; varid[iv] = ncvardef(cdfid, va[iv].name, va[iv].type, va[iv].ndims, @@ -177,7 +176,7 @@ test_slabs(cdfid) for (iv = 0; iv < NVARS; iv++) { /* test each type of variable */ - v = emalloc(WSIZE*XSIZE*YSIZE*ZSIZE * nctypelen(va[iv].type)); + v = emalloc(WSIZE*XSIZE*YSIZE*ZSIZE * (size_t)nctypelen(va[iv].type)); /* fill it with values using a function of dimension indices */ ii = 0; diff --git a/nctest/val.c b/nctest/val.c index b7d27820d..247f33995 100644 --- a/nctest/val.c +++ b/nctest/val.c @@ -13,11 +13,11 @@ /* fill typed value block with values of specified type */ +/* type: netcdf type, NC_BYTE, ..., NC_DOUBLE */ +/* len: number of elements to fill with */ +/* vals: start of first block of values */ void -val_fill(type, len, vals) - nc_type type; /* netcdf type, NC_BYTE, ..., NC_DOUBLE */ - long len; /* number of elements to fill with */ - void *vals; /* start of first block of values */ +val_fill(nc_type type, long len, void *vals) { static char pname[] = "val_fill"; long half = len/2; @@ -64,11 +64,11 @@ val_fill(type, len, vals) /* fill typed value block with zeros of specified type */ +/* type: netcdf type, NC_BYTE, ..., NC_DOUBLE */ +/* len: number of elements to fill with */ +/* vals: start of first block of values */ void -val_fill_zero(type, len, vals) - nc_type type; /* netcdf type, NC_BYTE, ..., NC_DOUBLE */ - long len; /* number of elements to fill with */ - void *vals; /* start of first block of values */ +val_fill_zero(nc_type type, long len, void *vals) { static char pname[] = "val_fill_zero"; int iel; @@ -118,12 +118,12 @@ val_fill_zero(type, len, vals) * compare two typed value blocks, return 0 if equal, 1+n otherwise, * where n is the index of the first differing element. */ +/* type: netcdf type, NC_BYTE, ..., NC_DOUBLE */ +/* len: number of elements of type to compare */ +/* v1: start of first block of values */ +/* v2: start of second block of values */ int -val_cmp (type, len, v1, v2) - nc_type type; /* netcdf type, NC_BYTE, ..., NC_DOUBLE */ - long len; /* number of elements of type to compare */ - void *v1; /* start of first block of values */ - void *v2; /* start of second block of values */ +val_cmp (nc_type type, long len, void *v1, void *v2) { static char pname[] = "val_cmp"; int iel; @@ -195,11 +195,11 @@ val_cmp (type, len, v1, v2) /* print typed value block with values of specified type */ +/* type: netcdf type, NC_BYTE, ..., NC_DOUBLE */ +/* len: number of elements to fill with */ +/* vals: start of first block of values */ void -val_out(type, len, vals) - nc_type type; /* netcdf type, NC_BYTE, ..., NC_DOUBLE */ - long len; /* number of elements to fill with */ - void *vals; /* start of first block of values */ +val_out(nc_type type, long len, void *vals) { static char pname[] = "val_oout"; int iel; diff --git a/nctest/vardef.c b/nctest/vardef.c index b02df5dfd..3d5ac18ca 100644 --- a/nctest/vardef.c +++ b/nctest/vardef.c @@ -105,8 +105,7 @@ init_epsilons() * try in data mode, check error */ int -test_ncvardef(path) - const char *path; /* name of writable netcdf file to open */ +test_ncvardef(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; int cdfid; /* netcdf id */ @@ -166,7 +165,7 @@ test_ncvardef(path) /* in define mode, add variables of each type with various shapes */ for (iv = 0; iv < nv; iv++) { /* set shape to use subset of dimensions previously defined */ - va[iv].dims = (int *) emalloc(sizeof(int) * va[iv].ndims); + va[iv].dims = (int *) emalloc(sizeof(int) * (size_t)va[iv].ndims); for (id = 0; id < va[iv].ndims; id++) va[iv].dims[id] = di_id[id]; if ((va_id[iv] = ncvardef(cdfid, va[iv].name, va[iv].type, diff --git a/nctest/varget.c b/nctest/varget.c index 1f1ab41ad..6de9637c0 100644 --- a/nctest/varget.c +++ b/nctest/varget.c @@ -24,8 +24,7 @@ * try with bad netCDF handle, check error */ int -test_ncvarget(path) - const char *path; /* name of writable netcdf file to open */ +test_ncvarget(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncvarget"; @@ -62,7 +61,7 @@ test_ncvarget(path) hc.edg[id] = 1; } /* get space for vals */ - hc.vals = emalloc(nctypelen(test.vars[iv].type) + 8); + hc.vals = emalloc((size_t)nctypelen(test.vars[iv].type) + 8); id = test.vars[iv].ndims - 1; tmp = hc.cor[id]; diff --git a/nctest/vargetg.c b/nctest/vargetg.c index b02f33b93..5b5580abc 100644 --- a/nctest/vargetg.c +++ b/nctest/vargetg.c @@ -25,8 +25,7 @@ * try with bad netCDF handle, check error */ int -test_ncvargetg(path) - const char *path; /* name of writable netcdf file to open */ +test_ncvargetg(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncvargetg"; @@ -64,7 +63,7 @@ test_ncvargetg(path) } /* get space for vals */ - hc.vals = emalloc(nctypelen(test.vars[iv].type) + 8); + hc.vals = emalloc((size_t)nctypelen(test.vars[iv].type) + 8); # define TEST_FAILS(varid) \ (ncvarputg(cdfid, varid, hc.cor, hc.edg, \ diff --git a/nctest/varput.c b/nctest/varput.c index b3ea893a7..dfd121df7 100644 --- a/nctest/varput.c +++ b/nctest/varput.c @@ -25,8 +25,7 @@ * try with bad netCDF handle, check error */ int -test_ncvarput(path) - const char *path; /* name of writable netcdf file to open */ +test_ncvarput(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncvarput"; @@ -66,7 +65,7 @@ test_ncvarput(path) hc.edg[id] = 1; } /* fill in vals with value of appropriate type */ - hc.vals = emalloc(nctypelen(test.vars[iv].type)); + hc.vals = emalloc((size_t)nctypelen(test.vars[iv].type)); val_fill(test.vars[iv].type, 1, hc.vals); id = test.vars[iv].ndims - 1; diff --git a/nctest/varputg.c b/nctest/varputg.c index 491121303..27c63d65c 100644 --- a/nctest/varputg.c +++ b/nctest/varputg.c @@ -25,8 +25,7 @@ * try with bad netCDF handle, check error */ int -test_ncvarputg(path) - const char *path; /* name of writable netcdf file to open */ +test_ncvarputg(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncvarputg"; @@ -66,7 +65,7 @@ test_ncvarputg(path) } /* fill in vals with value of appropriate type */ - hc.vals = emalloc(nctypelen(test.vars[iv].type)); + hc.vals = emalloc((size_t)nctypelen(test.vars[iv].type)); val_fill(test.vars[iv].type, 1, hc.vals); # define TEST_FAILS(varid) \ diff --git a/nctest/vartests.c b/nctest/vartests.c index 8fe35983c..53c1a82ed 100644 --- a/nctest/vartests.c +++ b/nctest/vartests.c @@ -25,8 +25,7 @@ * try with bad handle, check error */ int -test_ncvarid(path) - const char *path; /* name of writable netcdf file to open */ +test_ncvarid(const char *path) /* name of writable netcdf file to open */ { static char pname[] = "test_ncvarid"; int cdfid; /* netcdf id */ @@ -48,7 +47,7 @@ test_ncvarid(path) ncclose(cdfid); return ++nerrs; } /* in define mode, add a variable */ - xx.dims = (int *) emalloc(sizeof(int) * xx.ndims); + xx.dims = (int *) emalloc(sizeof(int) * (size_t)xx.ndims); for (id = 0; id < xx.ndims; id++) xx.dims[id] = id; if ((varid = ncvardef(cdfid, @@ -104,8 +103,7 @@ test_ncvarid(path) * try with bad netCDF handle, check error */ int -test_ncvarinq(path) - const char *path; /* name of writable netcdf file to open */ +test_ncvarinq(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncvarinq"; @@ -249,8 +247,7 @@ struct cdfelm { /* coordinates and generic value */ * get values and compare with put values */ static int -test_varputget1(cdfid) - int cdfid; /* handle of netcdf open and in data mode */ +test_varputget1(int cdfid) /* handle of netcdf open and in data mode */ { int nerrs = 0; static char pname[] = "test_varputget1"; @@ -380,8 +377,7 @@ test_varputget1(cdfid) * try with bad netCDF handle, check error */ int -test_ncvarput1(path) - const char *path; /* name of writable netcdf file to open */ +test_ncvarput1(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncvarput1"; @@ -468,8 +464,7 @@ test_ncvarput1(path) * try with bad netCDF handle, check error */ int -test_ncvarget1(path) - const char *path; /* name of writable netcdf file to open */ +test_ncvarget1(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncvarget1"; @@ -556,8 +551,7 @@ test_ncvarget1(path) * try renaming to existing variable name, check error */ int -test_ncvarrename(path) - const char *path; /* name of writable netcdf file to open */ +test_ncvarrename(const char *path) /* name of writable netcdf file to open */ { int nerrs = 0; static char pname[] = "test_ncvarrename"; @@ -584,7 +578,7 @@ test_ncvarrename(path) ncclose(cdfid); return ++nerrs; } /* in define mode, add two variables */ - yy.dims = (int *) emalloc(sizeof(int) * yy.ndims); + yy.dims = (int *) emalloc(sizeof(int) * (size_t)yy.ndims); for (id = 0; id < yy.ndims; id++) yy.dims[id] = id; if ((yy_id = ncvardef(cdfid, @@ -593,7 +587,7 @@ test_ncvarrename(path) ncclose(cdfid); return ++nerrs; } add_var(&test, &yy); /* keep in-memory netcdf in sync */ - zz.dims = (int *) emalloc(sizeof(int) * zz.ndims); + zz.dims = (int *) emalloc(sizeof(int) * (size_t)zz.ndims); for (id = 0; id < zz.ndims; id++) zz.dims[id] = id; if (ncvardef(cdfid, zz.name, zz.type, zz.ndims, zz.dims) == -1) { diff --git a/nctest/vputget.c b/nctest/vputget.c index 8c2ff2ee9..9e2c8421f 100644 --- a/nctest/vputget.c +++ b/nctest/vputget.c @@ -34,8 +34,7 @@ */ int -test_varputget(cdfid) - int cdfid; /* handle of netcdf open and in data mode */ +test_varputget(int cdfid) /* handle of netcdf open and in data mode */ { int nerrs = 0; static char pname[] = "test_varputget"; @@ -77,8 +76,8 @@ test_varputget(cdfid) for (ie = 0; ie < ne; ie++) { /* for each of ne points */ /* allocate space for the cube of values */ - hc[ie].vals = emalloc(nel[ie]*nctypelen(test.vars[iv].type) + 8); - tmp.vals = emalloc(nel[ie]*nctypelen(test.vars[iv].type) + 8); + hc[ie].vals = emalloc((size_t)(nel[ie]*nctypelen(test.vars[iv].type) + 8)); + tmp.vals = emalloc((size_t)(nel[ie]*nctypelen(test.vars[iv].type) + 8)); /* fill allocated space with different values of right type */ val_fill(test.vars[iv].type, nel[ie], hc[ie].vals); diff --git a/nctest/vputgetg.c b/nctest/vputgetg.c index c4b87b537..e20ccab7b 100644 --- a/nctest/vputgetg.c +++ b/nctest/vputgetg.c @@ -5,6 +5,7 @@ *********************************************************************/ #include +#include #include #include /* for free() */ #include "netcdf.h" @@ -34,8 +35,7 @@ */ int -test_varputgetg(cdfid) - int cdfid; /* handle of netcdf open and in data mode */ +test_varputgetg(int cdfid) /* handle of netcdf open and in data mode */ { int nerrs = 0; static char pname[] = "test_varputgetg"; @@ -109,7 +109,7 @@ test_varputgetg(cdfid) } for (ie = 0; ie < ne; ie++) { /* for each test */ - int nelms = (int)nel[ie]*nctypelen(test.vars[iv].type) + 8; + size_t nelms = (size_t)(nel[ie]*nctypelen(test.vars[iv].type) + 8); /* allocate space for the cube of values */ hc[ie].vals = emalloc(nelms); tmp.vals = emalloc(nelms); diff --git a/nczarr_test/bm_chunks3.c b/nczarr_test/bm_chunks3.c index 9a34bda33..66d0b64a0 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/ncdumpchunks.c b/nczarr_test/ncdumpchunks.c old mode 100755 new mode 100644 diff --git a/nczarr_test/ref_groups.h5 b/nczarr_test/ref_groups.h5 old mode 100755 new mode 100644 diff --git a/nczarr_test/test_zchunks.c b/nczarr_test/test_zchunks.c index 7307910af..5e7cf6213 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 54963761c..1907d3429 100644 --- a/nczarr_test/test_zchunks2.c +++ b/nczarr_test/test_zchunks2.c @@ -26,7 +26,7 @@ calculate_waste(int ndims, size_t *dimlen, size_t *chunksize, float *waste) size_t chunk_size = 1; assert(waste && dimlen && chunksize && ndims); - if (!(num_chunks = calloc(ndims, sizeof(size_t)))) ERR; + if (!(num_chunks = calloc((size_t)ndims, sizeof(size_t)))) ERR; #ifdef PRINT_CHUNK_WASTE_REPORT printf("\n"); @@ -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/nczarr_test/ut_chunking.c b/nczarr_test/ut_chunking.c old mode 100755 new mode 100644 diff --git a/nczarr_test/ut_projtest.h b/nczarr_test/ut_projtest.h old mode 100755 new mode 100644 diff --git a/nczarr_test/ut_test.c b/nczarr_test/ut_test.c old mode 100755 new mode 100644 diff --git a/nczarr_test/ut_util.c b/nczarr_test/ut_util.c index e9bb88bb5..8d0921da4 100644 --- a/nczarr_test/ut_util.c +++ b/nczarr_test/ut_util.c @@ -4,6 +4,7 @@ */ #include "ut_includes.h" +#include #undef DEBUG @@ -81,8 +82,8 @@ parsedimdef(const char* s0, Dimdef** defp) s = s0; if((p = strchr(s,'=')) == NULL) abort(); if((count = (p - s)) == 0) return THROW(NC_EINVAL); - def->name = malloc(count+1); - memcpy(def->name,s,count); + def->name = malloc((size_t)count+1); + memcpy(def->name,s,(size_t)count); def->name[count] = '\0'; s = p+1; sscanf(s,"%u%n",&l,&nchars); @@ -115,7 +116,7 @@ parsevardef(const char* s0, NClist* dimdefs, Vardef** varp) if(p == NULL) return THROW(NC_EINVAL); len = (p - s); if(len == 0) return THROW(NC_EINVAL); - memcpy(name,s,len); + memcpy(name,s,(size_t)len); name[len] = '\0'; vd->typeid = ut_typeforname(name); vd->typesize = ut_typesize(vd->typeid); @@ -127,7 +128,7 @@ parsevardef(const char* s0, NClist* dimdefs, Vardef** varp) if(p == NULL) return THROW(NC_EINVAL); len = (p - s); if(len == 0) return THROW(NC_EINVAL); - memcpy(name,s,len); + memcpy(name,s,(size_t)len); name[len] = '\0'; vd->name = strdup(name); /* parse a vector of dimnames and chunksizes and convert */ @@ -178,7 +179,7 @@ parsestringvector(const char* s0, int stopchar, char*** namesp) /* First, compute number of elements */ for(s=s0,nelems=1;*s;s++) {if(*s == ',') nelems++; if(*s == stopchar) break;} if(nelems == 0) return THROW(NC_EINVAL); - names = calloc(nelems+1,sizeof(char*)); + names = calloc((size_t)nelems+1,sizeof(char*)); for(s=s0,i=0;itypesize = sizeof(int); if(var != NULL) { common->rank = var->rank; - memcpy(common->dimlens,var->dimsizes,sizeof(size64_t)*common->rank); - memcpy(common->chunklens,var->chunksizes,sizeof(size64_t)*common->rank); - memcpy(common->memshape,common->dimlens,sizeof(size64_t)*common->rank); /* fake it */ + memcpy(common->dimlens,var->dimsizes,sizeof(size64_t)*(size_t)common->rank); + memcpy(common->chunklens,var->chunksizes,sizeof(size64_t)*(size_t)common->rank); + memcpy(common->memshape,common->dimlens,sizeof(size64_t)*(size_t)common->rank); /* fake it */ } } diff --git a/netCDFConfig.cmake.in b/netCDFConfig.cmake.in index db7bb823e..7adef5cf9 100644 --- a/netCDFConfig.cmake.in +++ b/netCDFConfig.cmake.in @@ -14,6 +14,8 @@ set(netCDF_LIBRARIES netCDF::netcdf) # include target information include("${CMAKE_CURRENT_LIST_DIR}/netCDFTargets.cmake") +@IMPORT_MPI@ + # Compiling Options # set(netCDF_C_COMPILER "@CC_VERSION@") diff --git a/oc2/dapparse.c b/oc2/dapparse.c index 8d039ad55..9a4517f79 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 fc7c45d1f..c9883f980 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 cde4df3fd..a87dca972 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); } diff --git a/oc2/ocinternal.c b/oc2/ocinternal.c index 12daca6ec..ef82342a3 100644 --- a/oc2/ocinternal.c +++ b/oc2/ocinternal.c @@ -2,6 +2,7 @@ See the COPYRIGHT file for more information. */ #include "config.h" +#include #include #ifdef HAVE_STDINT_H @@ -320,7 +321,7 @@ createtempfile(OCstate* state, OCtree* tree) int stat = OC_NOERR; char* path = NULL; char* tmppath = NULL; - int len; + size_t len; NCglobalstate* globalstate = NC_getglobalstate(); len = @@ -552,7 +553,7 @@ ocset_curlproperties(OCstate* state) int stat = NC_NOERR; char* path = NULL; char* tmppath = NULL; - int len; + size_t len; errno = 0; /* Create the unique cookie file name */ len = diff --git a/oc2/ocutil.c b/oc2/ocutil.c index dce1f032a..5366feb1c 100644 --- a/oc2/ocutil.c +++ b/oc2/ocutil.c @@ -3,6 +3,7 @@ #include "config.h" #include +#include #ifdef HAVE_UNISTD_H #include #endif @@ -695,7 +696,7 @@ done: char** ocmerge(const char** list1, const char** list2) { - int l1, l2; + size_t l1, l2; char** merge; const char** p; for(l1=0,p=list1;*p;p++) {l1++;} diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 60384f6b8..4f101773d 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -62,6 +62,9 @@ MACRO(buildplugin TARGET TARGETLIB) set_target_properties(${TARGET} PROPERTIES LINK_FLAGS "/INCREMENTAL:NO /DEBUG /OPT:REF /OPT:ICF") # Set file name & location set_target_properties(${TARGET} PROPERTIES COMPILE_PDB_NAME ${TARGET} COMPILE_PDB_OUTPUT_DIR ${CMAKE_BINARY_DIR}) + IF(MPI_C_INCLUDE_PATH) + target_include_directories(${TARGET} PRIVATE ${MPI_C_INCLUDE_PATH}) + ENDIF(MPI_C_INCLUDE_PATH) ENDIF() IF(STATUS_PARALLEL) diff --git a/plugins/H5Zblosc.c b/plugins/H5Zblosc.c old mode 100755 new mode 100644 diff --git a/plugins/H5Zblosc.h b/plugins/H5Zblosc.h old mode 100755 new mode 100644 diff --git a/plugins/H5Zszip.h b/plugins/H5Zszip.h old mode 100755 new mode 100644 diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 5bfdad03c..fc7a98408 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -9,17 +9,7 @@ include $(top_srcdir)/lib_flags.am # for information regarding incrementing `-version-info`. plugin_version_info = -version-info 0:0:0 -AM_LDFLAGS += -module -shared -export-dynamic $(NOUNDEFINED) - -if ISMINGW - AM_LDFLAGS += -avoid-version -else !ISMINGW -if ISCYGWIN -AM_LDFLAGS += -avoid-version -else !ISCYGWIN -AM_LDFLAGS += $(plugin_version_info) -endif !ISCYGWIN -endif !ISMINGW +AM_LDFLAGS += -module -avoid-version -shared -export-dynamic $(NOUNDEFINED) # Create an alternate directory if not installing. ALTPLUGINDIR = ${abs_top_builddir}/plugins/plugindir diff --git a/s3gc.in b/s3gc.in index 612e371d7..a63345c6c 100755 --- a/s3gc.in +++ b/s3gc.in @@ -61,7 +61,9 @@ if ! aws s3api list-objects-v2 --bucket ${S3TESTBUCKET} --prefix "${S3TESTSUBTRE fi aws s3api list-objects-v2 --bucket ${S3TESTBUCKET} --prefix "${S3TESTSUBTREE}" | grep -F '"Key":' >s3gc.keys while read -r line; do - KEY=`echo "$line" | sed -e 's|[^"]*"Key":[^"]*"\([^"]*\)".*|\1|'` + KEY0=`echo "$line" | sed -e 's|[^"]*"Key":[^"]*"\([^"]*\)".*|\1|'` + # Strip off any leading '/' + KEY=`echo "$KEY0" | sed -e 's|^[/]*\(.*\)|\1|'` # Ignore keys that do not start with ${S3TESTSUBTREE} PREFIX=`echo "$KEY" | sed -e 's|\([^/]*\)/.*|\1|'` if test "x$PREFIX" = "x$S3TESTSUBTREE" ; then diff --git a/unit_test/tst_xcache.c b/unit_test/tst_xcache.c index d527fcd50..e3a382540 100644 --- a/unit_test/tst_xcache.c +++ b/unit_test/tst_xcache.c @@ -84,7 +84,7 @@ generatestrings(int n, unsigned seed) char* s = NULL; srandom(seed); - strings = (NCXSTR*)calloc(sizeof(NCXSTR),(n+1)); + strings = (NCXSTR*)calloc(sizeof(NCXSTR),((size_t)n+1)); if(strings == NULL) abort(); for(i=0;i