From 900eb4beae414c05bdc3e3c6607696c9dd342045 Mon Sep 17 00:00:00 2001 From: Baili Deng Date: Wed, 24 Jan 2024 13:05:07 +0800 Subject: [PATCH] cras_string: Check for error before setting value In parse_float and parse_double, finish checking the errno values first before setting the parsed value. BUG=b:320231979 TEST=Unit tests Change-Id: I658e50bfd33b9efbc727f6e300eed70efea29278 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5233200 Tested-by: Baili Deng Reviewed-by: Li-Yu Yu Commit-Queue: Curtis Malainey Auto-Submit: Baili Deng Reviewed-by: Curtis Malainey Tested-by: chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com --- cras/src/common/cras_string.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cras/src/common/cras_string.h b/cras/src/common/cras_string.h index 37599af44..d997f8c4a 100644 --- a/cras/src/common/cras_string.h +++ b/cras/src/common/cras_string.h @@ -58,10 +58,11 @@ static __attribute__((warn_unused_result)) inline int parse_float( } char* endptr; errno = 0; - *out = strtof(str, &endptr); + float f = strtof(str, &endptr); if (endptr == str) { return -EINVAL; } + *out = f; return -errno; } @@ -74,10 +75,11 @@ static __attribute__((warn_unused_result)) inline int parse_double( } char* endptr; errno = 0; - *out = strtod(str, &endptr); + double d = strtod(str, &endptr); if (endptr == str) { return -EINVAL; } + *out = d; return -errno; }