From 7c749893bfcf9f1a117ee6a2d35a6c8f0f05909d Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Fri, 7 Sep 2018 03:01:38 +0200 Subject: add osmo_str_tolower() and _toupper() with test We already have osmo_str2lower() and osmo_str2upper(), but these lack: * proper destination buffer bounds checking, * ability to call directly as printf() argument. Deprecate osmo_str2upper() and osmo_str2lower() because of missing bounds checking. Introduce osmo_str_tolower_buf(), osmo_str_toupper_buf() to provide bounds-safe conversion, also able to safely convert a buffer in-place. Introduce osmo_str_tolower(), osmo_str_toupper() that call the above _buf() equivalents using a static buffer[128] and returning the resulting string directly, convenient for direct printing. Possibly truncated but always safe. Add unit tests to utils_test.c. Replace all libosmocore uses of now deprecated osmo_str2lower(). Naming: the ctype.h API is called tolower() and toupper(), so just prepend 'osmo_str_' and don't separate 'to_lower'. Change-Id: Ib0ee1206b9f31d7ba25c31f8008119ac55440797 --- include/osmocom/core/utils.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index 976d4a85..0b54c880 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -7,6 +7,7 @@ #include #include #include +#include /*! \defgroup utils General-purpose utility functions * @{ @@ -57,8 +58,18 @@ char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__(( #define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__)); -void osmo_str2lower(char *out, const char *in); -void osmo_str2upper(char *out, const char *in); +void osmo_str2lower(char *out, const char *in) + OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead," + " to properly check target memory bounds"); +void osmo_str2upper(char *out, const char *in) + OSMO_DEPRECATED("Use osmo_str_toupper() or osmo_str_toupper_buf() instead," + " to properly check target memory bounds"); + +size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src); +const char *osmo_str_tolower(const char *src); + +size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src); +const char *osmo_str_toupper(const char *src); #define OSMO_SNPRINTF_RET(ret, rem, offset, len) \ do { \ -- cgit v1.2.3