Skip to content

Commit 6a4bde1

Browse files
jhunkelerpllimmdlpstsci
authored
(ACS) Implement variadic trailer functions (#644)
* Variadic trl messaging functions * Increase message buffer length to 2Kb * Use snprintf * Fix allocation of obsnames array * Add extra byte for nul terminator * Fix ODR violation * status is already global * Report the (potentially bad) format string * Fix overflow in ioerr * Formatting * Replace magic number usage * Remove VLA * Temporarily escape scale noise format * The new trlmessage interprets '%%' as an invalid formatter * Fix ODR violation * MsgText is already global * status is already global * ACS: Use variadic trailer functions * Replaces sprintf+trl(message|warn|error) with single trl calls * Redundant message handlers have been purged * Fix snprintf size mismatch * Remove errant line feeds * Fix error message * Say calloc not malloc * Fix bad printf call * Use HSTCAL-specific error codes on exit * Update lib/hstcalversion.c MsgText is written to the header/history * Remove errant semi-colons * Fix MSG_BUFF_LENGTH definition * Fix missing warning * Fix incorrect message type * Remove unused function prototypes * acs.h: asnerror * acs.h: asnwarn Co-authored-by: Michele De La Pena <[email protected]> --------- Co-authored-by: P. L. Lim <[email protected]> Co-authored-by: Michele De La Pena <[email protected]>
1 parent 60ff2aa commit 6a4bde1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+592
-1002
lines changed

ctegen2/ctehelpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ No. Name Ver Type Cards Dimensions Format
252252
extern int status; /* variable for return status */
253253

254254
/* VARIABLE FOR FILENAME + EXTENSION NUMBER. */
255-
char filename_wext[strlen(filename) + 4];
255+
char filename_wext[CHAR_FNAME_LENGTH] = {0};
256256

257257
/* NAMES OF DATA COLUMNS WE WANT FROM THE FILE, DATA WILL BE STORED IN THE PARS STRUCTURE */
258258
/* QPROF table - the last column is a description string */

hstio/hstio.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,12 @@ static void ioerr(HSTIOError e, IODescPtr x_, int status) {
427427
IODesc *x;
428428
char cfitsio_errmsg[81];
429429
x = (IODesc *)x_;
430-
sprintf(&error_msg[strlen(error_msg)],
430+
snprintf(&error_msg[strlen(error_msg)],
431+
sizeof(error_msg) - strlen(error_msg),
431432
"Filename %s EXTNAME %s EXTVER %d CFITSIO status %d\n",
432433
x->filename, x->extname, x->extver, status);
433434
while (fits_read_errmsg(cfitsio_errmsg)) {
434-
strncat(error_msg, cfitsio_errmsg, 80);
435+
strncat(error_msg, cfitsio_errmsg, sizeof(error_msg) - strlen(error_msg) - 1);
435436
}
436437
error(e,0);
437438
}
@@ -2050,8 +2051,7 @@ static char *make_iodesc(IODesc **x, char *fname, char *ename, int ever) {
20502051
iodesc->type = 0;
20512052
if (fname == 0) fname = "";
20522053
if (ename == 0) ename = "";
2053-
iodesc->filename = (char *)calloc(((flen = strlen(fname)) + 1),
2054-
sizeof(char));
2054+
iodesc->filename = (char *)calloc(((flen = strlen(fname)) + 1), sizeof(char));
20552055
if (iodesc->filename == NULL) {
20562056
free(iodesc);
20572057
error(NOMEM,"Allocating I/O descriptor");
@@ -2077,13 +2077,14 @@ static char *make_iodesc(IODesc **x, char *fname, char *ename, int ever) {
20772077

20782078
/* make up the proper filename */
20792079
/* check for a request for the primary HDU */
2080-
tmp = (char *)calloc((flen + 80),sizeof(char));
2080+
const size_t flen_new = flen + 80;
2081+
tmp = (char *)calloc(flen_new,sizeof(char));
20812082
if (tmp == NULL) { error(NOMEM,"Allocating I/O descriptor"); return NULL; }
20822083
strcpy(tmp,fname);
20832084
if (ever == 0 || ename == 0 || ename[0] == '\0' || ename[0] == ' ')
20842085
strcat(tmp,"[0]");
20852086
else
2086-
sprintf(&tmp[flen],"[%s,%d]",xname,ever);
2087+
snprintf(&tmp[flen], flen_new, "[%s,%d]",xname,ever);
20872088

20882089
*x = iodesc;
20892090
return tmp;

hstio/keyword.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,9 +1407,9 @@ char *comment i: comment to add, if keyword doesn't exist
14071407
char strBuffer[MSG_BUFF_LENGTH];
14081408
*strBuffer = '\0';
14091409
if (value == True)
1410-
sprintf(strBuffer, "%s T %s", keyword, comment);
1410+
snprintf(strBuffer, sizeof(strBuffer), "%s T %s", keyword, comment);
14111411
else if(value == False)
1412-
sprintf(strBuffer, "%s F %s", keyword, comment);
1412+
snprintf(strBuffer, sizeof(strBuffer), "%s F %s", keyword, comment);
14131413
else
14141414
assert(0); // Unimplemented
14151415
tempStatus = addHistoryKw(hd, strBuffer);
@@ -1487,7 +1487,7 @@ char *comment i: comment to add, if keyword doesn't exist
14871487
// add as history if keyword missing
14881488
char strBuffer[MSG_BUFF_LENGTH];
14891489
*strBuffer = '\0';
1490-
sprintf(strBuffer, "%s %s %s", keyword, value, comment);
1490+
snprintf(strBuffer, sizeof(strBuffer), "%s %s %s", keyword, value, comment);
14911491
tempStatus = addHistoryKw(hd, strBuffer);
14921492
}
14931493
return tempStatus;

include/hstcal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
//
99
// Note: IRAF_SZ_LINE = 1023 relates to the maximum filename size accepted by cfitsio.
1010
#define CHAR_FNAME_LENGTH 255
11-
#define CHAR_LINE_LENGTH 255
12-
#define MSG_BUFF_LENGTH CHAR_LINE_LENGTH + 1
11+
#define CHAR_LINE_LENGTH 1023
12+
#define MSG_BUFF_LENGTH ((CHAR_LINE_LENGTH * 2) + 1)
1313

1414
/* Standard string buffer for use in messages */
1515
extern char MsgText[MSG_BUFF_LENGTH];

include/trlbuf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ void SetTrlQuietMode (int quiet);
2828
void InitTrlPreface (void);
2929
void ResetTrlPreface (void);
3030
void CloseTrlBuf (struct TrlBuf * ptr);
31-
void trlmessage (const char *message);
32-
void trlwarn (const char *message);
33-
void trlerror (const char *message);
31+
void trlmessage (const char *fmt, ...);
32+
void trlwarn (const char *fmt, ...);
33+
void trlerror (const char *fmt, ...);
3434
void trlopenerr (const char *filename);
3535
void trlreaderr (const char *filename);
3636
void trlkwerr (const char *keyword, const char *filename);

lib/getphttab.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,14 @@ static int OpenPhotTab (char *tabname, char *photvar, PhtCols *tabinfo) {
312312
/* Copy in the column names now */
313313
strcpy(colnames[0],photvar);
314314
for (parnum=1;parnum <= tabinfo->parnum; parnum++){
315-
sprintf(colnames[parnum],"%s%i",photvar,parnum);
316-
sprintf(ecolnames[parnum],"NELEM%i",parnum);
317-
sprintf(pncolnames[parnum],"PAR%iNAMES",parnum);
318-
sprintf(pvcolnames[parnum],"PAR%iVALUES",parnum);
315+
snprintf(colnames[parnum], SZ_COLNAME, "%s%i", photvar, parnum);
316+
snprintf(ecolnames[parnum], SZ_COLNAME, "NELEM%i", parnum);
317+
snprintf(pncolnames[parnum], SZ_COLNAME, "PAR%iNAMES", parnum);
318+
snprintf(pvcolnames[parnum], SZ_COLNAME, "PAR%iVALUES", parnum);
319319
}
320320

321321
/* Create name of table with extension to be opened */
322-
sprintf(tname,"%s[%s]",tabname,photvar);
322+
snprintf(tname, sizeof(tname), "%s[%s]", tabname, photvar);
323323

324324
/* keep track of what extension we are processing here */
325325
strcpy(tabinfo->photvar, photvar);
@@ -497,9 +497,9 @@ static int InterpretPhotmode(char *photmode, PhotPar *obs){
497497
/*
498498
Initialize intermediate variable
499499
*/
500-
obsnames = (char **)calloc(obselems,sizeof(char *));
500+
obsnames = calloc(obselems + 1,sizeof(*obsnames));
501501
for (i=0;i<obselems;i++)
502-
obsnames[i] = (char *) calloc(SZ_COLNAME,sizeof(char));
502+
obsnames[i] = (char *) calloc(SZ_COLNAME + 1,sizeof(char));
503503
/* Start adding elements to the intermediate variable */
504504
strcpy(tempmode,photmode);
505505
chtok=strtok(tempmode,",");
@@ -695,8 +695,8 @@ static int ReadPhotArray (PhtCols *tabinfo, int row, PhtRow *tabrow) {
695695
for (n=1,col=0; n<=tabrow->parnum; n++,col++){
696696
/* Build up names of columns for parameterized values
697697
which need to be read in */
698-
sprintf(col_nelem,"NELEM%d",n); /* col name: NELEMn */
699-
sprintf(col_parval,"PAR%dVALUES",n); /* col name: PARnVALUES */
698+
snprintf(col_nelem, sizeof(col_nelem), "NELEM%d",n); /* col name: NELEMn */
699+
snprintf(col_parval, sizeof(col_parval), "PAR%dVALUES",n); /* col name: PARnVALUES */
700700

701701
/* Read in number of elements for this parameterized variable */
702702
c_tbegti (tabinfo->tp, tabinfo->cp_nelem[n], row, &tabrow->nelem[n]);

lib/hstcalversion.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ char * sprintfGitInfo(char ** buffer)
1414

1515
const char * format = "git tag: %s\ngit branch: %s\nHEAD @: %s";
1616
size_t length = strlen(format) + strlen(VERSION) + strlen(BRANCH) + strlen(COMMIT); // NOTE: this doesn't require an explicit +1 for '\0' as it is larger than needed due to '%s' & '\n'.
17-
*buffer = malloc(length*sizeof(char));
17+
*buffer = malloc((length + 1) * sizeof(char));
1818
if (!*buffer)
1919
return NULL;
20-
sprintf(*buffer, format, VERSION, BRANCH, COMMIT);
20+
snprintf(*buffer, length, format, VERSION, BRANCH, COMMIT);
2121
return *buffer;
2222
}
2323

0 commit comments

Comments
 (0)