[MERGED] openbsc[master]: cosmetic: use osmo_strlcpy() everywhere

Harald Welte gerrit-no-reply at lists.osmocom.org
Mon Jan 23 11:59:03 UTC 2017


Harald Welte has submitted this change and it was merged.

Change subject: cosmetic: use osmo_strlcpy() everywhere
......................................................................


cosmetic: use osmo_strlcpy() everywhere

Shorten some code and make obvious to the reader that the string copy is done
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, 97 insertions(+), 117 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/openbsc/src/gprs/gprs_gmm.c b/openbsc/src/gprs/gprs_gmm.c
index e3d5c7d..88be512 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_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 @@
 		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 @@
 				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 @@
 				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 29e21ce..d3e2ea7 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,8 @@
 	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 @@
 {
 	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 @@
 		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 a0af42b..83e17db 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_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 667013b..afeeda6 100644
--- a/openbsc/src/gprs/gtphub_ares.c
+++ b/openbsc/src/gprs/gtphub_ares.c
@@ -121,8 +121,8 @@
 	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 @@
 	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 4b69a0d..1fa7fc4 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_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 0c723e8..2698225 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_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 @@
 	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 0538482..59fac35 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_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 1f98cc6..1ecdee5 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_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 861212a..8be4e7a 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_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 79e136d..d153247 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_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 5cccb32..68dac76 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_strlcpy(sms->src.addr, sender->extension, sizeof(sms->src.addr));
 	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_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 @@
 	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 @@
 		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 @@
 
 	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 @@
 	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 @@
 	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 @@
 	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 @@
 	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 34492bb..742f803 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,9 @@
 
 	/* 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 @@
 	}
 
 	/* 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 @@
 	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 48a87e0..6164a08 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_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 @@
 		}
 	}
 
-	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 cd2b648..3ddcdc3 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_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_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 228e61d..657272e 100644
--- a/openbsc/src/libmsc/smpp_openbsc.c
+++ b/openbsc/src/libmsc/smpp_openbsc.c
@@ -133,12 +133,13 @@
 	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 4f412b1..490d794 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_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 ed19175..6b38ee5 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,9 @@
 		}
 		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 58667fe..e7c387c 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,15 @@
 {
 	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 061a7a4..316d203 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_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 @@
 	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 2b542dd..e7c27d2 100644
--- a/openbsc/tests/gtphub/gtphub_test.c
+++ b/openbsc/tests/gtphub/gtphub_test.c
@@ -437,21 +437,15 @@
 	       "  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 fea635e..e1fd2e7 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_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 @@
 	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 @@
 
 	/* 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");

-- 
To view, visit https://gerrit.osmocom.org/1592
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I900726cf06d34128db22a3d3d911ee0d1423b1bd
Gerrit-PatchSet: 5
Gerrit-Project: openbsc
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder


More information about the gerrit-log mailing list