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
diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index e0cdaaf..52e4674 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -595,11 +595,13 @@
static char dbuf[256];
/*! Dump LCLS parameters (GCR excluded) into string for printing.
+ * \param[out] buf caller-allocated output string buffer
+ * \param[in] buf_len size of buf in bytes
* \param[in] lcls pointer to the struct to print.
* \returns string representation of LCLS or NULL on error. */
-char *osmo_lcls_dump(const struct osmo_lcls *lcls)
+char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
{
- struct osmo_strbuf s = { .buf = dbuf, .len = 256 };
+ struct osmo_strbuf s = { .buf = buf, .len = buf_len };
if (!lcls)
return NULL;
@@ -612,12 +614,22 @@
return dbuf;
}
+/*! Dump LCLS parameters (GCR excluded) into static string buffer for printing.
+ * \param[in] lcls pointer to the struct to print.
+ * \returns string representation of LCLS in static buffer or NULL on error. */
+char *osmo_lcls_dump(const struct osmo_lcls *lcls)
+{
+ return osmo_lcls_dump_buf(dbuf, sizeof(dbuf), lcls);
+}
+
/*! Dump GCR struct into string for printing.
+ * \param[out] buf caller-allocated output string buffer
+ * \param[in] buf_len size of buf in bytes
* \param[in] lcls pointer to the struct to print.
* \returns string representation of GCR or NULL on error. */
-char *osmo_gcr_dump(const struct osmo_lcls *lcls)
+char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
{
- struct osmo_strbuf s = { .buf = dbuf, .len = 256 };
+ struct osmo_strbuf s = { .buf = buf, .len = buf_len };
if (!lcls)
return NULL;
@@ -631,6 +643,15 @@
return dbuf;
}
+/*! Dump GCR struct into static string buffer for printing.
+ * \param[in] lcls pointer to the struct to print.
+ * \returns string representation of GCR in static buffer or NULL on error. */
+char *osmo_gcr_dump(const struct osmo_lcls *lcls)
+{
+ return osmo_gcr_dump_buf(dbuf, sizeof(dbuf), lcls);
+}
+
+
/*! Encode TS 08.08 Encryption Information IE
* \param[out] msg Message Buffer to which IE is to be appended
* \param[in] ei Encryption Information to be encoded
@@ -1838,13 +1859,18 @@
#undef APPEND_STR
#undef APPEND_CELL_ID_U
-const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
+char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct)
{
- static char buf[128];
- snprintf(buf, sizeof(buf), "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
+ snprintf(buf, buf_len, "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
ct->ch_indctr, ct->ch_rate_type,
osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
return buf;
}
+const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
+{
+ static char buf[128];
+ return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
+}
+
/*! @} */