Merge pull request #2743 from seanm/snprintf2

Removed a use of sprintf that required changing a function signature
This commit is contained in:
Ward Fisher 2023-12-11 15:23:29 -07:00 committed by GitHub
commit b99a263de6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 16 deletions

View File

@ -150,14 +150,14 @@ typedef struct timeinfo_t {
# define MSC_NCTIME_EXTRA __declspec(dllimport) # define MSC_NCTIME_EXTRA __declspec(dllimport)
# endif # 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 cdChar2Comp(cdCalenType timetype, char* chartime, cdCompTime* comptime);
MSC_NCTIME_EXTRA extern void Cdh2e(CdTime *htime, double *etime); 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 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 cdParseRelunits(cdCalenType timetype, char* relunits, cdUnitTime* unit, cdCompTime* base_comptime);
MSC_NCTIME_EXTRA extern int cdSetErrOpts(int opts); MSC_NCTIME_EXTRA extern int cdSetErrOpts(int opts);
#else #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 cdChar2Comp(cdCalenType timetype, char* chartime, cdCompTime* comptime);
extern void Cdh2e(CdTime *htime, double *etime); extern void Cdh2e(CdTime *htime, double *etime);
extern void Cde2h(double etime, CdTimeType timeType, long baseYear, CdTime *htime); extern void Cde2h(double etime, CdTimeType timeType, long baseYear, CdTime *htime);

View File

@ -1085,7 +1085,7 @@ cdRel2Comp(cdCalenType timetype, char* relunits, double reltime, cdCompTime* com
/* rkr: output as ISO 8601 strings */ /* rkr: output as ISO 8601 strings */
static void 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; double dtmp, sec;
int ihr, imin, isec; int ihr, imin, isec;
@ -1121,23 +1121,23 @@ cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time)
if(timetype & cdStandardCal){ if(timetype & cdStandardCal){
switch (nskip) { switch (nskip) {
case 0: /* sec != 0 && (int)sec != sec */ 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); comptime.year,comptime.month,comptime.day,separator,ihr,imin,sec);
break; break;
case 1: 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); comptime.year,comptime.month,comptime.day,separator,ihr,imin,isec);
break; break;
case 2: 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); comptime.year,comptime.month,comptime.day,separator,ihr,imin);
break; break;
case 3: 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); comptime.year,comptime.month,comptime.day,separator,ihr);
break; break;
case 4: 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); comptime.year,comptime.month,comptime.day);
break; break;
} }
@ -1145,23 +1145,23 @@ cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time)
else { /* Climatological */ else { /* Climatological */
switch (nskip) { switch (nskip) {
case 0: /* sec != 0 && (int)sec != sec */ 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); comptime.month,comptime.day,separator,ihr,imin,sec);
break; break;
case 1: 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); comptime.month,comptime.day,separator,ihr,imin,isec);
break; break;
case 2: 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); comptime.month,comptime.day,separator,ihr,imin);
break; break;
case 3: 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); comptime.month,comptime.day,separator,ihr);
break; break;
case 4: case 4:
sprintf(time,"%2.2hd-%2.2hd", snprintf(time,time_size,"%2.2hd-%2.2hd",
comptime.month,comptime.day); comptime.month,comptime.day);
break; break;
} }
@ -1171,12 +1171,12 @@ cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time)
/* rkr: added for output closer to ISO 8601 */ /* rkr: added for output closer to ISO 8601 */
void 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; cdCompTime comptime;
cdRel2Comp(timetype, relunits, reltime, &comptime); cdRel2Comp(timetype, relunits, reltime, &comptime);
cdComp2Iso(timetype, separator, comptime, chartime); cdComp2Iso(timetype, separator, comptime, chartime, chartime_size);
return; return;
} }

View File

@ -1270,7 +1270,7 @@ nctime_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
oldopts = cdSetErrOpts(0); oldopts = cdSetErrOpts(0);
newopts = oldopts | CU_VERBOSE; newopts = oldopts | CU_VERBOSE;
cdSetErrOpts(newopts); 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); cdSetErrOpts(oldopts);
res = strlen(sout); res = strlen(sout);
sout[res++] = '"'; sout[res++] = '"';