From 90a4de02b65f8842f60fa7dbc6265de6f65641ca Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 18 Feb 2025 22:31:55 +0100 Subject: [PATCH] src: do not format single string argument for THROW_ERR_* If the macros are used as ERR_*(isolate, message) or THROW_ERR_*(isolate, message) with a single string argument, do run formatter on the message, and allow the caller to pass in a message directly with characters that would otherwise need escaping if used as format string unconditionally. PR-URL: https://github.com/nodejs/node/pull/57126 Refs: https://github.com/fisker/prettier-issue-17139 Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel Reviewed-By: Chengzhong Wu --- src/node_errors.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/node_errors.h b/src/node_errors.h index 801b19fc918..4d2667fb4df 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -115,11 +115,21 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details); V(ERR_WORKER_INIT_FAILED, Error) \ V(ERR_PROTO_ACCESS, Error) +// If the macros are used as ERR_*(isolate, message) or +// THROW_ERR_*(isolate, message) with a single string argument, do run +// formatter on the message, and allow the caller to pass in a message +// directly with characters that would otherwise need escaping if used +// as format string unconditionally. #define V(code, type) \ template \ inline v8::Local code( \ v8::Isolate* isolate, const char* format, Args&&... args) { \ - std::string message = SPrintF(format, std::forward(args)...); \ + std::string message; \ + if (sizeof...(Args) == 0) { \ + message = format; \ + } else { \ + message = SPrintF(format, std::forward(args)...); \ + } \ v8::Local js_code = FIXED_ONE_BYTE_STRING(isolate, #code); \ v8::Local js_msg = \ v8::String::NewFromUtf8(isolate, \