Skip to content

Commit dfd4962

Browse files
tniessentargos
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 <daniel@lemire.me> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
1 parent 152c5ef commit dfd4962

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
@@ -346,17 +346,19 @@ inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
346346
std::string_view str);
347347

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

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

0 commit comments

Comments
 (0)