public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH teavpn2] server: entry: Fix a false-positive warning from GCC 12.1.1
@ 2022-09-01 13:49 Ammar Faizi
  0 siblings, 0 replies; only message in thread
From: Ammar Faizi @ 2022-09-01 13:49 UTC (permalink / raw)
  To: GNU/Weeb Mailing List; +Cc: Ammar Faizi, Arthur Lapz

@rlapz reported that he is seeing a dangling pointer warning when
building TeaVPN2 with GCC 12.1.1 [1]:

  src/teavpn2/server/entry.c: In function ‘run_server’:
  src/teavpn2/server/entry.c:455:18: warning: storing the address of \
  local variable ‘cfg’ in ‘data_dir’ [-Wdangling-pointer=]
    455 |         data_dir = cfg.sys.data_dir;
        |         ~~~~~~~~~^~~~~~~~~~~~~~~~~~

This is a false-positive warning. GCC doesn't really know the real
program flow at runtime. Reasoning why GCC is wrong:

  1) The lifetime of cfg.sys.data dir ends when run_server() in entry.c
     returns.

  2) @data_dir is only used in teavpn2_auth() in auth.c.

  3) teavpn2_auth() is only called in the run_server() call stack.

That being said, when @data_dir var is used, it never be a dangling
pointer. Just to make GCC happy on this, set @data_dir to NULL before
the function returns, this way GCC assumption about dangling pointer
should be proven wrong at compile time. @rlapz confirmed it fixes the
warning.

Link: https://t.me/GNUWeeb/662168 [1]
Reported-by: Arthur Lapz <[email protected]>
Tested-by: Arthur Lapz <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 src/teavpn2/server/entry.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/teavpn2/server/entry.c b/src/teavpn2/server/entry.c
index fd9bc85..6f47ef9 100644
--- a/src/teavpn2/server/entry.c
+++ b/src/teavpn2/server/entry.c
@@ -455,9 +455,13 @@ __cold int run_server(int argc, char *argv[])
 	data_dir = cfg.sys.data_dir;
 	switch (cfg.sock.type) {
 	case SOCK_UDP:
-		return -teavpn2_server_udp_run(&cfg);
+		ret = -teavpn2_server_udp_run(&cfg);
+		break;
 	case SOCK_TCP:
 	default:
-		return ESOCKTNOSUPPORT;
+		ret = ESOCKTNOSUPPORT;
+		break;
 	}
+	data_dir = NULL;
+	return ret;
 }

base-commit: cfee418df11d3b1c67ed96da25807462f68b3c9c
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-09-01 13:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-01 13:49 [PATCH teavpn2] server: entry: Fix a false-positive warning from GCC 12.1.1 Ammar Faizi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox