Fix config parser to handle inline comments

The config parser now properly handles inline comments in configuration
  directives. Previously, 'bind * -::* # comment' would fail because the
  parser treated # and subsequent text as additional arguments.

  This fix strips inline comments before parsing, while preserving any
  # characters that appear inside quoted strings.

  Fixes the issue where Redis fails to start with:
  Warning: Could not create server TCP listening socket #:6379
This commit is contained in:
gparemsky 2025-07-30 08:24:48 -04:00
parent db4fc2a833
commit 50dae24779
1 changed files with 19 additions and 0 deletions

View File

@ -454,6 +454,25 @@ void loadServerConfigFromString(char *config) {
/* Skip comments and blank lines */
if (lines[i][0] == '#' || lines[i][0] == '\0') continue;
/* Strip inline comments */
char *comment = strchr(lines[i], '#');
if (comment) {
/* Check if # is inside quotes by counting quotes before it */
char *p = lines[i];
int in_quotes = 0;
while (p < comment) {
if (*p == '"' || *p == '\''){
in_quotes = !in_quotes;
}
p++;
}
/* Only treat as comment if not inside quotes */
if (!in_quotes){
*comment = '\0';
lines[i] = sdstrim(lines[i], " \t\r\n");
}
}
/* Split into arguments */
argv = sdssplitargs(lines[i],&argc);
if (argv == NULL) {