Skip to content

Commit 853fe3e

Browse files
junjieqifacebook-github-bot
authored andcommitted
Switch sprintf to snprintf (#3363)
Summary: 'sprintf' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead {F1484071654} Differential Revision: D56009251
1 parent 40e8643 commit 853fe3e

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

faiss/impl/io.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ std::string fourcc_inv_printable(uint32_t x) {
267267
str += c;
268268
} else {
269269
char buf[10];
270-
sprintf(buf, "\\x%02x", c);
270+
snprintf(buf, sizeof(buf), "\\x%02x", c);
271271
str += buf;
272272
}
273273
}

faiss/utils/simdlib_neon.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,18 @@ static inline std::string elements_to_string(const char* fmt, const S& simd) {
168168
simd.store(bytes);
169169
char res[1000], *ptr = res;
170170
for (size_t i = 0; i < N; ++i) {
171-
ptr += sprintf(ptr, fmt, bytes[i]);
171+
int bytesWritten =
172+
snprintf(ptr, sizeof(res) - (ptr - res), fmt, bytes[i]);
173+
if (bytesWritten >= 0) {
174+
ptr += bytesWritten;
175+
} else {
176+
break;
177+
}
172178
}
173179
// strip last ,
174-
ptr[-1] = 0;
180+
if (ptr != res) {
181+
ptr[-1] = 0;
182+
}
175183
return std::string(res);
176184
}
177185

0 commit comments

Comments
 (0)