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.orgReview at https://gerrit.osmocom.org/1592 cosmetic: use strncpy0s() or strncpy0() everywhere Shorten some code and make obvious to the reader that strncpy() is invoked in a safe way. Change-Id: I900726cf06d34128db22a3d3d911ee0d1423b1bd --- M openbsc/src/gprs/gprs_gmm.c M openbsc/src/gprs/gprs_subscriber.c M openbsc/src/gprs/gtphub.c M openbsc/src/gprs/gtphub_ares.c M openbsc/src/gprs/sgsn_auth.c M openbsc/src/libbsc/abis_nm.c M openbsc/src/libbsc/bsc_rf_ctrl.c M openbsc/src/libcommon/gsm_subscriber_base.c M openbsc/src/libcommon/gsup_test_client.c M openbsc/src/libmsc/ctrl_commands.c M openbsc/src/libmsc/db.c M openbsc/src/libmsc/gsm_04_08.c M openbsc/src/libmsc/gsm_04_11.c M openbsc/src/libmsc/meas_feed.c M openbsc/src/libmsc/smpp_openbsc.c M openbsc/src/libmsc/vty_interface_layer3.c M openbsc/src/libtrau/rtp_proxy.c M openbsc/src/osmo-bsc_nat/bsc_nat_rewrite.c M openbsc/src/utils/meas_vis.c M openbsc/tests/gtphub/gtphub_test.c M openbsc/tests/sgsn/sgsn_test.c 21 files changed, 87 insertions(+), 118 deletions(-) git pull ssh://gerrit.osmocom.org:29418/openbsc refs/changes/92/1592/1 diff --git a/openbsc/src/gprs/gprs_gmm.c b/openbsc/src/gprs/gprs_gmm.c index 363b457..6394a03 100644 --- a/openbsc/src/gprs/gprs_gmm.c +++ b/openbsc/src/gprs/gprs_gmm.c @@ -66,6 +66,7 @@ #include <openbsc/sgsn.h> #include <openbsc/signal.h> #include <openbsc/iu.h> +#include <openbsc/strncpy0.h> #include <pdp.h> @@ -681,11 +682,10 @@ /* Prepend a '+' for international numbers */ if (called.plan == 1 && called.type == 1) { ctx->msisdn[0] = '+'; - strncpy(&ctx->msisdn[1], called.number, + strncpy0(&ctx->msisdn[1], called.number, sizeof(ctx->msisdn) - 1); } else { - strncpy(&ctx->msisdn[0], called.number, - sizeof(ctx->msisdn) - 1); + strncpy0s(ctx->msisdn, called.number); } } @@ -723,7 +723,7 @@ return; } - strncpy(&ctx->hlr[0], called.number, sizeof(ctx->hlr) - 1); + strncpy0s(ctx->hlr, called.number); } #ifdef BUILD_IU @@ -1032,10 +1032,10 @@ mm_ctx_cleanup_free(ictx, "GPRS IMSI re-use"); } } - strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi) - 1); + strncpy0s(ctx->imsi, mi_string); break; case GSM_MI_TYPE_IMEI: - strncpy(ctx->imei, mi_string, sizeof(ctx->imei) - 1); + strncpy0s(ctx->imei, mi_string); break; case GSM_MI_TYPE_IMEISV: break; @@ -1136,7 +1136,7 @@ reject_cause = GMM_CAUSE_NET_FAIL; goto rejected; } - strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi) - 1); + strncpy0s(ctx->imsi, mi_string); #endif } if (ctx->ran_type == MM_CTX_T_GERAN_Gb) { diff --git a/openbsc/src/gprs/gprs_subscriber.c b/openbsc/src/gprs/gprs_subscriber.c index 23dbdd4..cdeebfa 100644 --- a/openbsc/src/gprs/gprs_subscriber.c +++ b/openbsc/src/gprs/gprs_subscriber.c @@ -24,6 +24,7 @@ #include <osmocom/gsm/gsup.h> #include <openbsc/gsm_subscriber.h> #include <openbsc/gsup_client.h> +#include <openbsc/strncpy0.h> #include <openbsc/sgsn.h> #include <openbsc/gprs_sgsn.h> @@ -164,7 +165,7 @@ struct msgb *msg = gsup_client_msgb_alloc(); if (strlen(gsup_msg->imsi) == 0 && subscr) - strncpy(gsup_msg->imsi, subscr->imsi, sizeof(gsup_msg->imsi) - 1); + strncpy0s(gsup_msg->imsi, subscr->imsi); gsup_msg->cn_domain = OSMO_GSUP_CN_DOMAIN_PS; osmo_gsup_encode(msg, gsup_msg); @@ -185,7 +186,7 @@ { struct osmo_gsup_message gsup_reply = {0}; - strncpy(gsup_reply.imsi, gsup_orig->imsi, sizeof(gsup_reply.imsi) - 1); + strncpy0s(gsup_reply.imsi, gsup_orig->imsi); gsup_reply.cause = cause; gsup_reply.message_type = OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type); @@ -778,11 +779,8 @@ subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE; } - if (strcpy(subscr->equipment.imei, mmctx->imei) != 0) { - strncpy(subscr->equipment.imei, mmctx->imei, - sizeof(subscr->equipment.imei)-1); - subscr->equipment.imei[sizeof(subscr->equipment.imei)-1] = 0; - } + if (strcpy(subscr->equipment.imei, mmctx->imei) != 0) + strncpy0s(subscr->equipment.imei, mmctx->imei); if (subscr->lac != mmctx->ra.lac) subscr->lac = mmctx->ra.lac; diff --git a/openbsc/src/gprs/gtphub.c b/openbsc/src/gprs/gtphub.c index a0af42b..3def95a 100644 --- a/openbsc/src/gprs/gtphub.c +++ b/openbsc/src/gprs/gtphub.c @@ -35,6 +35,7 @@ #include <openbsc/gtphub.h> #include <openbsc/debug.h> #include <openbsc/gprs_utils.h> +#include <openbsc/strncpy0.h> #include <osmocom/core/utils.h> #include <osmocom/core/logging.h> @@ -2360,8 +2361,7 @@ ggsn->peer = pp; gtphub_port_ref_count_inc(pp); - strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str)); - ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0'; + strncpy0s(ggsn->apn_oi_str, apn_oi_str); ggsn->expiry_entry.del_cb = resolved_gssn_del_cb; expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now); diff --git a/openbsc/src/gprs/gtphub_ares.c b/openbsc/src/gprs/gtphub_ares.c index 667013b..f74d307 100644 --- a/openbsc/src/gprs/gtphub_ares.c +++ b/openbsc/src/gprs/gtphub_ares.c @@ -30,6 +30,7 @@ #include <openbsc/gtphub.h> #include <openbsc/debug.h> +#include <openbsc/strncpy0.h> #include <osmocom/core/utils.h> #include <osmocom/gsm/apn.h> @@ -121,8 +122,7 @@ apn_oi_str = osmo_apn_qualify_from_imsi(lookup->imsi_str, lookup->apn_ni_str, lookup->have_3dig_mnc); - strncpy(lookup->apn_oi_str, apn_oi_str, sizeof(lookup->apn_oi_str)); - lookup->apn_oi_str[sizeof(lookup->apn_oi_str)-1] = '\0'; + strncpy0s(lookup->apn_oi_str, apn_oi_str); } static int start_ares_query(struct ggsn_lookup *lookup) @@ -170,11 +170,8 @@ expiring_item_init(&lookup->expiry_entry); lookup->hub = hub; - strncpy(lookup->imsi_str, imsi_str, sizeof(lookup->imsi_str)); - lookup->imsi_str[sizeof(lookup->imsi_str)-1] = '\0'; - - strncpy(lookup->apn_ni_str, apn_ni_str, sizeof(lookup->apn_ni_str)); - lookup->apn_ni_str[sizeof(lookup->apn_ni_str)-1] = '\0'; + strncpy0s(lookup->imsi_str, imsi_str); + strncpy0s(lookup->apn_ni_str, apn_ni_str); make_addr_str(lookup); diff --git a/openbsc/src/gprs/sgsn_auth.c b/openbsc/src/gprs/sgsn_auth.c index 4b69a0d..1b29641 100644 --- a/openbsc/src/gprs/sgsn_auth.c +++ b/openbsc/src/gprs/sgsn_auth.c @@ -25,6 +25,7 @@ #include <openbsc/gprs_gmm.h> #include <openbsc/gsm_subscriber.h> #include <openbsc/debug.h> +#include <openbsc/strncpy0.h> const struct value_string auth_state_names[] = { { SGSN_AUTH_ACCEPTED, "accepted"}, @@ -62,7 +63,7 @@ acl = talloc_zero(NULL, struct imsi_acl_entry); if (!acl) return -ENOMEM; - strncpy(acl->imsi, imsi, sizeof(acl->imsi) - 1); + strncpy0s(acl->imsi, imsi); llist_add(&acl->list, &cfg->imsi_acl); diff --git a/openbsc/src/libbsc/abis_nm.c b/openbsc/src/libbsc/abis_nm.c index 0c723e8..4e3c086 100644 --- a/openbsc/src/libbsc/abis_nm.c +++ b/openbsc/src/libbsc/abis_nm.c @@ -43,6 +43,7 @@ #include <openbsc/abis_nm.h> #include <openbsc/misdn.h> #include <openbsc/signal.h> +#include <openbsc/strncpy0.h> #include <osmocom/abis/e1_input.h> #define OM_ALLOC_SIZE 1024 @@ -2235,8 +2236,7 @@ } /* construct new filename */ - strncpy(dir, bs11_sw->swl_fname, sizeof(dir)); - dir[sizeof(dir)-1] = '\0'; + strncpy0s(dir, bs11_sw->swl_fname); strncat(fle->fname, dirname(dir), sizeof(fle->fname) - 1); strcat(fle->fname, "/"); strncat(fle->fname, file_id, sizeof(fle->fname) - 1 -strlen(fle->fname)); @@ -2307,8 +2307,7 @@ bs11_sw->user_cb = cbfn; bs11_sw->forced = forced; - strncpy(bs11_sw->swl_fname, fname, sizeof(bs11_sw->swl_fname)); - bs11_sw->swl_fname[sizeof(bs11_sw->swl_fname)-1] = '\0'; + strncpy0s(bs11_sw->swl_fname, fname); rc = bs11_read_swl_file(bs11_sw); if (rc < 0) return rc; diff --git a/openbsc/src/libbsc/bsc_rf_ctrl.c b/openbsc/src/libbsc/bsc_rf_ctrl.c index 0538482..e1e2060 100644 --- a/openbsc/src/libbsc/bsc_rf_ctrl.c +++ b/openbsc/src/libbsc/bsc_rf_ctrl.c @@ -26,6 +26,7 @@ #include <openbsc/signal.h> #include <openbsc/osmo_msc_data.h> #include <openbsc/ipaccess.h> +#include <openbsc/strncpy0.h> #include <osmocom/core/talloc.h> #include <osmocom/core/utils.h> @@ -451,8 +452,7 @@ } local.sun_family = AF_UNIX; - strncpy(local.sun_path, path, sizeof(local.sun_path)); - local.sun_path[sizeof(local.sun_path) - 1] = '\0'; + strncpy0s(local.sun_path, path); unlink(local.sun_path); /* we use the same magic that X11 uses in Xtranssock.c for diff --git a/openbsc/src/libcommon/gsm_subscriber_base.c b/openbsc/src/libcommon/gsm_subscriber_base.c index 1f98cc6..d063489 100644 --- a/openbsc/src/libcommon/gsm_subscriber_base.c +++ b/openbsc/src/libcommon/gsm_subscriber_base.c @@ -30,6 +30,7 @@ #include <osmocom/core/talloc.h> #include <openbsc/gsm_subscriber.h> #include <openbsc/debug.h> +#include <openbsc/strncpy0.h> LLIST_HEAD(active_subscribers); void *tall_subscr_ctx; @@ -115,7 +116,7 @@ if (!subscr) return NULL; - strncpy(subscr->imsi, imsi, sizeof(subscr->imsi)-1); + strncpy0s(subscr->imsi, imsi); subscr->group = sgrp; return subscr; } diff --git a/openbsc/src/libcommon/gsup_test_client.c b/openbsc/src/libcommon/gsup_test_client.c index 6af5fef..d72dca7 100644 --- a/openbsc/src/libcommon/gsup_test_client.c +++ b/openbsc/src/libcommon/gsup_test_client.c @@ -75,8 +75,7 @@ return NULL; io = talloc_zero(ctx, struct imsi_op); - strncpy(io->imsi, imsi, sizeof(io->imsi)); - io->imsi[sizeof(io->imsi)-1] = '\0'; + strncpy0s(io->imsi, imsi); io->type = type; io->timer.cb = imsi_op_timer_cb; io->timer.data = io; diff --git a/openbsc/src/libmsc/ctrl_commands.c b/openbsc/src/libmsc/ctrl_commands.c index 79e136d..7af8076 100644 --- a/openbsc/src/libmsc/ctrl_commands.c +++ b/openbsc/src/libmsc/ctrl_commands.c @@ -24,6 +24,7 @@ #include <openbsc/gsm_subscriber.h> #include <openbsc/db.h> #include <openbsc/debug.h> +#include <openbsc/strncpy0.h> #include <stdbool.h> @@ -103,8 +104,7 @@ goto fail; subscr->authorized = 1; - strncpy(subscr->extension, msisdn, GSM_EXTENSION_LENGTH - 1); - subscr->extension[GSM_EXTENSION_LENGTH-1] = '\0'; + strncpy0s(subscr->extension, msisdn); /* put it back to the db */ rc = db_sync_subscriber(subscr); diff --git a/openbsc/src/libmsc/db.c b/openbsc/src/libmsc/db.c index 5cccb32..d6290c3 100644 --- a/openbsc/src/libmsc/db.c +++ b/openbsc/src/libmsc/db.c @@ -34,6 +34,7 @@ #include <openbsc/gsm_04_11.h> #include <openbsc/db.h> #include <openbsc/debug.h> +#include <openbsc/strncpy0.h> #include <osmocom/gsm/protocol/gsm_23_003.h> #include <osmocom/core/talloc.h> @@ -240,7 +241,7 @@ snprintf(buf, sizeof(buf), "%llu", sender_id); sender = db_get_subscriber(GSM_SUBSCRIBER_ID, buf); OSMO_ASSERT(sender); - strncpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)-1); + strncpy0s(sms->src.addr, sender->extension); subscr_direct_free(sender); sender = NULL; @@ -252,10 +253,8 @@ "data_coding_scheme"); daddr = dbi_result_get_string(result, "dest_addr"); - if (daddr) { - strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr)); - sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0'; - } + if (daddr) + strncpy0s(sms->dst.addr, daddr); sms->user_data_len = dbi_result_get_field_length(result, "user_data"); user_data = dbi_result_get_binary(result, "user_data"); @@ -264,10 +263,8 @@ memcpy(sms->user_data, user_data, sms->user_data_len); text = dbi_result_get_string(result, "text"); - if (text) { - strncpy(sms->text, text, sizeof(sms->text)); - sms->text[sizeof(sms->text)-1] = '\0'; - } + if (text) + strncpy0s(sms->text, text); return sms; } @@ -549,7 +546,7 @@ return NULL; } subscr->id = dbi_conn_sequence_last(conn, NULL); - strncpy(subscr->imsi, imsi, sizeof(subscr->imsi)-1); + strncpy0s(subscr->imsi, imsi); dbi_result_free(result); LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi); if (alloc_exten) @@ -585,7 +582,7 @@ string = dbi_result_get_string(result, "imei"); if (string) - strncpy(equip->imei, string, sizeof(equip->imei)-1); + strncpy0s(equip->imei, string); string = dbi_result_get_string(result, "classmark1"); if (string) { @@ -824,21 +821,19 @@ const char *string; string = dbi_result_get_string(result, "imsi"); if (string) - strncpy(subscr->imsi, string, sizeof(subscr->imsi)-1); + strncpy0s(subscr->imsi, string); string = dbi_result_get_string(result, "tmsi"); if (string) subscr->tmsi = tmsi_from_string(string); string = dbi_result_get_string(result, "name"); - if (string) { - strncpy(subscr->name, string, GSM_NAME_LENGTH); - subscr->name[sizeof(subscr->name)-1] = '\0'; - } + if (string) + strncpy0s(subscr->name, string); string = dbi_result_get_string(result, "extension"); if (string) - strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH); + strncpy0s(subscr->extension, string); subscr->lac = dbi_result_get_ulonglong(result, "lac"); @@ -1351,8 +1346,7 @@ unsigned long long equipment_id, watch_id; dbi_result result; - strncpy(subscriber->equipment.imei, imei, - sizeof(subscriber->equipment.imei)-1); + strncpy0s(subscriber->equipment.imei, imei); result = dbi_conn_queryf(conn, "INSERT OR IGNORE INTO Equipment " @@ -1502,19 +1496,15 @@ sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi"); sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton"); daddr = dbi_result_get_string(result, "dest_addr"); - if (daddr) { - strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr)); - sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0'; - } + if (daddr) + strncpy0s(sms->dst.addr, daddr); sms->receiver = subscr_get_by_extension(net->subscr_group, sms->dst.addr); sms->src.npi = dbi_result_get_ulonglong(result, "src_npi"); sms->src.ton = dbi_result_get_ulonglong(result, "src_ton"); saddr = dbi_result_get_string(result, "src_addr"); - if (saddr) { - strncpy(sms->src.addr, saddr, sizeof(sms->src.addr)); - sms->src.addr[sizeof(sms->src.addr)-1] = '\0'; - } + if (saddr) + strncpy0s(sms->src.addr, saddr); sms->user_data_len = dbi_result_get_field_length(result, "user_data"); user_data = dbi_result_get_binary(result, "user_data"); @@ -1523,10 +1513,8 @@ memcpy(sms->user_data, user_data, sms->user_data_len); text = dbi_result_get_string(result, "text"); - if (text) { - strncpy(sms->text, text, sizeof(sms->text)); - sms->text[sizeof(sms->text)-1] = '\0'; - } + if (text) + strncpy0s(sms->text, text); return sms; } diff --git a/openbsc/src/libmsc/gsm_04_08.c b/openbsc/src/libmsc/gsm_04_08.c index 34492bb..37d148d 100644 --- a/openbsc/src/libmsc/gsm_04_08.c +++ b/openbsc/src/libmsc/gsm_04_08.c @@ -56,6 +56,7 @@ #include <openbsc/osmo_msc.h> #include <openbsc/handover.h> #include <openbsc/mncc_int.h> +#include <openbsc/strncpy0.h> #include <osmocom/abis/e1_input.h> #include <osmocom/core/bitvec.h> @@ -1973,10 +1974,8 @@ /* use subscriber as calling party number */ setup.fields |= MNCC_F_CALLING; - strncpy(setup.calling.number, trans->subscr->extension, - sizeof(setup.calling.number)-1); - strncpy(setup.imsi, trans->subscr->imsi, - sizeof(setup.imsi)-1); + strncpy0s(setup.calling.number, trans->subscr->extension); + strncpy0s(setup.imsi, trans->subscr->imsi); /* bearer capability */ if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { @@ -2153,8 +2152,7 @@ } /* IMSI of called subscriber */ - strncpy(call_conf.imsi, trans->subscr->imsi, - sizeof(call_conf.imsi)-1); + strncpy0s(call_conf.imsi, trans->subscr->imsi); new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF); @@ -2307,10 +2305,8 @@ tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0); /* use subscriber as connected party number */ connect.fields |= MNCC_F_CONNECTED; - strncpy(connect.connected.number, trans->subscr->extension, - sizeof(connect.connected.number)-1); - strncpy(connect.imsi, trans->subscr->imsi, - sizeof(connect.imsi)-1); + strncpy0s(connect.connected.number, trans->subscr->extension); + strncpy0s(connect.imsi, trans->subscr->imsi); /* facility */ if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { diff --git a/openbsc/src/libmsc/gsm_04_11.c b/openbsc/src/libmsc/gsm_04_11.c index 48a87e0..330249a 100644 --- a/openbsc/src/libmsc/gsm_04_11.c +++ b/openbsc/src/libmsc/gsm_04_11.c @@ -55,6 +55,7 @@ #include <openbsc/bsc_rll.h> #include <openbsc/chan_alloc.h> #include <openbsc/bsc_api.h> +#include <openbsc/strncpy0.h> #ifdef BUILD_SMPP #include "smpp_smsc.h" @@ -92,15 +93,15 @@ return NULL; sms->receiver = subscr_get(receiver); - strncpy(sms->text, text, sizeof(sms->text)-1); + strncpy0s(sms->text, text); - strncpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)-1); + strncpy0s(sms->src.addr, sender->extension); sms->reply_path_req = 0; sms->status_rep_req = 0; sms->ud_hdr_ind = 0; sms->protocol_id = 0; /* implicit */ sms->data_coding_scheme = dcs; - strncpy(sms->dst.addr, receiver->extension, sizeof(sms->dst.addr)-1); + strncpy0s(sms->dst.addr, receiver->extension); /* Generate user_data */ sms->user_data_len = gsm_7bit_encode_n(sms->user_data, sizeof(sms->user_data), sms->text, NULL); @@ -464,7 +465,7 @@ } } - strncpy(gsms->src.addr, conn->subscr->extension, sizeof(gsms->src.addr)-1); + strncpy0s(gsms->src.addr, conn->subscr->extension); LOGP(DLSMS, LOGL_INFO, "RX SMS: Sender: %s, MTI: 0x%02x, VPF: 0x%02x, " "MR: 0x%02x PID: 0x%02x, DCS: 0x%02x, DA: %s, " diff --git a/openbsc/src/libmsc/meas_feed.c b/openbsc/src/libmsc/meas_feed.c index cd2b648..05be23b 100644 --- a/openbsc/src/libmsc/meas_feed.c +++ b/openbsc/src/libmsc/meas_feed.c @@ -17,6 +17,7 @@ #include <openbsc/gsm_subscriber.h> #include <openbsc/meas_feed.h> #include <openbsc/vty.h> +#include <openbsc/strncpy0.h> #include "meas_feed.h" @@ -52,12 +53,9 @@ mfm->hdr.version = MEAS_FEED_VERSION; /* fill in MEAS_FEED_MEAS specific header */ - strncpy(mfm->imsi, subscr->imsi, sizeof(mfm->imsi)-1); - mfm->imsi[sizeof(mfm->imsi)-1] = '\0'; - strncpy(mfm->name, subscr->name, sizeof(mfm->name)-1); - mfm->name[sizeof(mfm->name)-1] = '\0'; - strncpy(mfm->scenario, g_mfs.scenario, sizeof(mfm->scenario)); - mfm->scenario[sizeof(mfm->scenario)-1] = '\0'; + strncpy0s(mfm->imsi, subscr->imsi); + strncpy0s(mfm->name, subscr->name); + strncpy0s(mfm->scenario, g_mfs.scenario); /* copy the entire measurement report */ memcpy(&mfm->mr, mr, sizeof(mfm->mr)); @@ -160,8 +158,7 @@ void meas_feed_scenario_set(const char *name) { - strncpy(g_mfs.scenario, name, sizeof(g_mfs.scenario)-1); - g_mfs.scenario[sizeof(g_mfs.scenario)-1] = '\0'; + strncpy0s(g_mfs.scenario, name); } const char *meas_feed_scenario_get(void) diff --git a/openbsc/src/libmsc/smpp_openbsc.c b/openbsc/src/libmsc/smpp_openbsc.c index 228e61d..1e1d91a 100644 --- a/openbsc/src/libmsc/smpp_openbsc.c +++ b/openbsc/src/libmsc/smpp_openbsc.c @@ -44,6 +44,7 @@ #include <openbsc/signal.h> #include <openbsc/transaction.h> #include <openbsc/gsm_subscriber.h> +#include <openbsc/strncpy0.h> #include "smpp_smsc.h" @@ -133,12 +134,12 @@ sms->receiver = dest; sms->dst.ton = submit->dest_addr_ton; sms->dst.npi = submit->dest_addr_npi; - strncpy(sms->dst.addr, dest->extension, sizeof(sms->dst.addr)-1); + strncpy0s(sms->dst.addr, dest->extension); /* fill in the source address */ sms->src.ton = submit->source_addr_ton; sms->src.npi = submit->source_addr_npi; - strncpy(sms->src.addr, (char *)submit->source_addr, sizeof(sms->src.addr)-1); + strncpy0s(sms->src.addr, (char *)submit->source_addr); if (submit->esm_class & 0x40) sms->ud_hdr_ind = 1; diff --git a/openbsc/src/libmsc/vty_interface_layer3.c b/openbsc/src/libmsc/vty_interface_layer3.c index a02f70c..97375a6 100644 --- a/openbsc/src/libmsc/vty_interface_layer3.c +++ b/openbsc/src/libmsc/vty_interface_layer3.c @@ -568,8 +568,7 @@ return CMD_WARNING; } - strncpy(subscr->name, name, sizeof(subscr->name)); - subscr->name[sizeof(subscr->name)-1] = '\0'; + strncpy0s(subscr->name, name); talloc_free(name); db_sync_subscriber(subscr); diff --git a/openbsc/src/libtrau/rtp_proxy.c b/openbsc/src/libtrau/rtp_proxy.c index ed19175..bc8812f 100644 --- a/openbsc/src/libtrau/rtp_proxy.c +++ b/openbsc/src/libtrau/rtp_proxy.c @@ -36,6 +36,7 @@ #include <openbsc/rtp_proxy.h> #include <openbsc/mncc.h> #include <openbsc/trau_upqueue.h> +#include <openbsc/strncpy0.h> #include <osmocom/netif/rtp.h> @@ -368,9 +369,7 @@ } if (rtph->type == RTCP_TYPE_SDES) { char new_cname[255]; - strncpy(new_cname, inet_ntoa(rss->sin_local.sin_addr), - sizeof(new_cname)); - new_cname[sizeof(new_cname)-1] = '\0'; + strncpy0s(new_cname, inet_ntoa(rss->sin_local.sin_addr)); rc = rtcp_sdes_cname_mangle(msg, rtph, &old_len, new_cname); if (rc < 0) diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_rewrite.c b/openbsc/src/osmo-bsc_nat/bsc_nat_rewrite.c index 58667fe..698059f 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_rewrite.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_rewrite.c @@ -28,6 +28,7 @@ #include <openbsc/debug.h> #include <openbsc/ipaccess.h> #include <openbsc/nat_rewrite_trie.h> +#include <openbsc/strncpy0.h> #include <osmocom/core/linuxlist.h> #include <osmocom/core/talloc.h> @@ -129,15 +130,13 @@ { if (strncmp(chosen_number, "00", 2) == 0) { called->type = 1; - strncpy(called->number, chosen_number + 2, sizeof(called->number)); + strncpy0s(called->number, chosen_number + 2); } else { /* rewrite international to unknown */ if (called->type == 1) called->type = 0; - strncpy(called->number, chosen_number, sizeof(called->number)); + strncpy0s(called->number, chosen_number); } - - called->number[sizeof(called->number) - 1] = '\0'; } /** diff --git a/openbsc/src/utils/meas_vis.c b/openbsc/src/utils/meas_vis.c index 061a7a4..8460a66 100644 --- a/openbsc/src/utils/meas_vis.c +++ b/openbsc/src/utils/meas_vis.c @@ -17,6 +17,7 @@ #include <osmocom/gsm/gsm_utils.h> #include <openbsc/meas_feed.h> +#include <openbsc/strncpy0.h> struct ms_state_uni { CDKSLIDER *cdk; @@ -73,7 +74,7 @@ ms = find_ms(imsi); if (!ms) { ms = talloc_zero(NULL, struct ms_state); - strncpy(ms->imsi, imsi, sizeof(ms->imsi)-1); + strncpy0s(ms->imsi, imsi); ms->ul._lbl[0] = ms->ul.label; ms->dl._lbl[0] = ms->dl.label; llist_add_tail(&ms->list, &g_st.ms_list); @@ -88,7 +89,7 @@ struct ms_state *ms = find_alloc_ms(mfm->imsi); time_t now = time(NULL); - strncpy(ms->name, mfm->name, sizeof(ms->imsi)-1); + strncpy0s(ms->name, mfm->name); memcpy(&ms->mr, &mfm->mr, sizeof(ms->mr)); ms->ul.last_update = now; if (ms->mr.flags & MEAS_REP_F_DL_VALID) diff --git a/openbsc/tests/gtphub/gtphub_test.c b/openbsc/tests/gtphub/gtphub_test.c index 7d7673b..49ca73c 100644 --- a/openbsc/tests/gtphub/gtphub_test.c +++ b/openbsc/tests/gtphub/gtphub_test.c @@ -29,6 +29,7 @@ #include <osmocom/core/application.h> #include <openbsc/debug.h> +#include <openbsc/strncpy0.h> #include <openbsc/gtphub.h> #include <gtp.h> @@ -449,21 +450,13 @@ " returning GGSN addr from imsi %s ni %s: %s\n", imsi_str, apn_ni_str, gtphub_port_str(pp)); - if (imsi_str) { - strncpy(resolve_ggsn_got_imsi, imsi_str, - sizeof(resolve_ggsn_got_imsi)); - resolve_ggsn_got_imsi[sizeof(resolve_ggsn_got_imsi) - 1] = '\0'; - } - else - strcpy(resolve_ggsn_got_imsi, "(null)"); + if (!imsi_str) + imsi_str = "(null)"; + strncpy0s(resolve_ggsn_got_imsi, imsi_str); - if (apn_ni_str) { - strncpy(resolve_ggsn_got_ni, apn_ni_str, - sizeof(resolve_ggsn_got_ni)); - resolve_ggsn_got_ni[sizeof(resolve_ggsn_got_ni) - 1] = '\0'; - } - else - strcpy(resolve_ggsn_got_ni, "(null)"); + if (!apn_ni_str) + apn_ni_str = "(null)"; + strncpy0s(resolve_ggsn_got_ni, apn_ni_str); return pp; } diff --git a/openbsc/tests/sgsn/sgsn_test.c b/openbsc/tests/sgsn/sgsn_test.c index a12aaf8..051b273 100644 --- a/openbsc/tests/sgsn/sgsn_test.c +++ b/openbsc/tests/sgsn/sgsn_test.c @@ -2248,7 +2248,7 @@ /* Create a context */ OSMO_ASSERT(count(gprs_llme_list()) == 0); ctx = alloc_mm_ctx(local_tlli, &raid); - strncpy(ctx->imsi, imsi1, sizeof(ctx->imsi) - 1); + strncpy0s(ctx->imsi, imsi1); /* Allocate and attach a subscriber */ s1 = gprs_subscr_get_or_create_by_mmctx(ctx); @@ -2273,7 +2273,7 @@ pdp_data = sgsn_subscriber_pdp_data_alloc(s1->sgsn_data); pdp_data->context_id = 1; pdp_data->pdp_type = 0x0121; - strncpy(pdp_data->apn_str, "*", sizeof(pdp_data->apn_str)-1); + strncpy0s(pdp_data->apn_str, "*"); /* Resolve GGSNs */ @@ -2326,7 +2326,7 @@ /* Add PDP data entry to subscriber */ - strncpy(pdp_data->apn_str, "Test.Apn", sizeof(pdp_data->apn_str)-1); + strncpy0s(pdp_data->apn_str, "Test.Apn"); tp.lv[GSM48_IE_GSM_APN].len = gprs_str_to_apn(apn_enc, sizeof(apn_enc), "Test.Apn"); -- To view, visit https://gerrit.osmocom.org/1592 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I900726cf06d34128db22a3d3d911ee0d1423b1bd Gerrit-PatchSet: 1 Gerrit-Project: openbsc Gerrit-Branch: master Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>