Skip to content

Commit 8c296ba

Browse files
tniessenrichardlau
authored andcommitted
src: enforce assumptions in FIXED_ONE_BYTE_STRING
These functions are both meant to be used with a null-terminated and thus non-empty sequence of `char`s. However, there is nothing stopping call sites from passing zero-length sequences, which would certainly not be null-terminated and also would cause an underflow in `N - 1`. Therefore, this commit - changes the size `N` of the array from `int` to `std::size_t`, - ensures that compilation will fail if `N = 0`, and - adds a runtime assertion that fails if the `N`-th `char` is not `\0`. Note that the runtime assertion should be eliminated by any optimizing compiler when given a string literal, which is how these functions are used for the most part (though not exclusively). PR-URL: #58155 Reviewed-By: Daniel Lemire <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]>
1 parent 5559456 commit 8c296ba

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/util.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,19 @@ inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
345345
std::string_view str);
346346

347347
// Used to be a macro, hence the uppercase name.
348-
template <int N>
349-
inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(
350-
v8::Isolate* isolate,
351-
const char(&data)[N]) {
348+
template <std::size_t N>
349+
requires(N > 0)
350+
inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(v8::Isolate* isolate,
351+
const char (&data)[N]) {
352+
CHECK_EQ(data[N - 1], '\0');
352353
return OneByteString(isolate, data, N - 1);
353354
}
354355

355356
template <std::size_t N>
357+
requires(N > 0)
356358
inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(
357-
v8::Isolate* isolate,
358-
const std::array<char, N>& arr) {
359+
v8::Isolate* isolate, const std::array<char, N>& arr) {
360+
CHECK_EQ(arr[N - 1], '\0');
359361
return OneByteString(isolate, arr.data(), N - 1);
360362
}
361363

0 commit comments

Comments
 (0)