Skip to content

Commit

Permalink
src: check empty before accessing string
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zcbenz committed Feb 5, 2024
1 parent 9448c61 commit 22199ff
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,20 @@ void Dotenv::ParseContent(const std::string_view content) {
// Remove leading whitespaces
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()) {
// Remove trailing whitespaces
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");
}

// Remove surrounding quotes
value = trim_quotes(value);
if (!value.empty()) {
// Remove surrounding quotes
value = trim_quotes(value);
}

store_.insert_or_assign(std::string(key), value);
lines = match.suffix();
Expand Down

0 comments on commit 22199ff

Please sign in to comment.