summaryrefslogtreecommitdiffstats
path: root/src/utils.c
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2019-03-18 18:27:00 +0100
committerHarald Welte <laforge@gnumonks.org>2019-04-03 18:03:14 +0200
commit4a62eda225ab7f3c9556990c81a6fc5e19b5eec8 (patch)
tree5eab6ad641d4411ff31a15a51f6d0c14bccdb6f3 /src/utils.c
parent98ed3393cdfdf35ad0bb79f454474f2b27bf3d56 (diff)
Add _buf() functions to bypass static string buffers
We have a number of static buffers in use in libosmo*. This means the related functions are not usable in a thread-safe way. While we so far don't have many multi-threaded programs in the osmocom universe, the static buffers also prevent us from calling the same e.g. string-ify function twice within a single printf() call. Let's make sure there's an alternative function in all those cases, where the user can pass in a caller-allocated buffer + size, and make the 'classic' function with the static buffer a wrapper around that _buf() variant. Change-Id: Ibf85f79e93244f53b2684ff6f1095c5b41203e05
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c31
1 files changed, 22 insertions, 9 deletions
diff --git a/src/utils.c b/src/utils.c
index 019e7338..47963650 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -273,17 +273,20 @@ const char *osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const unsigned
return out_buf;
}
-/*! Convert a sequence of unpacked bits to ASCII string
+/*! Convert a sequence of unpacked bits to ASCII string, in user-supplied buffer.
+ * \param[out] buf caller-provided output string buffer
+ * \param[out] buf_len size of buf in bytes
* \param[in] bits A sequence of unpacked bits
* \param[in] len Length of bits
+ * \returns string representation in static buffer.
*/
-char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
+char *osmo_ubit_dump_buf(char *buf, size_t buf_len, const uint8_t *bits, unsigned int len)
{
int i;
- if (len > sizeof(hexd_buff)-1)
- len = sizeof(hexd_buff)-1;
- memset(hexd_buff, 0, sizeof(hexd_buff));
+ if (len > buf_len-1)
+ len = buf_len-1;
+ memset(buf, 0, buf_len);
for (i = 0; i < len; i++) {
char outch;
@@ -301,10 +304,20 @@ char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
outch = 'E';
break;
}
- hexd_buff[i] = outch;
+ buf[i] = outch;
}
- hexd_buff[sizeof(hexd_buff)-1] = 0;
- return hexd_buff;
+ buf[buf_len-1] = 0;
+ return buf;
+}
+
+/*! Convert a sequence of unpacked bits to ASCII string, in static buffer.
+ * \param[in] bits A sequence of unpacked bits
+ * \param[in] len Length of bits
+ * \returns string representation in static buffer.
+ */
+char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
+{
+ return osmo_ubit_dump_buf(hexd_buff, sizeof(hexd_buff), bits, len);
}
/*! Convert binary sequence to hexadecimal ASCII string
@@ -632,7 +645,7 @@ const char *osmo_escape_str(const char *str, int in_len)
* \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length.
* \returns buf containing a quoted and escaped representation, possibly truncated.
*/
-const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize)
+char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize)
{
int l;
if (!str)