Skip to content

Commit 2cf0918

Browse files
Merge pull request #433 from Distributive-Network/philippe/426-fix
snprintf warning fix
2 parents ce0394d + 52181e1 commit 2cf0918

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/PyBytesProxyHandler.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,26 @@ static bool array_valueOf(JSContext *cx, unsigned argc, JS::Value *vp) {
3939
for (size_t i = 0; i < byteLength; i++) {
4040
numberOfDigits += data[i] < 10 ? 1 : data[i] < 100 ? 2 : 3;
4141
}
42+
4243
const size_t STRING_LENGTH = byteLength + numberOfDigits;
4344
JS::Latin1Char *buffer = (JS::Latin1Char *)malloc(sizeof(JS::Latin1Char) * STRING_LENGTH);
4445

45-
size_t charIndex = 0;
46-
snprintf((char *)&buffer[charIndex], 4, "%d", data[0]);
47-
charIndex += data[0] < 10 ? 1 : data[0] < 100 ? 2 : 3;
46+
if (snprintf((char *)&buffer[0], 3 + 1, "%hu", data[0]) < 0) {
47+
return false;
48+
}
49+
size_t charIndex = data[0] < 10 ? 1 : data[0] < 100 ? 2 : 3;
4850

4951
for (size_t dataIndex = 1; dataIndex < byteLength; dataIndex++) {
5052
buffer[charIndex] = ',';
5153
charIndex++;
52-
snprintf((char *)&buffer[charIndex], 4, "%d", data[dataIndex]);
54+
if (snprintf((char *)&buffer[charIndex], 3 + 1, "%hu", data[dataIndex]) < 0) {
55+
return false;
56+
}
5357
charIndex += data[dataIndex] < 10 ? 1 : data[dataIndex] < 100 ? 2 : 3;
5458
}
5559

5660
JS::UniqueLatin1Chars str(buffer);
57-
args.rval().setString(JS_NewLatin1String(cx, std::move(str), STRING_LENGTH - 1)); // don't include null byte
61+
args.rval().setString(JS_NewLatin1String(cx, std::move(str), STRING_LENGTH - 1)); // don't include the null terminating byte
5862
return true;
5963
}
6064

0 commit comments

Comments
 (0)