From fcf81b5deb8d02ba45907e90bb6668c67986a028 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 19 Oct 2017 15:50:44 +0200 Subject: Make osmo_apn_to_str() more robust Previously it would crash on NULL input. Let's handle it gracefully instead. Corresponding test case is also added. Change-Id: I587153e49d1c92128fac3ae5c124adba9592378e --- src/gsm/apn.c | 3 +++ tests/gprs/gprs_test.c | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/gsm/apn.c b/src/gsm/apn.c index 8d4b2bfc..f8303db9 100644 --- a/src/gsm/apn.c +++ b/src/gsm/apn.c @@ -53,6 +53,9 @@ char * osmo_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t apn_enc_len char *str = out_str; size_t rest_chars = apn_enc_len; + if (!apn_enc) + return NULL; + while (rest_chars > 0 && apn_enc[0]) { size_t label_size = apn_enc[0]; if (label_size + 1 > rest_chars) diff --git a/tests/gprs/gprs_test.c b/tests/gprs/gprs_test.c index be80e5c0..6f16fb2f 100644 --- a/tests/gprs/gprs_test.c +++ b/tests/gprs/gprs_test.c @@ -90,6 +90,13 @@ static void test_gsm_03_03_apn(void) OSMO_ASSERT(osmo_apn_to_str(NULL, input, ARRAY_SIZE(input) - 1) == NULL); } + { + /* NULL input */ + uint8_t input[] = { 0x1, 65 }; + char *output = "LOL"; + OSMO_ASSERT(osmo_apn_to_str(output, NULL, ARRAY_SIZE(input) - 1) == NULL); + } + { uint8_t input[] = { 0x3, 65, 66, 67, 0x2, 90, 122 }; const char *output = "ABC.Zz"; -- cgit v1.2.3