aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDominik Brodowski <linux@dominikbrodowski.net>2018-02-11 15:59:24 -0500
committerIngo Molnar <mingo@kernel.org>2018-02-13 03:04:56 -0500
commitd8e92de8ef952bed88c56c7a44c02d8dcae0984e (patch)
tree4c01ef70a580ea359004440800fd0790030ba25d
parent198ee8e17502da2634f7366395db1d77630e0219 (diff)
selftests/x86: Clean up and document sscanf() usage
Replace a couple of magically connected buffer length literal constants with a common definition that makes their relationship obvious. Also document why our sscanf() usage is safe. No intended functional changes. Suggested-by: Ingo Molnar <mingo@kernel.org> Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net> Cc: Andrew Lutomirski <luto@kernel.org> Cc: Andy Lutomirski <luto@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kselftest@vger.kernel.org Cc: shuah@kernel.org Link: http://lkml.kernel.org/r/20180211205924.GA23210@light.dominikbrodowski.net Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r--tools/testing/selftests/x86/test_vdso.c11
-rw-r--r--tools/testing/selftests/x86/test_vsyscall.c11
2 files changed, 16 insertions, 6 deletions
diff --git a/tools/testing/selftests/x86/test_vdso.c b/tools/testing/selftests/x86/test_vdso.c
index 558c8207e7b9..235259011704 100644
--- a/tools/testing/selftests/x86/test_vdso.c
+++ b/tools/testing/selftests/x86/test_vdso.c
@@ -26,6 +26,9 @@
26# endif 26# endif
27#endif 27#endif
28 28
29/* max length of lines in /proc/self/maps - anything longer is skipped here */
30#define MAPS_LINE_LEN 128
31
29int nerrs = 0; 32int nerrs = 0;
30 33
31typedef long (*getcpu_t)(unsigned *, unsigned *, void *); 34typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
@@ -37,17 +40,19 @@ static void *vsyscall_getcpu(void)
37{ 40{
38#ifdef __x86_64__ 41#ifdef __x86_64__
39 FILE *maps; 42 FILE *maps;
40 char line[128]; 43 char line[MAPS_LINE_LEN];
41 bool found = false; 44 bool found = false;
42 45
43 maps = fopen("/proc/self/maps", "r"); 46 maps = fopen("/proc/self/maps", "r");
44 if (!maps) /* might still be present, but ignore it here, as we test vDSO not vsyscall */ 47 if (!maps) /* might still be present, but ignore it here, as we test vDSO not vsyscall */
45 return NULL; 48 return NULL;
46 49
47 while (fgets(line, sizeof(line), maps)) { 50 while (fgets(line, MAPS_LINE_LEN, maps)) {
48 char r, x; 51 char r, x;
49 void *start, *end; 52 void *start, *end;
50 char name[128]; 53 char name[MAPS_LINE_LEN];
54
55 /* sscanf() is safe here as strlen(name) >= strlen(line) */
51 if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s", 56 if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
52 &start, &end, &r, &x, name) != 5) 57 &start, &end, &r, &x, name) != 5)
53 continue; 58 continue;
diff --git a/tools/testing/selftests/x86/test_vsyscall.c b/tools/testing/selftests/x86/test_vsyscall.c
index 7a744fa7b786..be81621446f0 100644
--- a/tools/testing/selftests/x86/test_vsyscall.c
+++ b/tools/testing/selftests/x86/test_vsyscall.c
@@ -33,6 +33,9 @@
33# endif 33# endif
34#endif 34#endif
35 35
36/* max length of lines in /proc/self/maps - anything longer is skipped here */
37#define MAPS_LINE_LEN 128
38
36static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), 39static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
37 int flags) 40 int flags)
38{ 41{
@@ -98,7 +101,7 @@ static int init_vsys(void)
98#ifdef __x86_64__ 101#ifdef __x86_64__
99 int nerrs = 0; 102 int nerrs = 0;
100 FILE *maps; 103 FILE *maps;
101 char line[128]; 104 char line[MAPS_LINE_LEN];
102 bool found = false; 105 bool found = false;
103 106
104 maps = fopen("/proc/self/maps", "r"); 107 maps = fopen("/proc/self/maps", "r");
@@ -108,10 +111,12 @@ static int init_vsys(void)
108 return 0; 111 return 0;
109 } 112 }
110 113
111 while (fgets(line, sizeof(line), maps)) { 114 while (fgets(line, MAPS_LINE_LEN, maps)) {
112 char r, x; 115 char r, x;
113 void *start, *end; 116 void *start, *end;
114 char name[128]; 117 char name[MAPS_LINE_LEN];
118
119 /* sscanf() is safe here as strlen(name) >= strlen(line) */
115 if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s", 120 if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
116 &start, &end, &r, &x, name) != 5) 121 &start, &end, &r, &x, name) != 5)
117 continue; 122 continue;