From 91d9f33e86b39737617d2d3fbc016673ba3b8cb1 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 21 Aug 2013 15:31:48 +0100 Subject: [PATCH] Fix some issues with comments in rcl. --- src/rcl/rcl_parser.c | 55 +++++++++++++++++++++++++++++--------------- test/test.cfg | 15 ++++++++++-- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/rcl/rcl_parser.c b/src/rcl/rcl_parser.c index cf7384019..90202b6c5 100644 --- a/src/rcl/rcl_parser.c +++ b/src/rcl/rcl_parser.c @@ -84,48 +84,49 @@ rspamd_cl_skip_comments (struct rspamd_cl_parser *parser, GError **err) parser->state != RSPAMD_RCL_STATE_MCOMMENT) { while (p < chunk->end) { if (*p == '\n') { - rspamd_cl_chunk_skipc (chunk, *p); + rspamd_cl_chunk_skipc (chunk, *++p); break; } - rspamd_cl_chunk_skipc (chunk, *p); - p ++; + rspamd_cl_chunk_skipc (chunk, *++p); } } } else if (*p == '/' && chunk->remain >= 2) { - p ++; + rspamd_cl_chunk_skipc (chunk, *++p); if (*p == '/' && parser->state != RSPAMD_RCL_STATE_SCOMMENT && parser->state != RSPAMD_RCL_STATE_MCOMMENT) { chunk->pos = p; while (p < chunk->end) { if (*p == '\n') { - rspamd_cl_chunk_skipc (chunk, *p); + rspamd_cl_chunk_skipc (chunk, *++p); break; } - rspamd_cl_chunk_skipc (chunk, *p); - p ++; + rspamd_cl_chunk_skipc (chunk, *++p); } } else if (*p == '*') { comments_nested ++; - chunk->pos = p; + rspamd_cl_chunk_skipc (chunk, *++p); while (p < chunk->end) { if (*p == '*') { - rspamd_cl_chunk_skipc (chunk, *p); - p ++; - rspamd_cl_chunk_skipc (chunk, *p); + rspamd_cl_chunk_skipc (chunk, *++p); if (*p == '/') { comments_nested --; if (comments_nested == 0) { + rspamd_cl_chunk_skipc (chunk, *++p); break; } } - p ++; - rspamd_cl_chunk_skipc (chunk, *p); + rspamd_cl_chunk_skipc (chunk, *++p); } - rspamd_cl_chunk_skipc (chunk, *p); - p ++; + else if (p[0] == '/' && chunk->remain >= 2 && p[1] == '*') { + comments_nested ++; + rspamd_cl_chunk_skipc (chunk, *++p); + rspamd_cl_chunk_skipc (chunk, *++p); + continue; + } + rspamd_cl_chunk_skipc (chunk, *++p); } if (comments_nested != 0) { rspamd_cl_set_err (chunk, RSPAMD_CL_ENESTED, "comments nesting is invalid", err); @@ -213,6 +214,20 @@ rspamd_cl_lex_is_atom_end (const guchar c) return FALSE; } +static inline gboolean +rspamd_cl_lex_is_comment (const guchar c1, const guchar c2) +{ + if (c1 == '/') { + if (c2 == '/' || c2 == '*') { + return TRUE; + } + } + else if (c1 == '#') { + return TRUE; + } + return FALSE; +} + /** * Parse possible number * @param parser @@ -623,7 +638,7 @@ rspamd_cl_parse_key (struct rspamd_cl_parser *parser, return FALSE; } } - else if ((p[0] == '/' && p[1] == '/') || *p == '#') { + else if (rspamd_cl_lex_is_comment (p[0], p[1])) { /* Check for comment */ if (!rspamd_cl_skip_comments (parser, err)) { return FALSE; @@ -772,7 +787,7 @@ rspamd_cl_parse_value (struct rspamd_cl_parser *parser, struct rspamd_cl_chunk * default: /* Skip any spaces and comments */ if (g_ascii_isspace (*p) || - (p[0] == '/' && p[1] == '/') || *p == '#') { + rspamd_cl_lex_is_comment (p[0], p[1])) { while (p < chunk->end && g_ascii_isspace (*p)) { rspamd_cl_chunk_skipc (chunk, *p); p ++; @@ -845,11 +860,13 @@ rspamd_cl_parse_after_value (struct rspamd_cl_parser *parser, struct rspamd_cl_c rspamd_cl_chunk_skipc (chunk, *p); p ++; } - else if ((p[0] == '/' && p[1] == '/') || *p == '#') { + else if (rspamd_cl_lex_is_comment (p[0], p[1])) { /* Skip comment */ if (!rspamd_cl_skip_comments (parser, err)) { return FALSE; } + /* Treat comment as a separator */ + got_sep = TRUE; p = chunk->pos; } else if (*p == ',') { @@ -1131,7 +1148,7 @@ rspamd_cl_state_machine (struct rspamd_cl_parser *parser, GError **err) /* Now we need to skip all spaces */ while (p < chunk->end) { if (!g_ascii_isspace (*p)) { - if ((p[0] == '/' && p[1] == '/') || *p == '#') { + if (rspamd_cl_lex_is_comment (p[0], p[1])) { /* Skip comment */ if (!rspamd_cl_skip_comments (parser, err)) { return FALSE; diff --git a/test/test.cfg b/test/test.cfg index 880b363d4..3300a82a4 100644 --- a/test/test.cfg +++ b/test/test.cfg @@ -1,5 +1,16 @@ section2 { param = "value"; - param2 = value - array = [ 100500, 10s, 50mb ] + param2 = value // comment + array = [ 100500, 10s, 50mb ] # array + /* + * This should be commented + * + * test123 = test + */ } +/* +section3 { + key = value +} +/* Nested comment */ +*/ -- 2.39.5