From 0c3242862a2265562e2e171bbc3f7a531f517c20 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 6 Sep 2018 10:49:55 +0200 Subject: [PATCH] src: make `FIXED_ONE_BYTE_STRING` an inline fn This prevents accidental usage on non-fixed strings. PR-URL: https://github.com/nodejs/node/pull/22725 Reviewed-By: Daniel Bevenius Reviewed-By: Minwoo Jung Reviewed-By: Colin Ihrig Reviewed-By: Gus Caplan Reviewed-By: James M Snell --- src/node_api.cc | 4 ++-- src/util.h | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 22be3647ea86dd..ef4ec29708c0d4 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -2173,11 +2173,11 @@ static napi_status set_error_code(napi_env env, } } name_string = v8::String::Concat( - isolate, name_string, FIXED_ONE_BYTE_STRING(isolate, " [")); + isolate, name_string, node::FIXED_ONE_BYTE_STRING(isolate, " [")); name_string = v8::String::Concat(isolate, name_string, code_value.As()); name_string = v8::String::Concat( - isolate, name_string, FIXED_ONE_BYTE_STRING(isolate, "]")); + isolate, name_string, node::FIXED_ONE_BYTE_STRING(isolate, "]")); set_maybe = err_object->Set(context, name_key, name_string); RETURN_STATUS_IF_FALSE(env, diff --git a/src/util.h b/src/util.h index 057346a2d04fca..4036383dee92de 100644 --- a/src/util.h +++ b/src/util.h @@ -89,9 +89,6 @@ NO_RETURN void Abort(); NO_RETURN void Assert(const char* const (*args)[4]); void DumpBacktrace(FILE* fp); -#define FIXED_ONE_BYTE_STRING(isolate, string) \ - (node::OneByteString((isolate), (string), sizeof(string) - 1)) - #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ void operator=(const TypeName&) = delete; \ void operator=(TypeName&&) = delete; \ @@ -248,6 +245,15 @@ inline v8::Local OneByteString(v8::Isolate* isolate, const unsigned char* data, int length = -1); +// Used to be a macro, hence the uppercase name. +template +inline v8::Local FIXED_ONE_BYTE_STRING( + v8::Isolate* isolate, + const char(&data)[N]) { + return OneByteString(isolate, data, N - 1); +} + + // Swaps bytes in place. nbytes is the number of bytes to swap and must be a // multiple of the word size (checked by function). inline void SwapBytes16(char* data, size_t nbytes);