From 20f7d0ecb4d04bd20b132fadad50b3f914ed9fe0 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Mon, 5 Mar 2018 04:19:21 +0100 Subject: fix osmo_mnc_from_str(): don't try to parse NULL In osmo_mnc_from_str() do not try to return some values even if the validation fails; hence don't try to decode a NULL pointer. That whole idea was half-baked and a can of worms to begin with. Change-Id: Ibaaa128ac60b941a015a31134eb52aef56bc6e22 --- src/gsm/gsm23003.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src/gsm') diff --git a/src/gsm/gsm23003.c b/src/gsm/gsm23003.c index 1f6bf7d0..574400de 100644 --- a/src/gsm/gsm23003.c +++ b/src/gsm/gsm23003.c @@ -206,7 +206,7 @@ void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn) * \param mnc[out] MNC result buffer, or NULL. * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL. * \returns zero on success, -EINVAL in case of surplus characters, negative errno in case of conversion - * errors. + * errors. In case of error, do not modify the out-arguments. */ int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits) { @@ -215,19 +215,17 @@ int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits) char *endptr; int rc = 0; - if (!mnc_str || !isdigit(mnc_str[0]) || strlen(mnc_str) > 3) { - /* return invalid, but give strtol a shot anyway, for callers that don't want to be - * strict */ - rc = -EINVAL; - } + if (!mnc_str || !isdigit(mnc_str[0]) || strlen(mnc_str) > 3) + return -EINVAL; + errno = 0; _mnc = strtol(mnc_str, &endptr, 10); if (errno) rc = -errno; else if (*endptr) - rc = -EINVAL; + return -EINVAL; if (_mnc < 0 || _mnc > 999) - rc = -EINVAL; + return -ERANGE; _mnc_3_digits = strlen(mnc_str) > 2; if (mnc) -- cgit v1.2.3