summaryrefslogtreecommitdiffstats
path: root/src/gsm/gsm0808_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/gsm/gsm0808_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/gsm/gsm0808_utils.c')
-rw-r--r--src/gsm/gsm0808_utils.c40
1 files changed, 33 insertions, 7 deletions
diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index e0cdaaf6..52e46743 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -595,11 +595,13 @@ int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp)
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 @@ char *osmo_lcls_dump(const struct osmo_lcls *lcls)
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 @@ char *osmo_gcr_dump(const struct osmo_lcls *lcls)
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 @@ const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
#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);
+}
+
/*! @} */