Skip to content

Commit

Permalink
MINOR: log: always consider "+M" option in lf_text_len()
Browse files Browse the repository at this point in the history
Historically, when lf_text_len() or lf_text() were called with a NULL
string and "+M" option was set, "-" would be printed.

However, if the input string was simply an empty one with len > 0, then
nothing would be printed. This can happen if lf_text() is called with
an empty string because in this case len is set to size (indeed, for
performance reasons we don't pre-compute the length, we stop as soon
as we encounter a NULL-byte)

In practise, a lot of call places making use of lf_text() or lf_text_len()
try their best to avoid calling lf_text() with an empty string, and
instead explicitly call lf_text_len() with NULL as parameter to consider
the "+M" option.

But this is not enough, as shown in GH haproxy#2797, there could still be places
where lf_text() is called with an empty string. In such case, instead of
ignoring the "+M" option, let's check after _lf_text_len() if the returned
pointer differs from the original one. If both are equal, then it means
that nothing was printed (ie: result of empty string): in that case we
check the "+M" option to print "-" when possible.

While this commit seems harmless, it's probably better to avoid
backporting it since it could break existing applications relying on the
historical behavior.
  • Loading branch information
Darlelet committed Nov 28, 2024
1 parent 3e47047 commit e379761
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -2461,21 +2461,23 @@ static inline char *_lf_quotetext_len(char *dst, const char *src,
*/
static char *lf_text_len(char *dst, const char *src, size_t len, size_t size, struct lf_buildctx *ctx)
{
char *ret;

if ((ctx->options & (LOG_OPT_QUOTE | LOG_OPT_ENCODE_JSON)))
return _lf_quotetext_len(dst, src, len, size, ctx);
else if ((ctx->options & LOG_OPT_ENCODE_CBOR) ||
(src && len))
return _lf_text_len(dst, src, len, size, ctx);

if (size < 2)
return NULL;
ret = _lf_text_len(dst, src, len, size, ctx);
if (dst != ret ||
(ctx->options & LOG_OPT_ENCODE_CBOR) ||
!(ctx->options & LOG_OPT_MANDATORY))
return ret;

if ((ctx->options & LOG_OPT_MANDATORY))
return _lf_text_len(dst, "-", 1, size, ctx);
/* empty output and "+M" option is set, try to print '-' */

*dst = '\0';
if (size < 2)
return NULL;

return dst;
return _lf_text_len(dst, "-", 1, size, ctx);
}

/*
Expand Down

0 comments on commit e379761

Please sign in to comment.