From d5cae44db6fcef611de2f58dd9f0982dcc26f6a5 Mon Sep 17 00:00:00 2001 From: Emerson Knapp Date: Wed, 1 Feb 2023 15:09:13 -0800 Subject: [PATCH] More precise emit_int error handling Signed-off-by: Emerson Knapp --- rcl/src/rcl/type_version_hash.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rcl/src/rcl/type_version_hash.c b/rcl/src/rcl/type_version_hash.c index a0e36bdcd..71146acfd 100644 --- a/rcl/src/rcl/type_version_hash.c +++ b/rcl/src/rcl/type_version_hash.c @@ -71,10 +71,15 @@ static int emit_key(yaml_emitter_t * emitter, const char * key) static int emit_int(yaml_emitter_t * emitter, size_t val, const char * fmt) { - // longest uint64 is 21 decimal digits + // longest uint64 is 20 decimal digits, plus one byte for trailing \0 char decimal_buf[21]; yaml_event_t event; - if ((size_t)snprintf(decimal_buf, sizeof(decimal_buf), fmt, val) >= sizeof(decimal_buf)) { + int ret = snprintf(decimal_buf, sizeof(decimal_buf), fmt, val); + if (ret < 0) { + emitter->problem = "Failed expanding integer"; + return 0; + } + if ((size_t)ret >= sizeof(decimal_buf)) { emitter->problem = "Decimal buffer overflow"; return 0; }