Skip to content

fix: Allow inline comments in clamd.conf - #1308

Merged
val-ms merged 2 commits into
Cisco-Talos:mainfrom
userwiths:fix/allow-inline-comments
May 25, 2025
Merged

val-ms merged 2 commits into
Cisco-Talos:mainfrom
userwiths:fix/allow-inline-comments

Conversation

@userwiths

Copy link
Copy Markdown
Contributor

This PR attempts to resolve the issue mentioned in #1175

  1. Regex has been changed in order to allow matching a given line even if it has a comment (separated with space or not) from the main expression.
  2. When passing through an argument, check if it contains the comment character # and if it does, try to get the content on its left side.

Thoughts:

  1. I have some doubts strtok is the best choice.
  2. Another approach, better imo, would be to get the first match from the regex expression. I failed to find an example in the project where cli_regexec returns the made matches (most likely in the 4th argument, but it seems its used mostly with NULL through out the project).

@val-ms

val-ms commented Jul 19, 2024

Copy link
Copy Markdown
Contributor

Thanks for the PR! I will try to review this after we have things lined up to publish 1.4.0, or after 1.4.0 ships.

@val-ms
val-ms force-pushed the fix/allow-inline-comments branch from ef7d483 to 8e9d15f Compare January 10, 2025 03:39

@val-ms val-ms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rebased this PR to re-run the tests.

I found just minor clang-format issues.

In manual testing, I found that for string-based options (vs bool, size, or number args) -- spaces before a # end up getting included in the option value.

For example, I tried this for freshclam.conf:

DatabaseMirror http://localhost:8000 # database.clamav.net

The result was that it tried to download daily.cvd from:
http://localhost:8000 /daily.cvd (with a space in the middle).

I would have to remove the space between 8000 and # to make it work. Like this:

DatabaseMirror http://localhost:8000# database.clamav.net

But this doesn't look very natural and is bound to get complaints from users that try this feature.

I also had trouble with getting a bool to work. I tried this in freshclam.conf, but it didn't enable verbose mode:

LogVerbose yes #no

@userwiths

userwiths commented Jan 14, 2025 •

Copy link
Copy Markdown
Contributor Author

Yeah, the clang-format I probably forgot about, now it should be formated as its supposed to.
I expected the tests to fail with the issue about spaces after arg, but on second thought there were no inline comments in the tests, currently added trimming of trailing spaces

Tried to see how I could implement a test for this fix, but I'm not sure if its possible to printout the options even in debug mode, as its managed in libclamav and not here.

Other caveat

If someone has # as a part of his option (dont know how viable that is, as a part of directory/file name for example) it would probably break their config too.

userwiths and others added 2 commits May 24, 2025 14:32
… files

The config parser will now treat a '#' character in a config file as an
inline comment, allowing users to write configs like this:
```freshclam.conf
DatabaseMirror http://localhost:8000  # My private server
```
E.g. this will also be valid:
```freshclam.conf
DatabaseMirror http://localhost:8000<tab># My private server.
```

Also:
- Fix compile warning regarding unnecessarily const string variable.
- Add parenthesis to resolve inconsistency between clang-format and
  vscode auto-format.
- Add a macro defining the max config line length.
@val-ms
val-ms force-pushed the fix/allow-inline-comments branch from 971f3e7 to 7bfe63f Compare May 24, 2025 19:09

@val-ms val-ms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the clang-format issue and also the trailing spaces issue. I tried a variety of variations of trailing spaces for options with strings, booleans, numbers. As far as I can tell it works nicely.

I am seeing a warning with this change when building:

/home/micah/dev/clamav/clamav-1/common/optparser.c: In function ‘optparse’:
/home/micah/dev/clamav/clamav-1/common/optparser.c:1195:35: warning: passing argument 1 of ‘strtok’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
 1195 |             arg          = strtok(arg, "#");
      |                                   ^~~
In file included from /home/micah/dev/clamav/clamav-1/common/optparser.c:36:
/usr/include/string.h:336:14: note: expected ‘char * __restrict__’ but argument is of type ‘const char *’
  336 | extern char *strtok (char *__restrict __s, const char *__restrict __delim)
      |              ^~~~~~
/home/micah/dev/clamav/clamav-1/common/optparser.c:1196:26: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
 1196 |             trim_comment = arg + strlen(arg) - 1;
      |                          ^

I took the liberty of fixing this and extending your change to also trim trailing tabs. I tidied the commits and rebased them with main so we've got all the latest commits in the branch. Pending a good run through GitHub Actions and Jenkins, I'm happy with this and will merge it.

@val-ms
val-ms merged commit 6c50810 into Cisco-Talos:main May 25, 2025
@val-ms

val-ms commented May 25, 2025

Copy link
Copy Markdown
Contributor

Thanks again for your work on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants