diff --git a/openbsc/src/gprs/gprs_gmm.c b/openbsc/src/gprs/gprs_gmm.c index e3d5c7d64..88be5126e 100644 --- a/openbsc/src/gprs/gprs_gmm.c +++ b/openbsc/src/gprs/gprs_gmm.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -683,11 +684,10 @@ static void extract_subscr_msisdn(struct sgsn_mm_ctx *ctx) /* Prepend a '+' for international numbers */ if (called.plan == 1 && called.type == 1) { ctx->msisdn[0] = '+'; - strncpy(&ctx->msisdn[1], called.number, - sizeof(ctx->msisdn) - 1); + osmo_strlcpy(&ctx->msisdn[1], called.number, + sizeof(ctx->msisdn)); } else { - strncpy(&ctx->msisdn[0], called.number, - sizeof(ctx->msisdn) - 1); + osmo_strlcpy(ctx->msisdn, called.number, sizeof(ctx->msisdn)); } } @@ -725,7 +725,7 @@ static void extract_subscr_hlr(struct sgsn_mm_ctx *ctx) return; } - strncpy(&ctx->hlr[0], called.number, sizeof(ctx->hlr) - 1); + osmo_strlcpy(ctx->hlr, called.number, sizeof(ctx->hlr)); } #ifdef BUILD_IU @@ -1034,10 +1034,10 @@ static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg) mm_ctx_cleanup_free(ictx, "GPRS IMSI re-use"); } } - strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi) - 1); + osmo_strlcpy(ctx->imsi, mi_string, sizeof(ctx->imsi)); break; case GSM_MI_TYPE_IMEI: - strncpy(ctx->imei, mi_string, sizeof(ctx->imei) - 1); + osmo_strlcpy(ctx->imei, mi_string, sizeof(ctx->imei)); break; case GSM_MI_TYPE_IMEISV: break; @@ -1138,7 +1138,7 @@ static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *ctx, struct msgb *msg, reject_cause = GMM_CAUSE_NET_FAIL; goto rejected; } - strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi) - 1); + osmo_strlcpy(ctx->imsi, mi_string, sizeof(ctx->imsi)); #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 29e21ced4..d3e2ea748 100644 --- a/openbsc/src/gprs/gprs_subscriber.c +++ b/openbsc/src/gprs/gprs_subscriber.c @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -164,7 +165,8 @@ static int gprs_subscr_tx_gsup_message(struct gsm_subscriber *subscr, 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); + osmo_strlcpy(gsup_msg->imsi, subscr->imsi, + sizeof(gsup_msg->imsi)); gsup_msg->cn_domain = OSMO_GSUP_CN_DOMAIN_PS; osmo_gsup_encode(msg, gsup_msg); @@ -185,7 +187,8 @@ static int gprs_subscr_tx_gsup_error_reply(struct gsm_subscriber *subscr, { struct osmo_gsup_message gsup_reply = {0}; - strncpy(gsup_reply.imsi, gsup_orig->imsi, sizeof(gsup_reply.imsi) - 1); + osmo_strlcpy(gsup_reply.imsi, gsup_orig->imsi, + sizeof(gsup_reply.imsi)); gsup_reply.cause = cause; gsup_reply.message_type = OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type); @@ -778,9 +781,8 @@ struct gsm_subscriber *gprs_subscr_get_or_create_by_mmctx(struct sgsn_mm_ctx *mm subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE; } - strncpy(subscr->equipment.imei, mmctx->imei, - sizeof(subscr->equipment.imei)-1); - subscr->equipment.imei[sizeof(subscr->equipment.imei)-1] = 0; + osmo_strlcpy(subscr->equipment.imei, mmctx->imei, + sizeof(subscr->equipment.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 a0af42bee..83e17dbd4 100644 --- a/openbsc/src/gprs/gtphub.c +++ b/openbsc/src/gprs/gtphub.c @@ -2360,8 +2360,7 @@ void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str, 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'; + osmo_strlcpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->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 667013b8b..afeeda657 100644 --- a/openbsc/src/gprs/gtphub_ares.c +++ b/openbsc/src/gprs/gtphub_ares.c @@ -121,8 +121,8 @@ static void make_addr_str(struct ggsn_lookup *lookup) 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'; + osmo_strlcpy(lookup->apn_oi_str, apn_oi_str, + sizeof(lookup->apn_oi_str)); } static int start_ares_query(struct ggsn_lookup *lookup) @@ -170,11 +170,9 @@ struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub, 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'; + osmo_strlcpy(lookup->imsi_str, imsi_str, sizeof(lookup->imsi_str)); + osmo_strlcpy(lookup->apn_ni_str, apn_ni_str, + sizeof(lookup->apn_ni_str)); make_addr_str(lookup); diff --git a/openbsc/src/gprs/sgsn_auth.c b/openbsc/src/gprs/sgsn_auth.c index 4b69a0d10..1fa7fc4ae 100644 --- a/openbsc/src/gprs/sgsn_auth.c +++ b/openbsc/src/gprs/sgsn_auth.c @@ -20,6 +20,7 @@ */ #include +#include #include #include #include @@ -62,7 +63,7 @@ int sgsn_acl_add(const char *imsi, struct sgsn_config *cfg) acl = talloc_zero(NULL, struct imsi_acl_entry); if (!acl) return -ENOMEM; - strncpy(acl->imsi, imsi, sizeof(acl->imsi) - 1); + osmo_strlcpy(acl->imsi, imsi, sizeof(acl->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 0c723e83d..269822516 100644 --- a/openbsc/src/libbsc/abis_nm.c +++ b/openbsc/src/libbsc/abis_nm.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -2235,8 +2236,7 @@ static int bs11_read_swl_file(struct abis_nm_bs11_sw *bs11_sw) } /* construct new filename */ - strncpy(dir, bs11_sw->swl_fname, sizeof(dir)); - dir[sizeof(dir)-1] = '\0'; + osmo_strlcpy(dir, bs11_sw->swl_fname, sizeof(dir)); 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 @@ int abis_nm_bs11_load_swl(struct gsm_bts *bts, const char *fname, 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'; + osmo_strlcpy(bs11_sw->swl_fname, fname, sizeof(bs11_sw->swl_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 0538482a1..59fac3503 100644 --- a/openbsc/src/libbsc/bsc_rf_ctrl.c +++ b/openbsc/src/libbsc/bsc_rf_ctrl.c @@ -451,8 +451,7 @@ static int rf_create_socket(struct osmo_bsc_rf *rf, const char *path) } local.sun_family = AF_UNIX; - strncpy(local.sun_path, path, sizeof(local.sun_path)); - local.sun_path[sizeof(local.sun_path) - 1] = '\0'; + osmo_strlcpy(local.sun_path, path, sizeof(local.sun_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 1f98cc66c..1ecdee5a5 100644 --- a/openbsc/src/libcommon/gsm_subscriber_base.c +++ b/openbsc/src/libcommon/gsm_subscriber_base.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -115,7 +116,7 @@ struct gsm_subscriber *subscr_get_or_create(struct gsm_subscriber_group *sgrp, if (!subscr) return NULL; - strncpy(subscr->imsi, imsi, sizeof(subscr->imsi)-1); + osmo_strlcpy(subscr->imsi, imsi, sizeof(subscr->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 861212a64..8be4e7aea 100644 --- a/openbsc/src/libcommon/gsup_test_client.c +++ b/openbsc/src/libcommon/gsup_test_client.c @@ -75,8 +75,7 @@ static struct imsi_op *imsi_op_alloc(void *ctx, const char *imsi, return NULL; io = talloc_zero(ctx, struct imsi_op); - strncpy(io->imsi, imsi, sizeof(io->imsi)); - io->imsi[sizeof(io->imsi)-1] = '\0'; + osmo_strlcpy(io->imsi, imsi, sizeof(io->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 79e136d52..d153247b0 100644 --- a/openbsc/src/libmsc/ctrl_commands.c +++ b/openbsc/src/libmsc/ctrl_commands.c @@ -20,6 +20,7 @@ */ #include +#include #include #include #include @@ -103,8 +104,7 @@ static int set_subscriber_modify(struct ctrl_cmd *cmd, void *data) goto fail; subscr->authorized = 1; - strncpy(subscr->extension, msisdn, GSM_EXTENSION_LENGTH - 1); - subscr->extension[GSM_EXTENSION_LENGTH-1] = '\0'; + osmo_strlcpy(subscr->extension, msisdn, sizeof(subscr->extension)); /* 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 5cccb3280..68dac766c 100644 --- a/openbsc/src/libmsc/db.c +++ b/openbsc/src/libmsc/db.c @@ -39,6 +39,7 @@ #include #include #include +#include #include @@ -240,7 +241,7 @@ static struct gsm_sms *sms_from_result_v3(dbi_result result) 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); + osmo_strlcpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)); subscr_direct_free(sender); sender = NULL; @@ -252,10 +253,8 @@ static struct gsm_sms *sms_from_result_v3(dbi_result result) "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) + osmo_strlcpy(sms->dst.addr, daddr, sizeof(sms->dst.addr)); 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 @@ static struct gsm_sms *sms_from_result_v3(dbi_result result) 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) + osmo_strlcpy(sms->text, text, sizeof(sms->text)); return sms; } @@ -549,7 +546,7 @@ struct gsm_subscriber *db_create_subscriber(const char *imsi, uint64_t smin, return NULL; } subscr->id = dbi_conn_sequence_last(conn, NULL); - strncpy(subscr->imsi, imsi, sizeof(subscr->imsi)-1); + osmo_strlcpy(subscr->imsi, imsi, sizeof(subscr->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 @@ static int get_equipment_by_subscr(struct gsm_subscriber *subscr) string = dbi_result_get_string(result, "imei"); if (string) - strncpy(equip->imei, string, sizeof(equip->imei)-1); + osmo_strlcpy(equip->imei, string, sizeof(equip->imei)); string = dbi_result_get_string(result, "classmark1"); if (string) { @@ -824,21 +821,19 @@ static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result) const char *string; string = dbi_result_get_string(result, "imsi"); if (string) - strncpy(subscr->imsi, string, sizeof(subscr->imsi)-1); + osmo_strlcpy(subscr->imsi, string, sizeof(subscr->imsi)); 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) + osmo_strlcpy(subscr->name, string, sizeof(subscr->name)); string = dbi_result_get_string(result, "extension"); if (string) - strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH); + osmo_strlcpy(subscr->extension, string, sizeof(subscr->extension)); subscr->lac = dbi_result_get_ulonglong(result, "lac"); @@ -1351,8 +1346,7 @@ int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM230 unsigned long long equipment_id, watch_id; dbi_result result; - strncpy(subscriber->equipment.imei, imei, - sizeof(subscriber->equipment.imei)-1); + osmo_strlcpy(subscriber->equipment.imei, imei, sizeof(subscriber->equipment.imei)); result = dbi_conn_queryf(conn, "INSERT OR IGNORE INTO Equipment " @@ -1502,19 +1496,15 @@ static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result resul 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) + osmo_strlcpy(sms->dst.addr, daddr, sizeof(sms->dst.addr)); 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) + osmo_strlcpy(sms->src.addr, saddr, sizeof(sms->src.addr)); 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 @@ static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result resul 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) + osmo_strlcpy(sms->text, text, sizeof(sms->text)); return sms; } diff --git a/openbsc/src/libmsc/gsm_04_08.c b/openbsc/src/libmsc/gsm_04_08.c index 34492bbea..742f803d4 100644 --- a/openbsc/src/libmsc/gsm_04_08.c +++ b/openbsc/src/libmsc/gsm_04_08.c @@ -65,6 +65,7 @@ #include #include #include +#include #include #include @@ -1973,10 +1974,9 @@ static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg) /* 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); + osmo_strlcpy(setup.calling.number, trans->subscr->extension, + sizeof(setup.calling.number)); + osmo_strlcpy(setup.imsi, trans->subscr->imsi, sizeof(setup.imsi)); /* bearer capability */ if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { @@ -2153,8 +2153,8 @@ static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg) } /* IMSI of called subscriber */ - strncpy(call_conf.imsi, trans->subscr->imsi, - sizeof(call_conf.imsi)-1); + osmo_strlcpy(call_conf.imsi, trans->subscr->imsi, + sizeof(call_conf.imsi)); new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF); @@ -2307,10 +2307,9 @@ static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg) 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); + osmo_strlcpy(connect.connected.number, trans->subscr->extension, + sizeof(connect.connected.number)); + osmo_strlcpy(connect.imsi, trans->subscr->imsi, sizeof(connect.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 48a87e055..6164a0887 100644 --- a/openbsc/src/libmsc/gsm_04_11.c +++ b/openbsc/src/libmsc/gsm_04_11.c @@ -37,6 +37,7 @@ #include #include +#include #include #include @@ -92,15 +93,15 @@ struct gsm_sms *sms_from_text(struct gsm_subscriber *receiver, return NULL; sms->receiver = subscr_get(receiver); - strncpy(sms->text, text, sizeof(sms->text)-1); + osmo_strlcpy(sms->text, text, sizeof(sms->text)); - strncpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)-1); + osmo_strlcpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)); 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); + osmo_strlcpy(sms->dst.addr, receiver->extension, sizeof(sms->dst.addr)); /* Generate user_data */ sms->user_data_len = gsm_7bit_encode_n(sms->user_data, sizeof(sms->user_data), sms->text, NULL); @@ -464,7 +465,8 @@ static int gsm340_rx_tpdu(struct gsm_subscriber_connection *conn, struct msgb *m } } - strncpy(gsms->src.addr, conn->subscr->extension, sizeof(gsms->src.addr)-1); + osmo_strlcpy(gsms->src.addr, conn->subscr->extension, + sizeof(gsms->src.addr)); 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 cd2b64842..3ddcdc39c 100644 --- a/openbsc/src/libmsc/meas_feed.c +++ b/openbsc/src/libmsc/meas_feed.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -52,12 +53,9 @@ static int process_meas_rep(struct gsm_meas_rep *mr) 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'; + osmo_strlcpy(mfm->imsi, subscr->imsi, sizeof(mfm->imsi)); + osmo_strlcpy(mfm->name, subscr->name, sizeof(mfm->name)); + osmo_strlcpy(mfm->scenario, g_mfs.scenario, sizeof(mfm->scenario)); /* copy the entire measurement report */ memcpy(&mfm->mr, mr, sizeof(mfm->mr)); @@ -160,8 +158,7 @@ void meas_feed_cfg_get(char **host, uint16_t *port) 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'; + osmo_strlcpy(g_mfs.scenario, name, sizeof(g_mfs.scenario)); } const char *meas_feed_scenario_get(void) diff --git a/openbsc/src/libmsc/smpp_openbsc.c b/openbsc/src/libmsc/smpp_openbsc.c index 228e61dfe..657272ed9 100644 --- a/openbsc/src/libmsc/smpp_openbsc.c +++ b/openbsc/src/libmsc/smpp_openbsc.c @@ -133,12 +133,13 @@ static int submit_to_sms(struct gsm_sms **psms, struct gsm_network *net, 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); + osmo_strlcpy(sms->dst.addr, dest->extension, sizeof(sms->dst.addr)); /* 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); + osmo_strlcpy(sms->src.addr, (char *)submit->source_addr, + sizeof(sms->src.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 4f412b106..490d7946b 100644 --- a/openbsc/src/libmsc/vty_interface_layer3.c +++ b/openbsc/src/libmsc/vty_interface_layer3.c @@ -567,8 +567,7 @@ DEFUN(ena_subscr_name, return CMD_WARNING; } - strncpy(subscr->name, name, sizeof(subscr->name)); - subscr->name[sizeof(subscr->name)-1] = '\0'; + osmo_strlcpy(subscr->name, name, sizeof(subscr->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 ed1917512..6b38ee5fe 100644 --- a/openbsc/src/libtrau/rtp_proxy.c +++ b/openbsc/src/libtrau/rtp_proxy.c @@ -29,6 +29,7 @@ #include /* uname() */ #include +#include #include #include #include @@ -368,9 +369,9 @@ static int rtcp_mangle(struct msgb *msg, struct rtp_socket *rs) } 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'; + osmo_strlcpy(new_cname, + inet_ntoa(rss->sin_local.sin_addr), + sizeof(new_cname)); 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 58667fe8c..e7c387c22 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_rewrite.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_rewrite.c @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -129,15 +130,15 @@ static void update_called_number(struct gsm_mncc_number *called, { if (strncmp(chosen_number, "00", 2) == 0) { called->type = 1; - strncpy(called->number, chosen_number + 2, sizeof(called->number)); + osmo_strlcpy(called->number, chosen_number + 2, + sizeof(called->number)); } else { /* rewrite international to unknown */ if (called->type == 1) called->type = 0; - strncpy(called->number, chosen_number, sizeof(called->number)); + osmo_strlcpy(called->number, chosen_number, + sizeof(called->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 061a7a4ce..316d20318 100644 --- a/openbsc/src/utils/meas_vis.c +++ b/openbsc/src/utils/meas_vis.c @@ -73,7 +73,7 @@ static struct ms_state *find_alloc_ms(const char *imsi) ms = find_ms(imsi); if (!ms) { ms = talloc_zero(NULL, struct ms_state); - strncpy(ms->imsi, imsi, sizeof(ms->imsi)-1); + osmo_strlcpy(ms->imsi, imsi, sizeof(ms->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 +88,7 @@ static int handle_meas(struct msgb *msg) struct ms_state *ms = find_alloc_ms(mfm->imsi); time_t now = time(NULL); - strncpy(ms->name, mfm->name, sizeof(ms->imsi)-1); + osmo_strlcpy(ms->name, mfm->name, sizeof(ms->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 2b542ddf8..e7c27d2cb 100644 --- a/openbsc/tests/gtphub/gtphub_test.c +++ b/openbsc/tests/gtphub/gtphub_test.c @@ -437,21 +437,15 @@ struct gtphub_peer_port *__wrap_gtphub_resolve_ggsn_addr(struct gtphub *hub, " 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)"; + osmo_strlcpy(resolve_ggsn_got_imsi, imsi_str, + sizeof(resolve_ggsn_got_imsi)); - 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)"; + osmo_strlcpy(resolve_ggsn_got_ni, apn_ni_str, + sizeof(resolve_ggsn_got_ni)); return pp; } diff --git a/openbsc/tests/sgsn/sgsn_test.c b/openbsc/tests/sgsn/sgsn_test.c index fea635e9b..e1fd2e713 100644 --- a/openbsc/tests/sgsn/sgsn_test.c +++ b/openbsc/tests/sgsn/sgsn_test.c @@ -2248,7 +2248,7 @@ static void test_ggsn_selection(void) /* 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); + osmo_strlcpy(ctx->imsi, imsi1, sizeof(ctx->imsi)); /* Allocate and attach a subscriber */ s1 = gprs_subscr_get_or_create_by_mmctx(ctx); @@ -2273,7 +2273,7 @@ static void test_ggsn_selection(void) 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); + osmo_strlcpy(pdp_data->apn_str, "*", sizeof(pdp_data->apn_str)); /* Resolve GGSNs */ @@ -2326,7 +2326,7 @@ static void test_ggsn_selection(void) /* Add PDP data entry to subscriber */ - strncpy(pdp_data->apn_str, "Test.Apn", sizeof(pdp_data->apn_str)-1); + osmo_strlcpy(pdp_data->apn_str, "Test.Apn", sizeof(pdp_data->apn_str)); tp.lv[GSM48_IE_GSM_APN].len = gprs_str_to_apn(apn_enc, sizeof(apn_enc), "Test.Apn");