Skip to content

Commit acd06d6

Browse files
junjieqifacebook-github-bot
authored andcommitted
Switch sprintf to snprintf (facebookresearch#3363)
Summary: Pull Request resolved: facebookresearch#3363 '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} Reviewed By: kuarora Differential Revision: D56009251 fbshipit-source-id: ec222cf589ff98b016979058d59fc20cccec8f43
1 parent 40e8643 commit acd06d6

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
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

+8-1
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,16 @@ 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 ,
180+
174181
ptr[-1] = 0;
175182
return std::string(res);
176183
}

0 commit comments

Comments
 (0)