[PATCH] openbsc[master]: cosmetic: use osmo_strscpy() or osmo_strncpy() everywhere

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
Mon Jan 16 12:22:19 UTC 2017


Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/1592

to look at the new patch set (#3).

cosmetic: use osmo_strscpy() or osmo_strncpy() everywhere

Shorten some code and make obvious to the reader that strncpy() is invoked in a
safe way.

Depends on libosmocore Change-Id Ie973fc51835066540eba7ed2946ddf33a268b022

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, 81 insertions(+), 117 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/openbsc refs/changes/92/1592/3

diff --git a/openbsc/src/gprs/gprs_gmm.c b/openbsc/src/gprs/gprs_gmm.c
index e3d5c7d..bd87c41 100644
--- a/openbsc/src/gprs/gprs_gmm.c
+++ b/openbsc/src/gprs/gprs_gmm.c
@@ -42,6 +42,7 @@
 #include <osmocom/core/signal.h>
 #include <osmocom/core/talloc.h>
 #include <osmocom/core/rate_ctr.h>
+#include <osmocom/core/utils.h>
 #include <osmocom/crypt/auth.h>
 #include <osmocom/gsm/apn.h>
 #include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
@@ -683,11 +684,10 @@
 	/* 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_strncpy(&ctx->msisdn[1], called.number,
+			     sizeof(ctx->msisdn) - 1);
 	} else {
-		strncpy(&ctx->msisdn[0], called.number,
-			sizeof(ctx->msisdn) - 1);
+		osmo_strscpy(ctx->msisdn, called.number);
 	}
 }
 
@@ -725,7 +725,7 @@
 		return;
 	}
 
-	strncpy(&ctx->hlr[0], called.number, sizeof(ctx->hlr) - 1);
+	osmo_strscpy(ctx->hlr, called.number);
 }
 
 #ifdef BUILD_IU
@@ -1034,10 +1034,10 @@
 				mm_ctx_cleanup_free(ictx, "GPRS IMSI re-use");
 			}
 		}
-		strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi) - 1);
+		osmo_strscpy(ctx->imsi, mi_string);
 		break;
 	case GSM_MI_TYPE_IMEI:
-		strncpy(ctx->imei, mi_string, sizeof(ctx->imei) - 1);
+		osmo_strscpy(ctx->imei, mi_string);
 		break;
 	case GSM_MI_TYPE_IMEISV:
 		break;
@@ -1138,7 +1138,7 @@
 				reject_cause = GMM_CAUSE_NET_FAIL;
 				goto rejected;
 			}
-			strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi) - 1);
+			osmo_strscpy(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 29e21ce..491e712 100644
--- a/openbsc/src/gprs/gprs_subscriber.c
+++ b/openbsc/src/gprs/gprs_subscriber.c
@@ -22,6 +22,7 @@
 
 #include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
 #include <osmocom/gsm/gsup.h>
+#include <osmocom/core/utils.h>
 #include <openbsc/gsm_subscriber.h>
 #include <openbsc/gsup_client.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);
+		osmo_strscpy(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);
+	osmo_strscpy(gsup_reply.imsi, gsup_orig->imsi);
 	gsup_reply.cause = cause;
 	gsup_reply.message_type =
 		OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
@@ -778,9 +779,7 @@
 		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_strscpy(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..e3ff5d8 100644
--- a/openbsc/src/gprs/gtphub.c
+++ b/openbsc/src/gprs/gtphub.c
@@ -2360,8 +2360,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';
+	osmo_strscpy(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..1df2ffb 100644
--- a/openbsc/src/gprs/gtphub_ares.c
+++ b/openbsc/src/gprs/gtphub_ares.c
@@ -121,8 +121,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';
+	osmo_strscpy(lookup->apn_oi_str, apn_oi_str);
 }
 
 static int start_ares_query(struct ggsn_lookup *lookup)
@@ -170,11 +169,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';
+	osmo_strscpy(lookup->imsi_str, imsi_str);
+	osmo_strscpy(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..e413e3e 100644
--- a/openbsc/src/gprs/sgsn_auth.c
+++ b/openbsc/src/gprs/sgsn_auth.c
@@ -20,6 +20,7 @@
  */
 
 #include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
+#include <osmocom/core/utils.h>
 #include <openbsc/sgsn.h>
 #include <openbsc/gprs_sgsn.h>
 #include <openbsc/gprs_gmm.h>
@@ -62,7 +63,7 @@
 	acl = talloc_zero(NULL, struct imsi_acl_entry);
 	if (!acl)
 		return -ENOMEM;
-	strncpy(acl->imsi, imsi, sizeof(acl->imsi) - 1);
+	osmo_strscpy(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..e49f052 100644
--- a/openbsc/src/libbsc/abis_nm.c
+++ b/openbsc/src/libbsc/abis_nm.c
@@ -40,6 +40,7 @@
 #include <osmocom/gsm/tlv.h>
 #include <osmocom/gsm/abis_nm.h>
 #include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
 #include <openbsc/abis_nm.h>
 #include <openbsc/misdn.h>
 #include <openbsc/signal.h>
@@ -2235,8 +2236,7 @@
 		}
 
 		/* construct new filename */
-		strncpy(dir, bs11_sw->swl_fname, sizeof(dir));
-		dir[sizeof(dir)-1] = '\0';
+		osmo_strscpy(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';
+	osmo_strscpy(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..76c0ebc 100644
--- a/openbsc/src/libbsc/bsc_rf_ctrl.c
+++ b/openbsc/src/libbsc/bsc_rf_ctrl.c
@@ -451,8 +451,7 @@
 	}
 
 	local.sun_family = AF_UNIX;
-	strncpy(local.sun_path, path, sizeof(local.sun_path));
-	local.sun_path[sizeof(local.sun_path) - 1] = '\0';
+	osmo_strscpy(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..e838acd 100644
--- a/openbsc/src/libcommon/gsm_subscriber_base.c
+++ b/openbsc/src/libcommon/gsm_subscriber_base.c
@@ -28,6 +28,7 @@
 #include <assert.h>
 
 #include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
 #include <openbsc/gsm_subscriber.h>
 #include <openbsc/debug.h>
 
@@ -115,7 +116,7 @@
 	if (!subscr)
 		return NULL;
 
-	strncpy(subscr->imsi, imsi, sizeof(subscr->imsi)-1);
+	osmo_strscpy(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 17de4f0..1e7fca4 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';
+	osmo_strscpy(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..87bf58e 100644
--- a/openbsc/src/libmsc/ctrl_commands.c
+++ b/openbsc/src/libmsc/ctrl_commands.c
@@ -20,6 +20,7 @@
  */
 
 #include <osmocom/ctrl/control_cmd.h>
+#include <osmocom/core/utils.h>
 #include <openbsc/gsm_data.h>
 #include <openbsc/gsm_subscriber.h>
 #include <openbsc/db.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';
+	osmo_strscpy(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..633d071 100644
--- a/openbsc/src/libmsc/db.c
+++ b/openbsc/src/libmsc/db.c
@@ -39,6 +39,7 @@
 #include <osmocom/core/talloc.h>
 #include <osmocom/core/statistics.h>
 #include <osmocom/core/rate_ctr.h>
+#include <osmocom/core/utils.h>
 
 #include <openssl/rand.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);
+	osmo_strscpy(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)
+		osmo_strscpy(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)
+		osmo_strscpy(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);
+	osmo_strscpy(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);
+		osmo_strscpy(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);
+		osmo_strscpy(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)
+		osmo_strscpy(subscr->name, string);
 
 	string = dbi_result_get_string(result, "extension");
 	if (string)
-		strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
+		osmo_strscpy(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);
+	osmo_strscpy(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)
+		osmo_strscpy(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)
+		osmo_strscpy(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)
+		osmo_strscpy(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..1d13440 100644
--- a/openbsc/src/libmsc/gsm_04_08.c
+++ b/openbsc/src/libmsc/gsm_04_08.c
@@ -65,6 +65,7 @@
 #include <osmocom/gsm/protocol/gsm_04_08.h>
 #include <osmocom/core/msgb.h>
 #include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
 #include <osmocom/gsm/tlv.h>
 
 #include <assert.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);
+	osmo_strscpy(setup.calling.number, trans->subscr->extension);
+	osmo_strscpy(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);
+	osmo_strscpy(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);
+	osmo_strscpy(connect.connected.number, trans->subscr->extension);
+	osmo_strscpy(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..40d4487 100644
--- a/openbsc/src/libmsc/gsm_04_11.c
+++ b/openbsc/src/libmsc/gsm_04_11.c
@@ -37,6 +37,7 @@
 
 #include <osmocom/core/msgb.h>
 #include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
 
 #include <osmocom/gsm/gsm_utils.h>
 #include <osmocom/gsm/gsm0411_utils.h>
@@ -92,15 +93,15 @@
 		return NULL;
 
 	sms->receiver = subscr_get(receiver);
-	strncpy(sms->text, text, sizeof(sms->text)-1);
+	osmo_strscpy(sms->text, text);
 
-	strncpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)-1);
+	osmo_strscpy(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);
+	osmo_strscpy(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);
+	osmo_strscpy(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..28f0f0b 100644
--- a/openbsc/src/libmsc/meas_feed.c
+++ b/openbsc/src/libmsc/meas_feed.c
@@ -8,6 +8,7 @@
 #include <osmocom/core/socket.h>
 #include <osmocom/core/write_queue.h>
 #include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
 
 #include <osmocom/vty/command.h>
 #include <osmocom/vty/vty.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';
+	osmo_strscpy(mfm->imsi, subscr->imsi);
+	osmo_strscpy(mfm->name, subscr->name);
+	osmo_strscpy(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';
+	osmo_strscpy(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..642a1a7 100644
--- a/openbsc/src/libmsc/smpp_openbsc.c
+++ b/openbsc/src/libmsc/smpp_openbsc.c
@@ -133,12 +133,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);
+	osmo_strscpy(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);
+	osmo_strscpy(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 24a3cdd..9635fc0 100644
--- a/openbsc/src/libmsc/vty_interface_layer3.c
+++ b/openbsc/src/libmsc/vty_interface_layer3.c
@@ -567,8 +567,7 @@
 		return CMD_WARNING;
 	}
 
-	strncpy(subscr->name, name, sizeof(subscr->name));
-	subscr->name[sizeof(subscr->name)-1] = '\0';
+	osmo_strscpy(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..39dd46d 100644
--- a/openbsc/src/libtrau/rtp_proxy.c
+++ b/openbsc/src/libtrau/rtp_proxy.c
@@ -29,6 +29,7 @@
 #include <sys/utsname.h> /* uname() */
 
 #include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
 #include <openbsc/gsm_data.h>
 #include <osmocom/core/msgb.h>
 #include <osmocom/core/select.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';
+			osmo_strscpy(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..7003e39 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 <osmocom/core/linuxlist.h>
 #include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
 #include <osmocom/gsm/gsm0808.h>
 #include <osmocom/gsm/ipa.h>
 
@@ -129,15 +130,13 @@
 {
 	if (strncmp(chosen_number, "00", 2) == 0) {
 		called->type = 1;
-		strncpy(called->number, chosen_number + 2, sizeof(called->number));
+		osmo_strscpy(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));
+		osmo_strscpy(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..67ce574 100644
--- a/openbsc/src/utils/meas_vis.c
+++ b/openbsc/src/utils/meas_vis.c
@@ -73,7 +73,7 @@
 	ms = find_ms(imsi);
 	if (!ms) {
 		ms = talloc_zero(NULL, struct ms_state);
-		strncpy(ms->imsi, imsi, sizeof(ms->imsi)-1);
+		osmo_strscpy(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 +88,7 @@
 	struct ms_state *ms = find_alloc_ms(mfm->imsi);
 	time_t now = time(NULL);
 
-	strncpy(ms->name, mfm->name, sizeof(ms->imsi)-1);
+	osmo_strscpy(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..ebb74de 100644
--- a/openbsc/tests/gtphub/gtphub_test.c
+++ b/openbsc/tests/gtphub/gtphub_test.c
@@ -449,21 +449,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)";
+	osmo_strscpy(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)";
+	osmo_strscpy(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 d3179e3..1b4d6ea 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);
+	osmo_strscpy(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);
+	osmo_strscpy(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);
+	osmo_strscpy(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: newpatchset
Gerrit-Change-Id: I900726cf06d34128db22a3d3d911ee0d1423b1bd
Gerrit-PatchSet: 3
Gerrit-Project: openbsc
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list