hc
2023-11-22 f743a7adbd6e230d66a6206fa115b59fec2d88eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
From 61291de03cb6dd1aea2a633eb72951f3fe453e7f Mon Sep 17 00:00:00 2001
From: Aaron Conole <aconole@redhat.com>
Date: Mon, 3 Aug 2020 15:33:08 -0400
Subject: [PATCH 8/9] stringops: fix some string copy errors
 
Reported when using gcc-10.
 
Signed-off-by: Aaron Conole <aconole@redhat.com>
---
 dcb_protocol.c | 13 ++++---------
 lldp/ports.c   |  2 +-
 2 files changed, 5 insertions(+), 10 deletions(-)
 
diff --git a/dcb_protocol.c b/dcb_protocol.c
index 75ca139..930251b 100644
--- a/dcb_protocol.c
+++ b/dcb_protocol.c
@@ -2257,13 +2257,8 @@ cmd_status get_bwg_descrpt(char *device_name, u8 bwgid, char **name)
 
     if ((it != NULL) &&
         (bwgid < it->second->max_pgid_desc)) {
-        size = (int)strlen(it->second->pgid_desc[bwgid]) +
-            sizeof(char);  /* Localization OK */
-        *name = (char*)malloc(size);
-        if (*name != NULL) {
-            strncpy(*name, it->second->pgid_desc[bwgid],
-                    size); /* Localization OK */
-        } else {
+        *name = strdup(it->second->pgid_desc[bwgid]);
+        if (*name == NULL) {
             goto Error;
         }
     } else {
@@ -2272,9 +2267,9 @@ cmd_status get_bwg_descrpt(char *device_name, u8 bwgid, char **name)
             size = (int)strlen(
                 attribs.descript.pgid_desc[bwgid]) +
                 sizeof(char);
-            *name = (char*)malloc(size);
+            *name = (char*)calloc(size, sizeof(char));
             if (*name != NULL) {
-                memcpy(*name, attribs.descript.pgid_desc[bwgid], size); /* Localization OK */
+                memcpy(*name, attribs.descript.pgid_desc[bwgid], size - 1); /* Localization OK */
             } else {
                 goto Error;
             }
diff --git a/lldp/ports.c b/lldp/ports.c
index 6384f14..9b681f7 100644
--- a/lldp/ports.c
+++ b/lldp/ports.c
@@ -264,7 +264,7 @@ struct port *add_port(int ifindex, const char *ifname)
     memset(newport, 0, sizeof(*newport));
     newport->ifindex = ifindex;
     newport->next = NULL;
-    strncpy(newport->ifname, ifname, IFNAMSIZ);
+    strncpy(newport->ifname, ifname, IFNAMSIZ - 1);
 
     newport->bond_master = is_bond(ifname);
     /* Initialize relevant port variables */
-- 
2.28.0