Skip to content

fix: Allow inline comments in clamd.conf #1308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions common/optparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@
#endif

#define MAXCMDOPTS 150
#define MAX_OPTION_LINE_LENGTH 1024

#define MATCH_NUMBER "^[0-9]+$"
#define MATCH_SIZE "^[0-9]+[KMG]?$"
#define MATCH_BOOL "^(yes|true|1|no|false|0)$"
#define MATCH_NUMBER "^[0-9]+((( +)?#(.*))?)$"
#define MATCH_SIZE "^[0-9]+[KMG]?(( +)?#(.*))?$"
#define MATCH_BOOL "^(yes|true|1|no|false|0)(( +)?#(.*))?$"

#define FLAG_MULTIPLE 1 /* option can be used multiple times */
#define FLAG_REQUIRED 2 /* arg is required, even if there's a default value */
Expand Down Expand Up @@ -964,15 +965,18 @@ struct optstruct *optparse(const char *cfgfile, int argc, char **argv, int verbo
FILE *fs = NULL;
const struct clam_option *optentry;
char *pt;
const char *name = NULL, *arg;
const char *name = NULL;
char *arg;
int i, err = 0, lc = 0, sc = 0, opt_index, line = 0, ret;
struct optstruct *opts = NULL, *opts_last = NULL, *opt;
char buffer[1024], *buff;
char buffer[MAX_OPTION_LINE_LENGTH], *buff;
struct option longopts[MAXCMDOPTS];
char shortopts[MAXCMDOPTS];
regex_t regex;
long long numarg, lnumarg, lnumlimit;
int regflags = REG_EXTENDED | REG_NOSUB;
int regflags = REG_EXTENDED | REG_NOSUB;
const char *inlinecomment = NULL;
char *trim_comment;

#ifdef _WIN32
if (!is_initialized) {
Expand Down Expand Up @@ -1054,8 +1058,9 @@ struct optstruct *optparse(const char *cfgfile, int argc, char **argv, int verbo
break;

buff = buffer;
for (i = 0; i < (int)strlen(buff) - 1 && (buff[i] == ' ' || buff[i] == '\t'); i++)
for (i = 0; i < (int)strlen(buff) - 1 && (buff[i] == ' ' || buff[i] == '\t'); i++) {
;
}
buff += i;
line++;
if (strlen(buff) <= 2 || buff[0] == '#')
Expand All @@ -1076,11 +1081,13 @@ struct optstruct *optparse(const char *cfgfile, int argc, char **argv, int verbo
}
name = buff;
*pt++ = 0;
for (i = 0; i < (int)strlen(pt) - 1 && (pt[i] == ' ' || pt[i] == '\t'); i++)
for (i = 0; i < (int)strlen(pt) - 1 && (pt[i] == ' ' || pt[i] == '\t'); i++) {
;
}
pt += i;
for (i = strlen(pt); i >= 1 && (pt[i - 1] == ' ' || pt[i - 1] == '\t' || pt[i - 1] == '\n'); i--)
for (i = strlen(pt); i >= 1 && (pt[i - 1] == ' ' || pt[i - 1] == '\t' || pt[i - 1] == '\n'); i--) {
;
}
if (!i) {
if (verbose)
fprintf(stderr, "ERROR: Missing argument for option at %s:%d\n", cfgfile, line);
Expand Down Expand Up @@ -1228,7 +1235,18 @@ struct optstruct *optparse(const char *cfgfile, int argc, char **argv, int verbo
}
}

numarg = -1;
/* Find and remove inline comments. */
numarg = -1;
inlinecomment = strchr(arg, '#');
if (inlinecomment != NULL) {
/* Found a '#', indicating an inline comment. Strip it off along with any leading spaces or tabs. */
arg = strtok(arg, "#");
trim_comment = arg + strlen(arg) - 1;
while (trim_comment >= arg && (*trim_comment == ' ' || *trim_comment == '\t')) {
*(trim_comment--) = '\0';
}
}

switch (optentry->argtype) {
case CLOPT_TYPE_STRING:
if (!arg)
Expand Down
2 changes: 1 addition & 1 deletion common/optparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct clam_option {
int argtype;
const char *regex;
long long numarg;
const char *strarg;
char *strarg;
int flags;
int owner;
const char *description;
Expand Down
Loading