From 3563b623be8992441e27a7f550faee5ba2b0b1c9 Mon Sep 17 00:00:00 2001 From: Mamoru TASAKA Date: Tue, 1 Aug 2017 15:13:44 +0900 Subject: [PATCH] store_saver_id: fix stack-use-after-scope gcc71 -fsanitize-address detects the following stack-use-after-scope error: $ ./driver/xscreensaver ================================================================= ==23777==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7ffdb4460461 at pc 0x7efd5ef6a36e bp 0x7ffdb4460200 sp 0x7ffdb445f9a8 READ of size 22 at 0x7ffdb4460461 thread T0 #0 0x7efd5ef6a36d (/lib64/libasan.so.4+0x5136d) #1 0x41606c in store_saver_id ../../driver/windows.c:991 #2 0x41606c in initialize_screensaver_window_1 ../../driver/windows.c:1340 #3 0x41ad48 in initialize_screensaver_window ../../driver/windows.c:1368 #4 0x409bce in main ../../driver/xscreensaver.c:1556 #5 0x7efd5b9a04d9 in __libc_start_main (/lib64/libc.so.6+0x204d9) #6 0x40dde9 in _start (/home/mtasaka/rpmbuild/fedora-specific/xscreensaver/master/xscreensaver-5.37/x86_64-unknown-linux-gnu/driver/xscreensaver+0x40dde9) Address 0x7ffdb4460461 is located in stack of thread T0 at offset 481 in frame #0 0x41533f in initialize_screensaver_window_1 ../../driver/windows.c:1155 This frame has 6 object(s): [32, 48) 'black' [96, 112) 'class_hints' [160, 200) 'changes' [256, 368) 'attrs' [416, 806) 'uts' <== Memory access at offset 481 is inside this variable [864, 884) 'buf' uts.nodename is non-dynamically allocated array on stack, so uts must be in the scope when using uts.nodename. --- driver/windows.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/driver/windows.c b/driver/windows.c index 1a9701a..9b2bf84 100644 --- a/driver/windows.c +++ b/driver/windows.c @@ -944,6 +944,9 @@ store_saver_id (saver_screen_info *ssi) struct passwd *p = getpwuid (getuid ()); const char *name, *host; char *id; +# if defined(HAVE_UNAME) + struct utsname uts; +# endif /* UNAME */ /* First store the name and class on the window. */ @@ -976,7 +979,6 @@ store_saver_id (saver_screen_info *ssi) # if defined(HAVE_UNAME) { - struct utsname uts; if (uname (&uts) < 0) host = "???"; else -- 2.13.3