Skip to content

Commit

Permalink
cras_string: Check for error before setting value
Browse files Browse the repository at this point in the history
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 <bailideng@google.com>
Reviewed-by: Li-Yu Yu <aaronyu@google.com>
Commit-Queue: Curtis Malainey <cujomalainey@chromium.org>
Auto-Submit: Baili Deng <bailideng@google.com>
Reviewed-by: Curtis Malainey <cujomalainey@chromium.org>
Tested-by: chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com <chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com>
  • Loading branch information
baili0411 authored and Chromeos LUCI committed Jan 24, 2024
1 parent 6183120 commit 900eb4b
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cras/src/common/cras_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 900eb4b

Please sign in to comment.