Skip to content

Commit 50a21b7

Browse files
committed
Refactor: tools: Strip rest of trailing whitespace in load_env_vars()
By stripping the rest of the trailing whitespace after stripping the comment, we eliminate the need to null-terminate explictly. If the value is valid (not followed by any garbage), then end already points to a terminating null byte after stripping the comment and remaining whitespace. Signed-off-by: Reid Wahl <[email protected]>
1 parent 3a7615c commit 50a21b7

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

daemons/execd/remoted_pidone.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ load_env_vars(void)
109109
char *name = NULL;
110110
char *end = NULL;
111111
char *value = NULL;
112-
char *value_end = NULL;
113112
char *quote = NULL;
114113
char *comment = NULL;
115114

@@ -142,7 +141,7 @@ load_env_vars(void)
142141
// Matching closing quote wasn't found
143142
goto cleanup_loop;
144143
}
145-
// Null-terminate value, and advance beyond close quote
144+
// Discard closing quote and advance to check for trailing garbage
146145
*end++ = '\0';
147146

148147
} else {
@@ -153,36 +152,28 @@ load_env_vars(void)
153152
&& (*end != '\0')) {
154153
end++;
155154
}
156-
// Do NOT null-terminate value (yet)
157155
}
158156

159157
/* We have a valid name and value, and end is now the character after
160158
* the closing quote or the first whitespace after the unquoted value.
161159
* Make sure the rest of the line, if any, is just optional whitespace
162160
* followed by a comment.
163161
*/
164-
value_end = end;
165162

166163
// Strip trailing comment beginning with '#'
167164
comment = strchr(end, '#');
168165
if (comment != NULL) {
169166
*comment = '\0';
170167
}
171168

172-
while (isspace(*end)) {
173-
end++;
174-
}
169+
// Strip any remaining trailing whitespace from value
170+
g_strchomp(end);
175171

176172
if (*end != '\0') {
177173
// Found garbage after value
178174
goto cleanup_loop;
179175
}
180176

181-
if (quote == NULL) {
182-
// Now we can null-terminate an unquoted value
183-
*value_end = '\0';
184-
}
185-
186177
// Don't overwrite (bundle options take precedence)
187178
setenv(name, value, 0);
188179

0 commit comments

Comments
 (0)