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
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);