refactor: replace sprintf with snprintf for safer string handling in dump functions (#16913)

This commit is contained in:
nullname
2025-11-05 04:25:39 +08:00
committed by GitHub
parent ad51c0a720
commit a5c07dcd7b

View File

@@ -43,46 +43,46 @@ static inline int32_t htp_is_one_chunk(void * addr, uint32_t n, uint32_t chunk_s
} }
static inline void htp_dump_int8_line(char * pref, const int8_t * x, int n) { static inline void htp_dump_int8_line(char * pref, const int8_t * x, int n) {
char str[1024], *p = str; char str[1024], *p = str, *p_end = str + sizeof(str);
p += sprintf(p, "%s: ", pref); p += snprintf(p, p_end - p, "%s: ", pref);
for (int i = 0; i < 16; i++) { for (int i = 0; i < n && p < p_end; i++) {
p += sprintf(p, "%d, ", x[i]); p += snprintf(p, p_end - p, "%d, ", x[i]);
} }
FARF(HIGH, "%s\n", str); FARF(HIGH, "%s\n", str);
} }
static inline void htp_dump_uint8_line(char * pref, const uint8_t * x, uint32_t n) { static inline void htp_dump_uint8_line(char * pref, const uint8_t * x, uint32_t n) {
char str[1024], *p = str; char str[1024], *p = str, *p_end = str + sizeof(str);
p += sprintf(p, "%s: ", pref); p += snprintf(p, p_end - p, "%s: ", pref);
for (int i = 0; i < n; i++) { for (int i = 0; i < n && p < p_end; i++) {
p += sprintf(p, "%d, ", x[i]); p += snprintf(p, p_end - p, "%d, ", x[i]);
} }
FARF(HIGH, "%s\n", str); FARF(HIGH, "%s\n", str);
} }
static inline void htp_dump_int32_line(char * pref, const int32_t * x, uint32_t n) { static inline void htp_dump_int32_line(char * pref, const int32_t * x, uint32_t n) {
char str[1024], *p = str; char str[1024], *p = str, *p_end = str + sizeof(str);
p += sprintf(p, "%s: ", pref); p += snprintf(p, p_end - p, "%s: ", pref);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
p += sprintf(p, "%d, ", (int) x[i]); p += snprintf(p, p_end - p, "%d, ", (int) x[i]);
} }
FARF(HIGH, "%s\n", str); FARF(HIGH, "%s\n", str);
} }
static inline void htp_dump_fp16_line(char * pref, const __fp16 * x, uint32_t n) { static inline void htp_dump_fp16_line(char * pref, const __fp16 * x, uint32_t n) {
char str[1024], *p = str; char str[1024], *p = str, *p_end = str + sizeof(str);
p += sprintf(p, "%s: ", pref); p += snprintf(p, p_end - p, "%s: ", pref);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
p += sprintf(p, "%.6f, ", (float) x[i]); p += snprintf(p, p_end - p, "%.6f, ", (float) x[i]);
} }
FARF(HIGH, "%s\n", str); FARF(HIGH, "%s\n", str);
} }
static inline void htp_dump_fp32_line(char * pref, const float * x, uint32_t n) { static inline void htp_dump_fp32_line(char * pref, const float * x, uint32_t n) {
char str[1024], *p = str; char str[1024], *p = str, *p_end = str + sizeof(str);
p += sprintf(p, "%s: ", pref); p += snprintf(p, p_end - p, "%s: ", pref);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
p += sprintf(p, "%.6f, ", x[i]); p += snprintf(p, p_end - p, "%.6f, ", x[i]);
} }
FARF(HIGH, "%s\n", str); FARF(HIGH, "%s\n", str);
} }