src: check empty before accessing string

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 <yagiz.nizipli@sentry.io>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
This commit is contained in:
Cheng Zhao
2024-02-16 23:33:37 +09:00
committed by GitHub
parent ec3c7bc2c4
commit c682fbfbe2

View File

@@ -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);
if (!value.empty()) {
value.erase(value.find_last_not_of(" \t") + 1);
}
const char maybeQuote = value.front();
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");
}