diff mbox series

[iproute2,10/11] rdma/utils: fix some analyzer warnings

Message ID 20230509212125.15880-11-stephen@networkplumber.org (mailing list archive)
State Accepted
Commit 33722349feb9ac8ea77cf658f79940a42261f44d
Delegated to: Stephen Hemminger
Headers show
Series fix analyzer warnings | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Stephen Hemminger May 9, 2023, 9:21 p.m. UTC
Add error checks for cases where analyzer thinks it is possible
to us a possibly NULL value.

utils.c: In function ‘get_port_from_argv’:
utils.c:76:17: warning: use of NULL where non-null expected [CWE-476] [-Wanalyzer-null-argument]
   76 |         slash = strchr(rd_argv(rd), '/');
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~
  ‘get_port_from_argv’: events 1-2
    |
    |   68 | static int get_port_from_argv(struct rd *rd, uint32_t *port,
    |      |            ^~~~~~~~~~~~~~~~~~
    |      |            |
    |      |            (1) entry to ‘get_port_from_argv’
    |......
    |   76 |         slash = strchr(rd_argv(rd), '/');
    |      |                        ~
    |      |                        |
    |      |                        (2) inlined call to ‘rd_argv’ from ‘get_port_from_argv’
    |
    +--> ‘rd_argv’: event 3
           |
           |   18 |         if (!rd_argc(rd))
           |      |            ^
           |      |            |
           |      |            (3) following ‘true’ branch...
           |
    <------+
    |
  ‘get_port_from_argv’: events 4-5
    |
    |   76 |         slash = strchr(rd_argv(rd), '/');
    |      |                 ^~~~~~~~~~~~~~~~~~~~~~~~
    |      |                 |
    |      |                 (4) ...to here
    |      |                 (5) argument 1 (‘<unknown>’) NULL where non-null expected
    |
In file included from rdma.h:10,
                 from utils.c:7:
/usr/include/string.h:246:14: note: argument 1 of ‘strchr’ must be non-null
  246 | extern char *strchr (const char *__s, int __c)
      |              ^~~~~~

Fixes: 40df8263a0f0 ("rdma: Add dev object")
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 rdma/utils.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
diff mbox series

Patch

diff --git a/rdma/utils.c b/rdma/utils.c
index 21177b565bf1..a33ff420f8cb 100644
--- a/rdma/utils.c
+++ b/rdma/utils.c
@@ -75,6 +75,13 @@  static int get_port_from_argv(struct rd *rd, uint32_t *port,
 
 	slash = strchr(rd_argv(rd), '/');
 	/* if no port found, return 0 */
+	if (slash == NULL) {
+		if (strict_port)
+			return -EINVAL;
+		else
+			return 0;
+	}
+
 	if (slash++) {
 		if (*slash == '-') {
 			if (strict_port)
@@ -747,6 +754,9 @@  struct dev_map *dev_map_lookup(struct rd *rd, bool allow_port_index)
 		return NULL;
 
 	dev_name = strdup(rd_argv(rd));
+	if (!dev_name)
+		return NULL;
+
 	if (allow_port_index) {
 		slash = strrchr(dev_name, '/');
 		if (slash)