Change in osmo-bts[master]: osmo-bts-trx: refactor parse_rsp(), fix compilation warnings

fixeria gerrit-no-reply at lists.osmocom.org
Tue Apr 27 17:01:35 UTC 2021


fixeria has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-bts/+/23917 )


Change subject: osmo-bts-trx: refactor parse_rsp(), fix compilation warnings
......................................................................

osmo-bts-trx: refactor parse_rsp(), fix compilation warnings

For a long time, we see this annoying -Wstringop-truncation warning:

  In function ‘parse_rsp’,
      inlined from ‘trx_ctrl_read_cb’ at trx_if.c:647:6:
  trx_if.c:416:2: warning: ‘strncat’ output may be truncated copying
                  between 0 and 45 bytes from a string of length 1495
    416 |  strncat(rsp->cmd, buf_in + 4, p - buf_in - 4);
        |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There is no real need to use strncat() in parse_rsp(), because we
do not concatenate any strings but simply copy TRXC response parts
from the input buffer to the corresponding 'name' and 'parameters'
buffers.  Let's use memcpy() instead.  This also fixes the warning.

Change-Id: Ia10adf7d76abe9a423b07e828852fbfb53b95125
---
M src/osmo-bts-trx/trx_if.c
1 file changed, 20 insertions(+), 12 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-bts refs/changes/17/23917/1

diff --git a/src/osmo-bts-trx/trx_if.c b/src/osmo-bts-trx/trx_if.c
index d2ad877..a79c347 100644
--- a/src/osmo-bts-trx/trx_if.c
+++ b/src/osmo-bts-trx/trx_if.c
@@ -397,6 +397,7 @@
 
 static int parse_rsp(const char *buf_in, size_t len_in, struct trx_ctrl_rsp *rsp)
 {
+	size_t nlen, plen;
 	char *p, *k;
 
 	if (strncmp(buf_in, "RSP ", 4))
@@ -406,14 +407,17 @@
 	if (!(p = strchr(buf_in + 4, ' ')))
 		goto parse_err;
 
-	if (p - buf_in >= sizeof(rsp->cmd)) {
-		LOGP(DTRX, LOGL_ERROR, "cmd buffer too small %lu >= %zu\n",
-		     (long unsigned) (p - buf_in), sizeof(rsp->cmd));
+	/* Calculate length of the name part */
+	nlen = p - (buf_in + 4);
+
+	if (nlen >= sizeof(rsp->cmd)) {
+		LOGP(DTRX, LOGL_ERROR, "TRXC command name part is too long: "
+		     "%zu >= %zu\n", nlen, sizeof(rsp->cmd));
 		goto parse_err;
 	}
 
-	rsp->cmd[0] = '\0';
-	strncat(rsp->cmd, buf_in + 4, p - buf_in - 4);
+	memcpy(&rsp->cmd[0], buf_in + 4, nlen);
+	rsp->cmd[nlen] = '\0';
 
 	/* Now comes the status code of the response */
 	p++;
@@ -427,18 +431,22 @@
 	else
 		k = p + strlen(p);
 
-	if (strlen(k) >= sizeof(rsp->params)) {
-		LOGP(DTRX, LOGL_ERROR, "params buffer too small %zu >= %zu\n",
-			strlen(k), sizeof(rsp->params));
+	/* Calculate length of the parameters part */
+	plen = strlen(k);
+
+	if (plen >= sizeof(rsp->params)) {
+		LOGP(DTRX, LOGL_ERROR, "TRXC command parameters part is too long: "
+		     "%zu >= %zu\n", plen, sizeof(rsp->params));
 		goto parse_err;
 	}
-	rsp->params[0] = '\0';
-	strcat(rsp->params, k);
+
+	memcpy(&rsp->params[0], k, plen);
+	rsp->params[plen] = '\0';
+
 	return 0;
 
 parse_err:
-	LOGP(DTRX, LOGL_NOTICE, "Unknown message on ctrl port: %s\n",
-		buf_in);
+	LOGP(DTRX, LOGL_NOTICE, "Unknown TRXC message: %s\n", buf_in);
 	return -1;
 }
 

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-bts/+/23917
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-bts
Gerrit-Branch: master
Gerrit-Change-Id: Ia10adf7d76abe9a423b07e828852fbfb53b95125
Gerrit-Change-Number: 23917
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <vyanitskiy at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210427/f7fa801b/attachment.htm>


More information about the gerrit-log mailing list