Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

efi: cper: fix scnprintf() use in cper_mem_err_location()

The last two if-clauses fail to update n, so whatever they might have
written at &msg[n] would be cut off by the final nul-termination.

That nul-termination is redundant; scnprintf(), just like snprintf(),
guarantees a nul-terminated output buffer, provided the buffer size is
positive.

And there's no need to discount one byte from the initial buffer;
vsnprintf() expects to be given the full buffer size - it's not going
to write the nul-terminator one beyond the given (buffer, size) pair.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>

authored by

Rasmus Villemoes and committed by
Ard Biesheuvel
5eff88dd e73f0f0e

+5 -6
+5 -6
drivers/firmware/efi/cper.c
··· 221 221 return 0; 222 222 223 223 n = 0; 224 - len = CPER_REC_LEN - 1; 224 + len = CPER_REC_LEN; 225 225 if (mem->validation_bits & CPER_MEM_VALID_NODE) 226 226 n += scnprintf(msg + n, len - n, "node: %d ", mem->node); 227 227 if (mem->validation_bits & CPER_MEM_VALID_CARD) ··· 258 258 n += scnprintf(msg + n, len - n, "responder_id: 0x%016llx ", 259 259 mem->responder_id); 260 260 if (mem->validation_bits & CPER_MEM_VALID_TARGET_ID) 261 - scnprintf(msg + n, len - n, "target_id: 0x%016llx ", 262 - mem->target_id); 261 + n += scnprintf(msg + n, len - n, "target_id: 0x%016llx ", 262 + mem->target_id); 263 263 if (mem->validation_bits & CPER_MEM_VALID_CHIP_ID) 264 - scnprintf(msg + n, len - n, "chip_id: %d ", 265 - mem->extended >> CPER_MEM_CHIP_ID_SHIFT); 264 + n += scnprintf(msg + n, len - n, "chip_id: %d ", 265 + mem->extended >> CPER_MEM_CHIP_ID_SHIFT); 266 266 267 - msg[n] = '\0'; 268 267 return n; 269 268 } 270 269