Skip to content

Commit

Permalink
Handle coverity defects in do_xlats() error messages (CID #1533664)
Browse files Browse the repository at this point in the history
  • Loading branch information
jejones3141 authored and arr2036 committed Jan 25, 2024
1 parent d5c9632 commit 04e9e32
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/bin/unit_test_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,26 @@ static void print_packet(FILE *fp, fr_radius_packet_t *packet, fr_pair_list_t *l
fr_pair_list_log(&default_log, 2, list);
}

/*
* A common function for reports of too much text when handling xlat
* and xlat_expr in do_xlats().
* The convolution deals with the edge case of the line being so long
* that it plus the surrounding text from the format could won't fit
* in the output sbuff, along with the fact that you don't print the
* %d or %.*s. OTOH it does include slen, but...
* * the format string is 41 characters minus 6 for %d and %.*s
* * given that slen reflects text read from line, once slen is
* large enough, we know line will fit
*/
static inline CC_HINT(always_inline) void too_much_text(fr_sbuff_t *out, ssize_t slen, fr_sbuff_t *line)
{
char const *format = "ERROR offset %d 'Too much text' ::%.*s::";

(void) fr_sbuff_in_sprintf(out, format, (int) slen,
fr_sbuff_remaining(out) - (strlen(format) - 5),
fr_sbuff_current(line));
}

/*
* Read a file composed of xlat's and expected results
*/
Expand Down Expand Up @@ -478,7 +498,7 @@ static bool do_xlats(fr_event_list_t *el, request_t *request, char const *filena

if (fr_sbuff_remaining(&line) > 0) {
talloc_free(xlat_ctx);
fr_sbuff_in_sprintf(&out, "ERROR offset %d 'Too much text' ::%s::", (int) slen, fr_sbuff_current(&line));
too_much_text(&out, slen, &line);
continue;
}

Expand Down Expand Up @@ -529,7 +549,7 @@ static bool do_xlats(fr_event_list_t *el, request_t *request, char const *filena

if (fr_sbuff_remaining(&line) > 0) {
talloc_free(xlat_ctx);
fr_sbuff_in_sprintf(&out, "ERROR offset %d 'Too much text' ::%s::", (int) slen, fr_sbuff_current(&line));
too_much_text(&out, slen, &line);
continue;
}

Expand All @@ -543,7 +563,7 @@ static bool do_xlats(fr_event_list_t *el, request_t *request, char const *filena
if (len < 0) {
char const *err = fr_strerror();
talloc_free(xlat_ctx);
fr_sbuff_in_sprintf(&out, "ERROR expanding xlat: %s", *err ? err : "no error provided");
(void) fr_sbuff_in_sprintf(&out, "ERROR expanding xlat: %s", *err ? err : "no error provided");
continue;
}

Expand Down

0 comments on commit 04e9e32

Please sign in to comment.