Skip to content

Commit a21642c

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 2d92bbc commit a21642c

1 file changed

Lines changed: 28 additions & 31 deletions

File tree

src/pg_stat_monitor.c

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,13 +3181,9 @@ static bool
31813181
append_comment_char(char *comments, size_t buf_len, size_t *idx, char c)
31823182
{
31833183
if (*idx >= buf_len - 1)
3184-
{
3185-
comments[buf_len - 1] = '\0';
31863184
return false;
3187-
}
31883185

3189-
comments[*idx] = c;
3190-
(*idx)++;
3186+
comments[(*idx)++] = c;
31913187

31923188
return true;
31933189
}
@@ -3196,7 +3192,6 @@ static void
31963192
extract_query_comments(const char *query, char *comments, size_t buf_len)
31973193
{
31983194
size_t curr_len = 0;
3199-
const char *q_iter = query;
32003195

32013196
Assert(query != NULL);
32023197

@@ -3206,44 +3201,46 @@ extract_query_comments(const char *query, char *comments, size_t buf_len)
32063201
return;
32073202
}
32083203

3209-
while (*q_iter)
3204+
for (const char *q_iter = query; *q_iter;)
32103205
{
3211-
/*
3212-
* multiline comments, + 1 is safe even if we've reach end of string
3213-
*/
32143206
if (*q_iter == '/' && *(q_iter + 1) == '*')
32153207
{
3216-
if (curr_len != 0)
3208+
/* Add separator between comments */
3209+
if (curr_len > 0)
32173210
{
32183211
if (!append_comment_char(comments, buf_len, &curr_len, ','))
3219-
return;
3212+
goto terminate;
32203213
if (!append_comment_char(comments, buf_len, &curr_len, ' '))
3221-
return;
3222-
}
3223-
while (*q_iter && *(q_iter + 1) && (*q_iter != '*' || *(q_iter + 1) != '/'))
3224-
{
3225-
if (!append_comment_char(comments, buf_len, &curr_len, *q_iter))
3226-
return;
3227-
q_iter++;
3214+
goto terminate;
32283215
}
32293216

3230-
if (*q_iter)
3231-
{
3232-
if (!append_comment_char(comments, buf_len, &curr_len, *q_iter))
3233-
return;
3234-
q_iter++;
3235-
}
3236-
if (*q_iter)
3217+
if (!append_comment_char(comments, buf_len, &curr_len, *(q_iter++)))
3218+
goto terminate;
3219+
if (!append_comment_char(comments, buf_len, &curr_len, *(q_iter++)))
3220+
goto terminate;
3221+
3222+
while (*q_iter)
32373223
{
3238-
if (!append_comment_char(comments, buf_len, &curr_len, *q_iter))
3239-
return;
3240-
q_iter++;
3224+
if (*q_iter == '*' && *(q_iter + 1) == '/')
3225+
{
3226+
if (!append_comment_char(comments, buf_len, &curr_len, *(q_iter++)))
3227+
goto terminate;
3228+
if (!append_comment_char(comments, buf_len, &curr_len, *(q_iter++)))
3229+
goto terminate;
3230+
break;
3231+
}
3232+
3233+
if (!append_comment_char(comments, buf_len, &curr_len, *(q_iter++)))
3234+
goto terminate;
32413235
}
32423236
}
3243-
3244-
q_iter++;
3237+
else
3238+
{
3239+
q_iter++
3240+
}
32453241
}
32463242

3243+
terminate:
32473244
comments[curr_len] = '\0';
32483245
}
32493246

0 commit comments

Comments
 (0)