From 2fae73a802b2e6b54c8b066ca16e003c804139eb Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Sun, 30 Apr 2023 21:04:05 -0400 Subject: [PATCH] Removed a use of sprintf that required changing a function signature --- include/nctime.h | 4 ++-- libdispatch/nctime.c | 26 +++++++++++++------------- ncdump/dumplib.c | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/include/nctime.h b/include/nctime.h index bbab97228e..04419efe88 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/libdispatch/nctime.c b/libdispatch/nctime.c index e265ba1f49..cdde2db00d 100644 --- a/libdispatch/nctime.c +++ b/libdispatch/nctime.c @@ -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/ncdump/dumplib.c b/ncdump/dumplib.c index 03fe3b278e..e6ca79a9c9 100644 --- a/ncdump/dumplib.c +++ b/ncdump/dumplib.c @@ -1270,7 +1270,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++] = '"';