selftest:net: Fix uninit return values
Subsystems (4)
PATCH v4 1/1 selftest:net: Fix uninit return values
Fix functions that return undefined values. These issues were caught by running clang using LLVM=1 option.
Clang warnings are as follows:
ovpn-cli.c:1587:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
1587 | if (!sock) {
| ^~~~~ovpn-cli.c:1635:9: note: uninitialized use occurs here
1635 | return ret;
| ^~~
ovpn-cli.c:1587:2: note: remove the 'if' if its condition is always false
1587 | if (!sock) {
| ^~~~~~~~~~~~
1588 | fprintf(stderr, "cannot allocate netlink socket\n");
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1589 | goto err_free;
| ~~~~~~~~~~~~~~
1590 | }
| ~
ovpn-cli.c:1584:15: note: initialize the variable 'ret' to silence this warning
1584 | int mcid, ret;
| ^
| = 0
ovpn-cli.c:2107:7: warning: variable 'ret' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]
2107 | case CMD_INVALID:
| ^~~~~~~~~~~ovpn-cli.c:2111:9: note: uninitialized use occurs here
2111 | return ret;
| ^~~
ovpn-cli.c:1939:12: note: initialize the variable 'ret' to silence this warning
1939 | int n, ret;
| ^
|ovpn module")
---
v4:
- Move changelog below sign-off.
- Remove double-hyphens in commit description.
v3:
- Use prefix net.
- Remove so_txtime fix as default case calls error().
- Changelog before sign-off.
- Three dashes after sign-off
v2:
- Use subsystem name "net".
- Add fixes tags.
- Remove txtimestamp fix as default case calls error.
- Assign constant error string instead of NULL.
tools/testing/selftests/net/ovpn/ovpn-cli.c +2 -2
@@ -1581,7 +1581,7 @@ static int ovpn_listen_mcast(void) {struct nl_sock *sock;struct nl_cb *cb; -int mcid, ret; +int mcid, ret =-1 ; sock =nl_socket_alloc ();if (!sock) {
@@ -1936,7 +1936,7 @@ static int ovpn_run_cmd(struct ovpn_ctx *ovpn) {char peer_id[10 ], vpnip[INET6_ADDRSTRLEN ], laddr[128 ], lport[10 ];char raddr[128 ], rport[10 ]; -int n, ret; +int n, ret =-1 ;FILE *fp;switch (ovpn->cmd) {
Signature · 1 line
2.47.3
Antonio Quartulli
maintainerHi,
Thanks a lot for fixing this - I hadn't see the warnings with gcc.
Quoted 60 lines · Sidharth Seela
Fix functions that return undefined values. These issues were caught by running clang using LLVM=1 option.
Clang warnings are as follows:
ovpn-cli.c:1587:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]1587 | if (!sock) { | ^~~~~ovpn-cli.c:1635:9: note: uninitialized use occurs here
1635 | return ret;
| ^~~
ovpn-cli.c:1587:2: note: remove the 'if' if its condition is always false1587 | if (!sock) { | ^~~~~~~~~~~~ 1588 | fprintf(stderr, "cannot allocate netlink socket\n"); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1589 | goto err_free; | ~~~~~~~~~~~~~~ 1590 | } | ~ ovpn-cli.c:1584:15: note: initialize the variable 'ret' to silence this warning 1584 | int mcid, ret; | ^ | = 0 ovpn-cli.c:2107:7: warning: variable 'ret' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized] 2107 | case CMD_INVALID: | ^~~~~~~~~~~ovpn-cli.c:2111:9: note: uninitialized use occurs here
2111 | return ret;
| ^~~
ovpn-cli.c:1939:12: note: initialize the variable 'ret' to silence this warning1939 | int n, ret; | ^ |Fixes 959bc330a439 ("testing/selftests: add test tool and scripts for ovpn module")ovpn module")
Signed-off-by Sidharth Seela <[email protected]>---
v4:
- Move changelog below sign-off.
- Remove double-hyphens in commit description.
v3:
- Use prefix net.
- Removeso_txtimefix as default case callserror().
- Changelog before sign-off.
- Three dashes after sign-off
v2:
- Use subsystem name "net".
- Add fixes tags.
- Remove txtimestamp fix as default case calls error.
- Assign constant error string instead of NULL.tools/testing/selftests/net/ovpn/ovpn-cli.c +0
-0
tools/testing/selftests/net/ovpn/ovpn-cli.c static int ovpn_listen_mcast(void) · line 1584
Quoted 5 lines
tools/testing/selftests/net/ovpn/ovpn-cli.c +1
-1@@ -1581,7 +1581,7 @@ static int ovpn_listen_mcast(void) {struct nl_sock *sock;struct nl_cb *cb; -int mcid, ret; +int mcid, ret =-1 ;
ret goes uninitialized only under the "if (!sock)" condition, therefore I'd rather assign ret a meaningful value instead of -1.
How about adding "err = -ENOMEM;" directly inside the if block?
tools/testing/selftests/net/ovpn/ovpn-cli.c static int ovpn_run_cmd(struct ovpn_ctx *ovpn) · line 1939
Quoted 9 lines
tools/testing/selftests/net/ovpn/ovpn-cli.c +1
-1sock =nl_socket_alloc ();if (!sock) {@@ -1936,7 +1936,7 @@ static int ovpn_run_cmd(struct ovpn_ctx *ovpn) {char peer_id[10 ], vpnip[INET6_ADDRSTRLEN ], laddr[128 ], lport[10 ];char raddr[128 ], rport[10 ]; -int n, ret; +int n, ret =-1 ;
Same here.
ret goes uninitialized only under the "CMD_INVALID" case.
How about adding "ret = -EINVAL;" inside the affected case?
Both values are returned by ovpn_run_cmd() and then printed as strerror(-ret). If we blindly use -1 we will get "Operation not permitted" which will confuse the user IMHO.
Thanks a lot!
Quoted 3 lines
tools/testing/selftests/net/ovpn/ovpn-cli.c +0
-0FILE *fp;switch (ovpn->cmd) {
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Quoted 2 lines · Antonio Quartulli
Hi,
Thanks a lot for fixing this - I hadn't see the warnings with gcc.
I am glad, thankyou.
Quoted 2 lines
ret goes uninitialized only under the "if (!sock)" condition, therefore I'd rather assign ret a meaningful value instead of -1.
Yes, you are right.
Quoted 8 lines
How about adding "err =
-ENOMEM;" directly inside the if block? Same here.
ret goes uninitialized only under the "CMD_INVALID" case. How about adding "ret =-EINVAL;" inside the affected case? Both values are returned byovpn_run_cmd()and then printed as strerror(-ret).
If we blindly use -1 we will get "Operation not permitted" which will confuse the user IMHO.
Alright, understood, Thank you.
Sending in the changes in v5.
Signature · 3 lines
Thanks, Sidharth Seela www.realtimedesign.org