aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2019-02-26 19:35:41 +0100
committerNeels Hofmeyr <neels@hofmeyr.de>2019-02-26 21:54:39 +0100
commit4d35abc33e3b18282456ad5721fe2bf36433b946 (patch)
tree8d675af07eba0c86481e7bea5b2f5bb9e563a2e4
parentb75f5ae6df9919f0f55d05329810e4d0b6f06cce (diff)
use osmo_static_string() instead of numerous static buffersneels/static_strings
-rw-r--r--include/osmocom/gsm/gsm0808_utils.h3
-rw-r--r--include/osmocom/gsm/gsm23003.h7
-rw-r--r--src/fsm.c10
-rw-r--r--src/gb/gprs_ns.c9
-rw-r--r--src/gsm/abis_nm.c5
-rw-r--r--src/gsm/apn.c7
-rw-r--r--src/gsm/gsm0808_utils.c16
-rw-r--r--src/gsm/gsm23003.c37
-rw-r--r--src/gsm/gsm48.c24
-rw-r--r--src/gsm/gsm_utils.c7
-rw-r--r--src/gsm/rsl.c2
-rw-r--r--src/msgb.c21
-rw-r--r--src/sim/core.c17
-rw-r--r--src/socket.c5
-rw-r--r--src/utils.c48
15 files changed, 122 insertions, 96 deletions
diff --git a/include/osmocom/gsm/gsm0808_utils.h b/include/osmocom/gsm/gsm0808_utils.h
index e1e345d6..4ca355ab 100644
--- a/include/osmocom/gsm/gsm0808_utils.h
+++ b/include/osmocom/gsm/gsm0808_utils.h
@@ -73,7 +73,8 @@ static inline const char *gsm0808_cell_id_discr_name(enum CELL_IDENT id_discr)
{ 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 b34a677b..6f7b63af 100644
--- a/include/osmocom/gsm/gsm23003.h
+++ b/include/osmocom/gsm/gsm23003.h
@@ -4,6 +4,7 @@
#include <stdint.h>
#include <stdbool.h>
+#include <osmocom/core/defs.h>
/* 23.003 Chapter 12.1 */
struct osmo_plmn_id {
@@ -98,10 +99,12 @@ bool osmo_imei_str_valid(const char *imei, bool with_15th_digit);
const char *osmo_mcc_name(uint16_t mcc);
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);
+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);
+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 6e15ab70..e8c008e9 100644
--- a/src/fsm.c
+++ b/src/fsm.c
@@ -391,9 +391,10 @@ void osmo_fsm_inst_free(struct osmo_fsm_inst *fi)
*/
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_inst_name(struct osmo_fsm_inst *fi)
*/
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 54964d38..57067016 100644
--- a/src/gb/gprs_ns.c
+++ b/src/gb/gprs_ns.c
@@ -1203,15 +1203,16 @@ int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
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 @@ const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc)
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 49d05ba5..5b8df57c 100644
--- a/src/gsm/abis_nm.c
+++ b/src/gsm/abis_nm.c
@@ -930,8 +930,9 @@ enum gsm_phys_chan_config abis_nm_pchan4chcomb(uint8_t chcomb)
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 26746631..99030d43 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 @@ static char apn_strbuf[APN_MAXLEN+1];
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 54ec19c2..eee6b2b1 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -1581,8 +1581,7 @@ static const char *gsm0808_cell_id_name_buf(const struct gsm0808_cell_id *cid,
*/
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_name(const struct gsm0808_cell_id *cid)
*/
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 @@ int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808
* 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_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
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 720c09b6..ab70fb03 100644
--- a/src/gsm/gsm23003.c
+++ b/src/gsm/gsm23003.c
@@ -95,8 +95,9 @@ bool osmo_imei_str_valid(const char *imei, bool with_15th_digit)
*/
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_mcc_name(uint16_t mcc)
*/
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 @@ static inline void plmn_name(char *buf, size_t buflen, const struct osmo_plmn_id
*/
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_name(const struct osmo_plmn_id *plmn)
*/
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_plmn_name2(const struct osmo_plmn_id *plmn)
*/
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 @@ static const char *_cgi_name(const struct osmo_cell_global_id *cgi, char *buf, s
*/
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_name(const struct osmo_cell_global_id *cgi)
*/
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 @@ static void to_bcd(uint8_t *bcd, uint16_t val)
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 4166c153..d91c9ae6 100644
--- a/src/gsm/gsm48.c
+++ b/src/gsm/gsm48.c
@@ -188,8 +188,9 @@ const char *rr_cause_name(uint8_t cause)
*/
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 *gsm48_mi_type_name(uint8_t mi)
*/
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 @@ const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len)
/* 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 @@ const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len)
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 struct value_string gsm48_nc_ss_msgtype_names[] = {
*/
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 @@ const char *gsm48_pdisc_msgtype_name(uint8_t pdisc, uint8_t msg_type)
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 @@ bool osmo_gsm48_classmark_is_r99(const struct osmo_gsm48_classmark *cm)
*/
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 @@ const char *osmo_gsm48_classmark_a5_name(const struct osmo_gsm48_classmark *cm)
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 02cb3d55..8ae63e4c 100644
--- a/src/gsm/gsm_utils.c
+++ b/src/gsm/gsm_utils.c
@@ -883,11 +883,12 @@ uint32_t gsm_gsmtime2fn(struct gsm_time *time)
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 e610ebf7..47bd8158 100644
--- a/src/gsm/rsl.c
+++ b/src/gsm/rsl.c
@@ -218,7 +218,7 @@ int rsl_dec_chan_nr(uint8_t chan_nr, uint8_t *type, uint8_t *subch, uint8_t *tim
/*! 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 3902f6e8..2372c645 100644
--- a/src/msgb.c
+++ b/src/msgb.c
@@ -398,7 +398,8 @@ int msgb_resize_area(struct msgb *msg, uint8_t *area,
*/
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 @@ const char *msgb_hexdump(const struct msgb *msg)
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 @@ const char *msgb_hexdump(const struct msgb *msg)
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 a78cecc3..3476cf9d 100644
--- a/src/sim/core.c
+++ b/src/sim/core.c
@@ -267,11 +267,10 @@ struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1,
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 @@ char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in)
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 37fd5844..44b470d0 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -822,8 +822,9 @@ int osmo_sock_get_name_buf(char *str, size_t str_len, int fd)
*/
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 12189b6e..ab386f77 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 @@ static char namebuf[255];
*/
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 @@ int osmo_hexparse(const char *str, uint8_t *b, int max_len)
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 @@ const char *osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const unsigned
*/
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 @@ 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[buflen-1] = 0;
+ return buf;
}
/*! Convert binary sequence to hexadecimal ASCII string
@@ -320,8 +322,9 @@ char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
*/
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(const unsigned char *buf, int len)
*/
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 @@ done:
*/
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_buf(const char *str, int in_len, char *buf, size_t bu
*/
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 @@ size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src)
*/
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 @@ size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src)
*/
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;
}