Skip to content

Commit 7da4605

Browse files
committed
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.
1 parent 9448c61 commit 7da4605

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/node_dotenv.cc

+13-7
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,26 @@ void Dotenv::ParseContent(const std::string_view content) {
113113
std::string value = match[2].str();
114114

115115
// Remove leading whitespaces
116-
value.erase(0, value.find_first_not_of(" \t"));
116+
auto iter = value.find_first_not_of(" \t");
117+
if (iter != std::string::npos) {
118+
value.erase(0, iter);
119+
}
117120

118121
// Remove trailing whitespaces
119-
value.erase(value.find_last_not_of(" \t") + 1);
120-
121-
const char maybeQuote = value.front();
122+
iter = value.find_first_not_of(" \t");
123+
if (iter != std::string::npos) {
124+
value.erase(iter + 1);
125+
}
122126

123-
if (maybeQuote == '"') {
127+
if (!value.empty() && value.front() == '"') {
124128
value = std::regex_replace(value, std::regex("\\\\n"), "\n");
125129
value = std::regex_replace(value, std::regex("\\\\r"), "\r");
126130
}
127131

128-
// Remove surrounding quotes
129-
value = trim_quotes(value);
132+
if (!value.empty()) {
133+
// Remove surrounding quotes
134+
value = trim_quotes(value);
135+
}
130136

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

0 commit comments

Comments
 (0)