From 3875fa1dc5121e0be7ebe46c3f68a7d27fa7d4d9 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 16 Feb 2024 23:33:37 +0900 Subject: [PATCH] src: check empty before accessing string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix an assertion when running dotnev tests with GN build: assertion !empty() failed: string::front(): string is empty which was caused by calling value.front() without verifying the value is not empty. PR-URL: https://github.com/nodejs/node/pull/51665 Reviewed-By: Yagiz Nizipli Reviewed-By: Marco Ippolito Reviewed-By: Luigi Pinca Reviewed-By: Gerhard Stöbich --- src/node_dotenv.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index 53faf461bb42f2..8d92e917082488 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -116,11 +116,11 @@ void Dotenv::ParseContent(const std::string_view content) { value.erase(0, value.find_first_not_of(" \t")); // Remove trailing whitespaces - value.erase(value.find_last_not_of(" \t") + 1); - - const char maybeQuote = value.front(); + if (!value.empty()) { + value.erase(value.find_last_not_of(" \t") + 1); + } - if (maybeQuote == '"') { + if (!value.empty() && value.front() == '"') { value = std::regex_replace(value, std::regex("\\\\n"), "\n"); value = std::regex_replace(value, std::regex("\\\\r"), "\r"); }