Skip to content

Commit a5c07dc

Browse files
authored
refactor: replace sprintf with snprintf for safer string handling in dump functions (ggml-org#16913)
1 parent ad51c0a commit a5c07dc

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

ggml/src/ggml-hexagon/htp/ops-utils.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,46 @@ static inline int32_t htp_is_one_chunk(void * addr, uint32_t n, uint32_t chunk_s
4343
}
4444

4545
static inline void htp_dump_int8_line(char * pref, const int8_t * x, int n) {
46-
char str[1024], *p = str;
47-
p += sprintf(p, "%s: ", pref);
48-
for (int i = 0; i < 16; i++) {
49-
p += sprintf(p, "%d, ", x[i]);
46+
char str[1024], *p = str, *p_end = str + sizeof(str);
47+
p += snprintf(p, p_end - p, "%s: ", pref);
48+
for (int i = 0; i < n && p < p_end; i++) {
49+
p += snprintf(p, p_end - p, "%d, ", x[i]);
5050
}
5151
FARF(HIGH, "%s\n", str);
5252
}
5353

5454
static inline void htp_dump_uint8_line(char * pref, const uint8_t * x, uint32_t n) {
55-
char str[1024], *p = str;
56-
p += sprintf(p, "%s: ", pref);
57-
for (int i = 0; i < n; i++) {
58-
p += sprintf(p, "%d, ", x[i]);
55+
char str[1024], *p = str, *p_end = str + sizeof(str);
56+
p += snprintf(p, p_end - p, "%s: ", pref);
57+
for (int i = 0; i < n && p < p_end; i++) {
58+
p += snprintf(p, p_end - p, "%d, ", x[i]);
5959
}
6060
FARF(HIGH, "%s\n", str);
6161
}
6262

6363
static inline void htp_dump_int32_line(char * pref, const int32_t * x, uint32_t n) {
64-
char str[1024], *p = str;
65-
p += sprintf(p, "%s: ", pref);
64+
char str[1024], *p = str, *p_end = str + sizeof(str);
65+
p += snprintf(p, p_end - p, "%s: ", pref);
6666
for (int i = 0; i < n; i++) {
67-
p += sprintf(p, "%d, ", (int) x[i]);
67+
p += snprintf(p, p_end - p, "%d, ", (int) x[i]);
6868
}
6969
FARF(HIGH, "%s\n", str);
7070
}
7171

7272
static inline void htp_dump_fp16_line(char * pref, const __fp16 * x, uint32_t n) {
73-
char str[1024], *p = str;
74-
p += sprintf(p, "%s: ", pref);
73+
char str[1024], *p = str, *p_end = str + sizeof(str);
74+
p += snprintf(p, p_end - p, "%s: ", pref);
7575
for (int i = 0; i < n; i++) {
76-
p += sprintf(p, "%.6f, ", (float) x[i]);
76+
p += snprintf(p, p_end - p, "%.6f, ", (float) x[i]);
7777
}
7878
FARF(HIGH, "%s\n", str);
7979
}
8080

8181
static inline void htp_dump_fp32_line(char * pref, const float * x, uint32_t n) {
82-
char str[1024], *p = str;
83-
p += sprintf(p, "%s: ", pref);
82+
char str[1024], *p = str, *p_end = str + sizeof(str);
83+
p += snprintf(p, p_end - p, "%s: ", pref);
8484
for (int i = 0; i < n; i++) {
85-
p += sprintf(p, "%.6f, ", x[i]);
85+
p += snprintf(p, p_end - p, "%.6f, ", x[i]);
8686
}
8787
FARF(HIGH, "%s\n", str);
8888
}

0 commit comments

Comments
 (0)