Max has submitted this change and it was merged.

Change subject: utils: add helper wrapper for osmo_strlcpy()
......................................................................


utils: add helper wrapper for osmo_strlcpy()

Add wrapper for osmo_strlcpy() which uses sizeof() to automatically
determine buffer's size and use it for GSMTAP logging. This is pretty
common use case for osmo_strlcpy() so it's a good idea to save some
typing by using generic define.

Related: OS#2864
Change-Id: I03d0d3d32a8d572ad573d03c603e14cdc27a3f7b
---
M include/osmocom/core/utils.h
M src/logging_gsmtap.c
2 files changed, 6 insertions(+), 4 deletions(-)

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



diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 72266ae..f1e011f 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -26,6 +26,9 @@
 /*! Number of bytes necessary to store given BITS */
 #define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8)
 
+/*! Copy a C-string into a sized buffer using sizeof to detect buffer's size */
+#define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array))
+
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
diff --git a/src/logging_gsmtap.c b/src/logging_gsmtap.c
index 82535ad..cb379b0 100644
--- a/src/logging_gsmtap.c
+++ b/src/logging_gsmtap.c
@@ -81,17 +81,16 @@
 
        /* Logging header */
        golh = (struct gsmtap_osmocore_log_hdr *) msgb_put(msg, sizeof(*golh));
-       osmo_strlcpy(golh->proc_name, target->tgt_gsmtap.ident,
-                    sizeof(golh->proc_name));
+       OSMO_STRLCPY_ARRAY(golh->proc_name, target->tgt_gsmtap.ident);
        if (subsys_name)
-               osmo_strlcpy(golh->subsys, subsys_name+1, sizeof(golh->subsys));
+               OSMO_STRLCPY_ARRAY(golh->subsys, subsys_name + 1);
        else
                golh->subsys[0] = '\0';
 
        /* strip all leading path elements from file, if any. */
        file_basename = strrchr(file, '/');
        file = (file_basename && file_basename[1])? file_basename + 1 : file;
-       osmo_strlcpy(golh->src_file.name, file, sizeof(golh->src_file.name));
+       OSMO_STRLCPY_ARRAY(golh->src_file.name, file);
        golh->src_file.line_nr = osmo_htonl(line);
        golh->level = level;
        /* we always store the timestamp in the message, irrespective

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I03d0d3d32a8d572ad573d03c603e14cdc27a3f7b
Gerrit-PatchSet: 3
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <msur...@sysmocom.de>
Gerrit-Reviewer: Harald Welte <lafo...@gnumonks.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Max <msur...@sysmocom.de>

Reply via email to