logging: Fix possible out-of-bound access in log_output

Coverity issue #187067

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2018-09-05 08:21:41 +02:00 committed by Carles Cufí
commit 9f7e25ad22
2 changed files with 7 additions and 1 deletions

View file

@ -273,7 +273,8 @@ u32_t log_msg_nargs_get(struct log_msg *msg);
* @param msg Standard log message. * @param msg Standard log message.
* @param arg_idx Argument index. * @param arg_idx Argument index.
* *
* @return Argument value. * @return Argument value or 0 if arg_idx exceeds number of arguments in the
* message.
*/ */
u32_t log_msg_arg_get(struct log_msg *msg, u32_t arg_idx); u32_t log_msg_arg_get(struct log_msg *msg, u32_t arg_idx);

View file

@ -116,6 +116,11 @@ u32_t log_msg_arg_get(struct log_msg *msg, u32_t arg_idx)
{ {
u32_t arg; u32_t arg;
/* Return early if requested argument not present in the message. */
if (arg_idx >= msg->hdr.params.std.nargs) {
return 0;
}
if (msg->hdr.params.std.nargs <= LOG_MSG_NARGS_SINGLE_CHUNK) { if (msg->hdr.params.std.nargs <= LOG_MSG_NARGS_SINGLE_CHUNK) {
arg = msg->payload.single.args[arg_idx]; arg = msg->payload.single.args[arg_idx];
} else { } else {