Change in libosmocore[master]: use osmo_static_string() instead of numerous static buffers

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Tue Feb 26 19:14:02 UTC 2019


Neels Hofmeyr has uploaded this change for review. ( https://gerrit.osmocom.org/13062


Change subject: use osmo_static_string() instead of numerous static buffers
......................................................................

use osmo_static_string() instead of numerous static buffers

Change-Id: Id0eab72fa71630c8eb9edafaa370a41ca16329aa
---
M include/osmocom/gsm/gsm0808_utils.h
M include/osmocom/gsm/gsm23003.h
M src/fsm.c
M src/gb/gprs_ns.c
M src/gsm/abis_nm.c
M src/gsm/apn.c
M src/gsm/gsm0808_utils.c
M src/gsm/gsm23003.c
M src/gsm/gsm48.c
M src/gsm/gsm_utils.c
M src/gsm/rsl.c
M src/msgb.c
M src/sim/core.c
M src/socket.c
M src/utils.c
15 files changed, 119 insertions(+), 94 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/62/13062/1

diff --git a/include/osmocom/gsm/gsm0808_utils.h b/include/osmocom/gsm/gsm0808_utils.h
index e1e345d..4ca355a 100644
--- a/include/osmocom/gsm/gsm0808_utils.h
+++ b/include/osmocom/gsm/gsm0808_utils.h
@@ -73,7 +73,8 @@
 { return get_value_string(gsm0808_cell_id_discr_names, id_discr); }
 
 const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid);
-const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid);
+const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
+	OSMO_DEPRECATED("Use gsm0808_cell_id_name() instead, which now returns distinct static buffers for every invocation");
 const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil);
 int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil);
 int gsm0808_cell_id_u_name(char *buf, size_t buflen,
diff --git a/include/osmocom/gsm/gsm23003.h b/include/osmocom/gsm/gsm23003.h
index b34a677..7c04760 100644
--- a/include/osmocom/gsm/gsm23003.h
+++ b/include/osmocom/gsm/gsm23003.h
@@ -99,9 +99,11 @@
 const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits);
 const char *osmo_plmn_name(const struct osmo_plmn_id *plmn);
 const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn);
+	OSMO_DEPRECATED("Use osmo_plmn_name() instead, which now returns distinct static buffers for every invocation");
 const char *osmo_lai_name(const struct osmo_location_area_id *lai);
 const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi);
 const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi);
+	OSMO_DEPRECATED("Use osmo_cgi_name() instead, which now returns distinct static buffers for every invocation");
 const char *osmo_gummei_name(const struct osmo_gummei *gummei);
 
 void osmo_plmn_to_bcd(uint8_t *bcd_dst, const struct osmo_plmn_id *plmn);
