Skip to content

Commit 97b3bb5

Browse files
committed
Simplify code for parsing comments
The code was hard to read for no obvious gain so this attempts to make the code read more like one would expect. Also remove a redundant comment. Also fixes a potential off by one read.
1 parent 11ccd16 commit 97b3bb5

1 file changed

Lines changed: 24 additions & 29 deletions

File tree

src/pg_stat_monitor.c

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,13 +3167,9 @@ static bool
31673167
append_comment_char(char *comments, size_t buf_len, size_t *idx, char c)
31683168
{
31693169
if (*idx >= buf_len - 1)
3170-
{
3171-
comments[buf_len - 1] = '\0';
31723170
return false;
3173-
}
31743171

3175-
comments[*idx] = c;
3176-
(*idx)++;
3172+
comments[(*idx)++] = c;
31773173

31783174
return true;
31793175
}
@@ -3182,42 +3178,40 @@ static void
31823178
extract_query_comments(const char *query, char *comments, size_t buf_len)
31833179
{
31843180
size_t curr_len = 0;
3185-
const char *q_iter = query;
31863181

31873182
Assert(query != NULL);
31883183

3189-
while (*q_iter)
3184+
for (const char *q_iter = query; *q_iter;)
31903185
{
3191-
/*
3192-
* multiline comments, + 1 is safe even if we've reach end of string
3193-
*/
31943186
if (*q_iter == '/' && *(q_iter + 1) == '*')
31953187
{
3196-
if (curr_len != 0)
3188+
/* Add separator between comments */
3189+
if (curr_len > 0)
31973190
{
31983191
if (!append_comment_char(comments, buf_len, &curr_len, ','))
3199-
return;
3192+
goto terminate;
32003193
if (!append_comment_char(comments, buf_len, &curr_len, ' '))
3201-
return;
3202-
}
3203-
while (*q_iter && *(q_iter + 1) && (*q_iter != '*' || *(q_iter + 1) != '/'))
3204-
{
3205-
if (!append_comment_char(comments, buf_len, &curr_len, *q_iter))
3206-
return;
3207-
q_iter++;
3194+
goto terminate;
32083195
}
32093196

3210-
if (*q_iter)
3211-
{
3212-
if (!append_comment_char(comments, buf_len, &curr_len, *q_iter))
3213-
return;
3214-
q_iter++;
3215-
}
3216-
if (*q_iter)
3197+
if (!append_comment_char(comments, buf_len, &curr_len, *(q_iter++)))
3198+
goto terminate;
3199+
if (!append_comment_char(comments, buf_len, &curr_len, *(q_iter++)))
3200+
goto terminate;
3201+
3202+
while (*q_iter)
32173203
{
3218-
if (!append_comment_char(comments, buf_len, &curr_len, *q_iter))
3219-
return;
3220-
q_iter++;
3204+
if (*q_iter == '*' && *(q_iter + 1) == '/')
3205+
{
3206+
if (!append_comment_char(comments, buf_len, &curr_len, *(q_iter++)))
3207+
goto terminate;
3208+
if (!append_comment_char(comments, buf_len, &curr_len, *(q_iter++)))
3209+
goto terminate;
3210+
break;
3211+
}
3212+
3213+
if (!append_comment_char(comments, buf_len, &curr_len, *(q_iter++)))
3214+
goto terminate;
32213215
}
32223216
}
32233217
else
@@ -3226,6 +3220,7 @@ extract_query_comments(const char *query, char *comments, size_t buf_len)
32263220
}
32273221
}
32283222

3223+
terminate:
32293224
comments[curr_len] = '\0';
32303225
}
32313226

0 commit comments

Comments
 (0)