Skip to content

Commit 2e9bcf2

Browse files
authored
Add asserts for snprintf, sprintf, and vsnprintf (#2910)
Signed-off-by: Mike Essenmacher <[email protected]>
1 parent 3824321 commit 2e9bcf2

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

src/Runtime/OMIndexLookup.inc

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ static inline uint32_t hash_string(uint32_t hval, const char *str) {
3434
// Adaptation of 32-bit FNV for int64_t values.
3535
static inline uint32_t hash_int64(uint32_t hval, int64_t val) {
3636
char str[20];
37-
snprintf(str, sizeof(str), "%lld", (long long)val);
37+
int num_chars_written = snprintf(str, sizeof(str), "%lld", (long long)val);
38+
assert(num_chars_written >= 0 && "snprintf write error to str");
3839
return hash_string(hval, str);
3940
}
4041

src/Runtime/OMInstrument.inc

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ static void ReportMemory() {
146146
char memOutput[200];
147147
FILE *memPipe;
148148
mypid = getpid();
149-
snprintf(memCommand, sizeof(memCommand), "ps -o vsz='' -p %d", mypid);
149+
int num_chars_written = snprintf(memCommand, sizeof(memCommand), "ps -o vsz='' -p %d", mypid);
150+
assert(num_chars_written >= 0 && "snprintf write error to memCommand");
150151
memPipe = popen(memCommand, "r");
151152
if (!memPipe) {
152153
fprintf(fout, ", error-failed-to-execute-ps\n");

src/Runtime/jni/jnilog.c

+16-6
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,25 @@ void log_printf(int level, const char *file, const char *func, int line,
160160
time_t now;
161161
struct tm *tm;
162162
char buf[LOG_MAX_LEN];
163+
int num_chars_written = 0;
163164

164165
/* Get local time and format as 2020-07-03 05:17:42 -0400 */
165166
if (time(&now) == -1 || (tm = localtime(&now)) == NULL ||
166-
strftime(buf, sizeof(buf), "[%F %T %z]", tm) == 0)
167-
sprintf(buf, "[-]");
167+
strftime(buf, sizeof(buf), "[%F %T %z]", tm) == 0) {
168+
num_chars_written = sprintf(buf, "[-]");
169+
assert(num_chars_written >= 0 && "sprintf write error to buf");
170+
}
168171

169172
/* Output thread ID, log level, file name, function number, and line number.
170173
* Note that pthread_t on most platforms is unsigned long but is a struct
171174
* of 8 bytes on z/OS.
172175
*/
173176
pthread_t tid = get_threadid();
174177
assert(LOG_MAX_LEN >= strlen(buf) && "error in snprintf length");
175-
snprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf),
178+
num_chars_written = snprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf),
176179
"[%016lx][%s]%s:%s:%d ", *(unsigned long *)&tid, log_level_name[level],
177180
get_filename(file), func, line);
181+
assert(num_chars_written >= 0 && "snprintf write error to buf");
178182

179183
/* Output actual log data */
180184
/* Definition of vsnprintf:
@@ -203,11 +207,15 @@ void log_printf(int level, const char *file, const char *func, int line,
203207

204208
va_list log_data;
205209
va_start(log_data, fmt);
206-
vsnprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf), fmt, log_data);
210+
num_chars_written =
211+
vsnprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf), fmt, log_data);
212+
assert(num_chars_written >= 0 && "vsnprintf write error to buf");
207213
va_end(log_data);
208214

209215
/* Add new line */
210-
snprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf), "\n");
216+
num_chars_written =
217+
snprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf), "\n");
218+
assert(num_chars_written >= 0 && "snprintf write error to buf");
211219

212220
/* Write out and flush the output buffer */
213221
FILE *fp = get_log_fp();
@@ -238,9 +246,11 @@ static FILE *get_log_file_by_name(char *name) {
238246
char *tname = (char *)malloc(strlen(name) + 32);
239247
if (tname) {
240248
pthread_t tid = get_threadid();
241-
snprintf(
249+
int num_chars_written = snprintf(
242250
tname, strlen(name) + 32, "%s.%016lx", name, *(unsigned long *)&tid);
251+
assert(num_chars_written >= 0 && "snprintf write error to tname");
243252
fp = fopen(tname, "w");
253+
assert(fp != NULL && "fopen error on tname");
244254
free(tname);
245255
}
246256
}

src/Runtime/jni/jnilog.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL };
4141
*/ \
4242
while (__i < __l && \
4343
(__k = snprintf(__p, __j, format, ((type *)data)[__i])) < __j) { \
44+
assert(__k >= 0 && "snprintf write error to __p"); \
4445
__p += __k; \
4546
__j -= __k; \
4647
__i++; \
@@ -55,8 +56,9 @@ enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL };
5556
* add "... " at the end to denote that the last element is \
5657
* truncated. \
5758
*/ \
58-
snprintf(buf + strlen(buf), 6, \
59+
int __m = snprintf(buf + strlen(buf), 6, \
5960
(__i == __l) ? " " : (__j == 1) ? " ... " : "... "); \
61+
assert(__m >= 0 && "snprintf write error to buf"); \
6062
} while (0)
6163

6264
/* Construct string of up to LOG_MAX_NUM elements of an array of ONNX type.
@@ -90,8 +92,10 @@ enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL };
9092
case ONNX_TYPE_DOUBLE: \
9193
LOG_BUF_C_TYPE(const double, hex ? " %016x" : " %lf", buf, data, n); \
9294
break; \
93-
default: \
94-
sprintf(buf, " unsupported data type %d ", type); \
95+
default: { \
96+
int __a = sprintf(buf, " unsupported data type %d ", type); \
97+
assert(__a >= 0 && "sprintf write error to buf"); \
98+
} \
9599
} \
96100
} while (0)
97101

src/Runtime/jni/jniwrapper.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@ Java_com_ibm_onnxmlir_OMModel_query_1entry_1points(JNIEnv *env, jclass cls) {
759759
*/
760760
for (int64_t i = 0; i < neps; i++) {
761761
char ep[32];
762-
sprintf(ep, "ep[%lld]", (long long)i);
762+
int num_chars_written = sprintf(ep, "ep[%lld]", (long long)i);
763+
assert(num_chars_written >= 0 && "sprintf write error to ep");
763764
HEX_DEBUG(ep, jni_eps[i], strlen(jni_eps[i]));
764765
LOG_PRINTF(LOG_DEBUG, "ep[%d](%ld):%s", i, strlen(jni_eps[i]), jni_eps[i]);
765766

0 commit comments

Comments
 (0)