From b91fe5598024bbda9f22773c404e3f4d98d53159 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Wed, 30 Jul 2014 11:05:08 +0200 Subject: [PATCH] Make sure meshlink_strerror() always returns a valid pointer to a string. --- src/meshlink.c | 2 ++ src/meshlink.h | 1 + 2 files changed, 3 insertions(+) diff --git a/src/meshlink.c b/src/meshlink.c index 80404370..f4e213c5 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -630,6 +630,8 @@ static const char *errstr[] = { }; const char *meshlink_strerror(meshlink_errno_t err) { + if(err < 0 || err >= sizeof errstr / sizeof *errstr) + return "Invalid error code"; return errstr[err]; } diff --git a/src/meshlink.h b/src/meshlink.h index 39f278d2..7fd71fe8 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -72,6 +72,7 @@ struct meshlink_channel { * @param err An error code returned by MeshLink. * * @return A pointer to a string containing the description of the error code. + * This function will always return a valid pointer, even if an invalid error code has been passed. */ extern const char *meshlink_strerror(meshlink_errno_t err); -- 2.39.2