diff --git a/src/fsm.c b/src/fsm.c
index 6e15ab7..e8c008e 100644
--- a/src/fsm.c
+++ b/src/fsm.c
@@ -391,9 +391,10 @@
  */
 const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event)
 {
-	static char buf[32];
 	if (!fsm->event_names) {
-		snprintf(buf, sizeof(buf), "%"PRIu32, event);
+		const size_t len = 32;
+		char *buf = osmo_static_string(len);
+		snprintf(buf, len, "%"PRIu32, event);
 		return buf;
 	} else
 		return get_value_string(fsm->event_names, event);
@@ -421,9 +422,10 @@
  */
 const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state)
 {
-	static char buf[32];
 	if (state >= fsm->num_states) {
-		snprintf(buf, sizeof(buf), "unknown %"PRIu32, state);
+		const size_t len = 32;
+		char *buf = osmo_static_string(len);
+		snprintf(buf, len, "unknown %"PRIu32, state);
 		return buf;
 	} else
 		return fsm->states[state].name;
diff --git a/src/gb/gprs_ns.c b/src/gb/gprs_ns.c
index 54964d3..5706701 100644
--- a/src/gb/gprs_ns.c
+++ b/src/gb/gprs_ns.c
@@ -1203,15 +1203,16 @@
 
 const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc)
 {
-	static char buf[80];
+	const size_t buflen = 80;
+	char *buf = osmo_static_string(buflen);
 
 	switch(nsvc->ll) {
 	case GPRS_NS_LL_UDP:
-		snprintf(buf, sizeof(buf), "%s:%u",
+		snprintf(buf, buflen, "%s:%u",
 			 inet_ntoa(nsvc->ip.bts_addr.sin_addr), osmo_ntohs(nsvc->ip.bts_addr.sin_port));
 		break;
 	case GPRS_NS_LL_FR_GRE:
-		snprintf(buf, sizeof(buf), "%s:%u",
+		snprintf(buf, buflen, "%s:%u",
 			 inet_ntoa(nsvc->frgre.bts_addr.sin_addr), osmo_ntohs(nsvc->frgre.bts_addr.sin_port));
 		break;
 	default:
@@ -1219,7 +1220,7 @@
 		break;
 	}
 
-	buf[sizeof(buf) - 1] = '\0';
+	buf[buflen - 1] = '\0';
 
 	return buf;
 }
diff --git a/src/gsm/abis_nm.c b/src/gsm/abis_nm.c
index 49d05ba..5b8df57 100644
--- a/src/gsm/abis_nm.c
+++ b/src/gsm/abis_nm.c
@@ -930,8 +930,9 @@
 
 const char *abis_nm_dump_foh(const struct abis_om_fom_hdr *foh)
 {
-	static char foh_buf[128];
-	snprintf(foh_buf, sizeof(foh_buf), "OC=%s(%02x) INST=(%02x,%02x,%02x)",
+	const size_t len = 128;
+	char *foh_buf = osmo_static_string(len);
+	snprintf(foh_buf, len, "OC=%s(%02x) INST=(%02x,%02x,%02x)",
 		get_value_string(abis_nm_obj_class_names, foh->obj_class),
 		foh->obj_class, foh->obj_inst.bts_nr, foh->obj_inst.trx_nr,
 		foh->obj_inst.ts_nr);
diff --git a/src/gsm/apn.c b/src/gsm/apn.c
index 2674663..99030d4 100644
--- a/src/gsm/apn.c
+++ b/src/gsm/apn.c
@@ -25,6 +25,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <osmocom/core/utils.h>
 #include <osmocom/gsm/apn.h>
 
 #define APN_OI_GPRS_FMT	"mnc%03u.mcc%03u.gprs"
@@ -34,9 +35,11 @@
 
 char *osmo_apn_qualify(unsigned int mcc, unsigned int mnc, const char *ni)
 {
-	snprintf(apn_strbuf, sizeof(apn_strbuf)-1, APN_GPRS_FMT,
+	const size_t len = APN_MAXLEN+1;
+	char *buf = osmo_static_string(len);
+	snprintf(buf, len-1, APN_GPRS_FMT,
 		ni, mnc, mcc);
-	apn_strbuf[sizeof(apn_strbuf)-1] = '\0';
+	apn_strbuf[len-1] = '\0';
 
 	return apn_strbuf;
 }
diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index 54ec19c..eee6b2b 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -1581,8 +1581,7 @@
  */
 const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
 {
-	static char buf[64];
-	return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
+	return gsm0808_cell_id_name_buf(cid, OSMO_STATIC_STRING(64));
 }
 
 /*! Like gsm0808_cell_id_name() but uses a different static buffer.
@@ -1591,8 +1590,7 @@
  */
 const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
 {
-	static char buf[64];
-	return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
+	return gsm0808_cell_id_name(cid);
 }
 
 /*! Return a human readable representation of the Cell Identifier List, like
@@ -1640,8 +1638,9 @@
  * See also gsm0808_cell_id_list_name_buf(). */
 const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
 {
-	static char buf[1024];
-	gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
+	const size_t len = 1024;
+	char *buf = osmo_static_string(len);
+	gsm0808_cell_id_list_name_buf(buf, len, cil);
 	return buf;
 }
 
@@ -1650,8 +1649,9 @@
 
 const char *gsm0808_channel_type_name(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",
+	const size_t len = 128;
+	char *buf = osmo_static_string(len);
+	snprintf(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;
diff --git a/src/gsm/gsm23003.c b/src/gsm/gsm23003.c
index 95fca91..0f3fa2d 100644
--- a/src/gsm/gsm23003.c
+++ b/src/gsm/gsm23003.c
@@ -95,8 +95,9 @@
  */
 const char *osmo_mcc_name(uint16_t mcc)
 {
-	static char buf[8];
-	snprintf(buf, sizeof(buf), "%03u", mcc);
+	const size_t len = 8;
+	char *buf = osmo_static_string(len);
+	snprintf(buf, len, "%03u", mcc);
 	return buf;
 }
 
@@ -107,8 +108,9 @@
  */
 const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits)
 {
-	static char buf[8];
-	snprintf(buf, sizeof(buf), "%0*u", mnc_3_digits ? 3 : 2, mnc);
+	const size_t len = 8;
+	char *buf = osmo_static_string(len);
+	snprintf(buf, len, "%0*u", mnc_3_digits ? 3 : 2, mnc);
 	return buf;
 }
 
@@ -124,8 +126,9 @@
  */
 const char *osmo_plmn_name(const struct osmo_plmn_id *plmn)
 {
-	static char buf[16];
-	plmn_name(buf, sizeof(buf), plmn);
+	const size_t len = 16;
+	char *buf = osmo_static_string(len);
+	plmn_name(buf, len, plmn);
 	return buf;
 }
 
@@ -135,9 +138,7 @@
  */
 const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn)
 {
-	static char buf[16];
-	plmn_name(buf, sizeof(buf), plmn);
-	return buf;
+	return osmo_plmn_name(plmn);
 }
 
 /*! Return MCC-MNC-LAC as string, in a static buffer.
@@ -146,8 +147,9 @@
  */
 const char *osmo_lai_name(const struct osmo_location_area_id *lai)
 {
-	static char buf[32];
-	snprintf(buf, sizeof(buf), "%s-%u", osmo_plmn_name(&lai->plmn), lai->lac);
+	const size_t len = 32;
+	char *buf = osmo_static_string(len);
+	snprintf(buf, len, "%s-%u", osmo_plmn_name(&lai->plmn), lai->lac);
 	return buf;
 }
 
@@ -163,8 +165,9 @@
  */
 const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi)
 {
-	static char buf[32];
-	return _cgi_name(cgi, buf, sizeof(buf));
+	const size_t len = 32;
+	char *buf = osmo_static_string(len);
+	return _cgi_name(cgi, buf, len);
 }
 
 /*! Same as osmo_cgi_name(), but uses a different static buffer.
@@ -174,8 +177,7 @@
  */
 const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi)
 {
-	static char buf[32];
-	return _cgi_name(cgi, buf, sizeof(buf));
+	return osmo_cgi_name(cgi);
 }
 
 static void to_bcd(uint8_t *bcd, uint16_t val)
@@ -189,8 +191,9 @@
 
 const char *osmo_gummei_name(const struct osmo_gummei *gummei)
 {
-	static char buf[32];
-	snprintf(buf, sizeof(buf), "%s-%04x-%02x", osmo_plmn_name(&gummei->plmn),
+	const size_t len = 32;
+	char *buf = osmo_static_string(len);
+	snprintf(buf, len, "%s-%04x-%02x", osmo_plmn_name(&gummei->plmn),
 		 gummei->mme.group_id, gummei->mme.code);
 	return buf;
 }
diff --git a/src/gsm/gsm48.c b/src/gsm/gsm48.c
index 4166c15..d91c9ae 100644
--- a/src/gsm/gsm48.c
+++ b/src/gsm/gsm48.c
@@ -188,8 +188,9 @@
  */
 const char *osmo_rai_name(const struct gprs_ra_id *rai)
 {
-	static char buf[32];
-	snprintf(buf, sizeof(buf), "%s-%s-%u-%u",
+	const size_t len = 32;
+	char *buf = osmo_static_string(len);
+	snprintf(buf, len, "%s-%s-%u-%u",
 		 osmo_mcc_name(rai->mcc), osmo_mnc_name(rai->mnc, rai->mnc_3_digits), rai->lac,
 		 rai->rac);
 	return buf;
@@ -440,7 +441,8 @@
  */
 const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len)
 {
-	static char mi_name[10 + GSM48_MI_SIZE + 1];
+	const size_t mi_name_len = 10 + GSM48_MI_SIZE + 1;
+	char *mi_name = osmo_static_string(mi_name_len);
 	uint8_t mi_type;
 	uint32_t tmsi;
 	char mi_string[GSM48_MI_SIZE];
@@ -452,7 +454,7 @@
 		/* Table 10.5.4.3, reverse generate_mid_from_tmsi */
 		if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
 			tmsi = osmo_load32be(&mi[1]);
-			snprintf(mi_name, sizeof(mi_name), "TMSI-0x%08" PRIX32, tmsi);
+			snprintf(mi_name, mi_name_len, "TMSI-0x%08" PRIX32, tmsi);
 			return mi_name;
 		}
 		return "TMSI-invalid";
@@ -461,7 +463,7 @@
 	case GSM_MI_TYPE_IMEI:
 	case GSM_MI_TYPE_IMEISV:
 		osmo_bcd2str(mi_string, sizeof(mi_string), mi, 1, (mi_len * 2) - (mi[0] & GSM_MI_ODD ? 0 : 1), true);
-		snprintf(mi_name, sizeof(mi_name), "%s-%s", gsm48_mi_type_name(mi_type), mi_string);
+		snprintf(mi_name, mi_name_len, "%s-%s", gsm48_mi_type_name(mi_type), mi_string);
 		return mi_name;
 
 	default:
@@ -1059,7 +1061,8 @@
  */
 const char *gsm48_pdisc_msgtype_name(uint8_t pdisc, uint8_t msg_type)
 {
-	static char namebuf[64];
+	const size_t len = 64;
+	char *buf = osmo_static_string(len);
 	const struct value_string *msgt_names;
 
 	switch (pdisc) {
@@ -1083,9 +1086,9 @@
 	if (msgt_names)
 		return get_value_string(msgt_names, msg_type);
 
-	snprintf(namebuf, sizeof(namebuf), "%s:0x%02x",
+	snprintf(buf, len, "%s:0x%02x",
 		 gsm48_pdisc_name(pdisc), msg_type);
-	return namebuf;
+	return buf;
 }
 
 const struct value_string gsm48_reject_value_names[] = {
@@ -1189,7 +1192,8 @@
  */
 const char *osmo_gsm48_classmark_a5_name(const struct osmo_gsm48_classmark *cm)
 {
-	static char buf[128];
+	const size_t len = 128;
+	char *buf = osmo_static_string(len);
 	char cm1[42] = "no-cm1";
 	char cm2[42] = " no-cm2";
 	char cm3[42] = " no-cm2";
@@ -1212,7 +1216,7 @@
 			 cm->classmark3[0] & (1 << 2) ? " A5/6" : "",
 			 cm->classmark3[0] & (1 << 3) ? " A5/7" : "");
 
-	snprintf(buf, sizeof(buf), "%s%s%s", cm1, cm2, cm3);
+	snprintf(buf, len, "%s%s%s", cm1, cm2, cm3);
 	return buf;
 }
 
diff --git a/src/gsm/gsm_utils.c b/src/gsm/gsm_utils.c
index 02cb3d5..8ae63e4 100644
--- a/src/gsm/gsm_utils.c
+++ b/src/gsm/gsm_utils.c
@@ -883,11 +883,12 @@
 
 char *osmo_dump_gsmtime(const struct gsm_time *tm)
 {
-	static char buf[64];
+	const size_t len = 64;
+	char *buf = osmo_static_string(len);
 
-	snprintf(buf, sizeof(buf), "%06"PRIu32"/%02"PRIu16"/%02"PRIu8"/%02"PRIu8"/%02"PRIu8,
+	snprintf(buf, len, "%06"PRIu32"/%02"PRIu16"/%02"PRIu8"/%02"PRIu8"/%02"PRIu8,
 		 tm->fn, tm->t1, tm->t2, tm->t3, (uint8_t)tm->fn%52);
-	buf[sizeof(buf)-1] = '\0';
+	buf[len-1] = '\0';
 	return buf;
 }
 
diff --git a/src/gsm/rsl.c b/src/gsm/rsl.c
index e610ebf..47bd815 100644
--- a/src/gsm/rsl.c
+++ b/src/gsm/rsl.c
@@ -218,7 +218,7 @@
 /*! Get human-readable string for RSL channel number */
 const char *rsl_chan_nr_str(uint8_t chan_nr)
 {
-	static char str[20];
+	char *str = osmo_static_string(20);
 	int ts = chan_nr & 7;
 	uint8_t cbits = chan_nr >> 3;
 
diff --git a/src/msgb.c b/src/msgb.c
index 3902f6e..2372c64 100644
--- a/src/msgb.c
+++ b/src/msgb.c
@@ -398,7 +398,8 @@
  */
 const char *msgb_hexdump(const struct msgb *msg)
 {
-	static char buf[4100];
+	const size_t len = 4100;
+	char *buf = osmo_static_string(len);
 	int buf_offs = 0;
 	int nchars;
 	const unsigned char *start = msg->data;
@@ -421,32 +422,32 @@
 		if (lxhs[i] > msg->tail)
 			continue;
 		if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
-			nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
+			nchars = snprintf(buf + buf_offs, len - buf_offs,
 					  "(L%d=data%+" PRIdPTR ") ",
 					  i+1, lxhs[i] - msg->data);
 			buf_offs += nchars;
 			continue;
 		}
 		if (lxhs[i] < start) {
-			nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
+			nchars = snprintf(buf + buf_offs, len - buf_offs,
 					  "(L%d%+" PRIdPTR ") ", i+1,
 					  start - lxhs[i]);
 			buf_offs += nchars;
 			continue;
 		}
-		nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
+		nchars = snprintf(buf + buf_offs, len - buf_offs,
 				  "%s[L%d]> ",
 				  osmo_hexdump(start, lxhs[i] - start),
 				  i+1);
-		if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
+		if (nchars < 0 || nchars + buf_offs >= len)
 			return "ERROR";
 
 		buf_offs += nchars;
 		start = lxhs[i];
 	}
-	nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
+	nchars = snprintf(buf + buf_offs, len - buf_offs,
 			  "%s", osmo_hexdump(start, msg->tail - start));
-	if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
+	if (nchars < 0 || nchars + buf_offs >= len)
 		return "ERROR";
 
 	buf_offs += nchars;
@@ -456,17 +457,17 @@
 			continue;
 
 		if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
-			nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
+			nchars = snprintf(buf + buf_offs, len - buf_offs,
 					  "(L%d out of range) ", i+1);
 		} else if (lxhs[i] <= msg->data + msg->data_len &&
 			   lxhs[i] > msg->tail) {
-			nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
+			nchars = snprintf(buf + buf_offs, len - buf_offs,
 					  "(L%d=tail%+" PRIdPTR ") ",
 					  i+1, lxhs[i] - msg->tail);
 		} else
 			continue;
 
-		if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
+		if (nchars < 0 || nchars + buf_offs >= len)
 			return "ERROR";
 		buf_offs += nchars;
 	}
diff --git a/src/sim/core.c b/src/sim/core.c
index a78cecc..3476cf9 100644
--- a/src/sim/core.c
+++ b/src/sim/core.c
@@ -267,11 +267,10 @@
 	return msg;
 }
 
-/* FIXME: do we want to mark this as __thread? */
-static char sw_print_buf[256];
-
 char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in)
 {
+	const size_t len = 256;
+	char *buf = osmo_static_string(len);
 	const struct osim_card_sw *csw;
 
 	if (!ch || !ch->prof)
@@ -283,23 +282,23 @@
 
 	switch (csw->type) {
 	case SW_TYPE_STR:
-		snprintf(sw_print_buf, sizeof(sw_print_buf),
+		snprintf(buf, len,
 			 "%04x (%s)", sw_in, csw->u.str);
 		break;
 	default:
 		goto ret_def;
 	}
 
-	sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
+	buf[len-1] = '\0';
 
-	return sw_print_buf;
+	return buf;
 
 ret_def:
-	snprintf(sw_print_buf, sizeof(sw_print_buf),
+	snprintf(buf, len,
 		 "%04x (Unknown)", sw_in);
-	sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
+	buf[len-1] = '\0';
 
-	return sw_print_buf;
+	return buf;
 }
 
 
diff --git a/src/socket.c b/src/socket.c
index 37fd584..44b470d 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -822,8 +822,9 @@
  */
 const char *osmo_sock_get_name2(int fd)
 {
-	static char str[OSMO_SOCK_NAME_MAXLEN];
-	osmo_sock_get_name_buf(str, sizeof(str), fd);
+	const size_t len = OSMO_SOCK_NAME_MAXLEN;
+	char *str = osmo_static_string(len);
+	osmo_sock_get_name_buf(str, len, fd);
 	return str;
 }
 
diff --git a/src/utils.c b/src/utils.c
index 12189b6..ab386f7 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -41,8 +41,6 @@
  *
  * \file utils.c */
 
-static char namebuf[255];
-
 /*! get human-readable string for given value
  *  \param[in] vs Array of value_string tuples
  *  \param[in] val Value to be converted
@@ -54,12 +52,15 @@
  */
 const char *get_value_string(const struct value_string *vs, uint32_t val)
 {
+	char *namebuf;
+	const size_t len = 19;
 	const char *str = get_value_string_or_null(vs, val);
 	if (str)
 		return str;
 
-	snprintf(namebuf, sizeof(namebuf), "unknown 0x%"PRIx32, val);
-	namebuf[sizeof(namebuf) - 1] = '\0';
+	namebuf = osmo_static_string(len);
+	snprintf(namebuf, len, "unknown 0x%"PRIx32, val);
+	namebuf[len-1] = '\0';
 	return namebuf;
 }
 
@@ -219,7 +220,6 @@
 	return nibblepos >> 1;
 }
 
-static char hexd_buff[4096];
 static const char hex_chars[] = "0123456789abcdef";
 
 /*! Convert binary sequence to hexadecimal ASCII string.
@@ -279,11 +279,13 @@
  */
 char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
 {
+	const size_t buflen = OSMO_MIN(4096, len + 1);
+	char *buf = osmo_static_string(buflen);
 	int i;
 
-	if (len > sizeof(hexd_buff)-1)
-		len = sizeof(hexd_buff)-1;
-	memset(hexd_buff, 0, sizeof(hexd_buff));
+	if (len > buflen-1)
+		len = buflen - 1;
+	memset(buf, 0, buflen);
 
 	for (i = 0; i < len; i++) {
 		char outch;
@@ -301,10 +303,10 @@
 			outch = 'E';
 			break;
 		}
-		hexd_buff[i] = outch;
+		buf[i] = outch;
 	}
-	hexd_buff[sizeof(hexd_buff)-1] = 0;
-	return hexd_buff;
+	buf[buflen-1] = 0;
+	return buf;
 }
 
 /*! Convert binary sequence to hexadecimal ASCII string
@@ -320,8 +322,9 @@
  */
 char *osmo_hexdump(const unsigned char *buf, int len)
 {
-	osmo_hexdump_buf(hexd_buff, sizeof(hexd_buff), buf, len, " ", true);
-	return hexd_buff;
+	/* N bytes make 2N hex characters, plus N space delims, plus terminating nul */
+	const size_t buflen = OSMO_MIN(4096, len * (2 + 1) + 1);
+	return (char*)osmo_hexdump_buf(OSMO_STATIC_STRING(buflen), buf, len, " ", true);
 }
 
 /*! Convert binary sequence to hexadecimal ASCII string
@@ -337,8 +340,9 @@
  */
 char *osmo_hexdump_nospc(const unsigned char *buf, int len)
 {
-	osmo_hexdump_buf(hexd_buff, sizeof(hexd_buff), buf, len, "", true);
-	return hexd_buff;
+	/* N bytes make 2N hex characters, plus terminating nul */
+	const size_t buflen = OSMO_MIN(4096, len * 2 + 1);
+	return (char*)osmo_hexdump_buf(OSMO_STATIC_STRING(buflen), buf, len, "", true);
 }
 
 /* Compat with previous typo to preserve abi */
@@ -622,7 +626,7 @@
  */
 const char *osmo_escape_str(const char *str, int in_len)
 {
-	return osmo_escape_str_buf(str, in_len, namebuf, sizeof(namebuf));
+	return osmo_escape_str_buf(str, in_len, OSMO_STATIC_STRING(254));
 }
 
 /*! Like osmo_escape_str(), but returns double-quotes around a string, or "NULL" for a NULL string.
@@ -666,7 +670,7 @@
  */
 const char *osmo_quote_str(const char *str, int in_len)
 {
-	return osmo_quote_str_buf(str, in_len, namebuf, sizeof(namebuf));
+	return osmo_quote_str_buf(str, in_len, OSMO_STATIC_STRING(254));
 }
 
 /*! perform an integer square root operation on unsigned 32bit integer.
@@ -747,8 +751,9 @@
  */
 const char *osmo_str_tolower(const char *src)
 {
-	static char buf[128];
-	osmo_str_tolower_buf(buf, sizeof(buf), src);
+	const size_t len = 128;
+	char *buf = osmo_static_string(len);
+	osmo_str_tolower_buf(buf, len, src);
 	return buf;
 }
 
@@ -790,8 +795,9 @@
  */
 const char *osmo_str_toupper(const char *src)
 {
-	static char buf[128];
-	osmo_str_toupper_buf(buf, sizeof(buf), src);
+	const size_t len = 128;
+	char *buf = osmo_static_string(len);
+	osmo_str_toupper_buf(buf, len, src);
 	return buf;
 }
 

-- 
To view, visit https://gerrit.osmocom.org/13062
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id0eab72fa71630c8eb9edafaa370a41ca16329aa
Gerrit-Change-Number: 13062
Gerrit-PatchSet: 1
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190226/9ac1bd44/attachment.htm>


More information about the gerrit-log mailing list