Browse Source

strace: handle possible NULL from localtime() call

* strace.c (printleader): Print raw tv_sec value if localtime() returned
NULL.
* tests/localtime.c: New file.
* tests/localtime.test: New test.
* tests/Makefile.am (check_PROGRAMS): Add localtime.
(MISC_TESTS): Add localtime.test.
(localtime_LDADD): New variable.
* tests/.gitignore: Add localtime.
* ci/install-dependencies.sh (common_packages): Add faketime.

Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
Closes: https://github.com/strace/strace/issues/42
Eugene Syromyatnikov 2 years ago
parent
commit
a02a583b3f
6 changed files with 106 additions and 3 deletions
  1. 1
    1
      ci/install-dependencies.sh
  2. 6
    2
      strace.c
  3. 1
    0
      tests/.gitignore
  4. 3
    0
      tests/Makefile.am
  5. 57
    0
      tests/localtime.c
  6. 38
    0
      tests/localtime.test

+ 1
- 1
ci/install-dependencies.sh View File

@@ -1,7 +1,7 @@
1 1
 #!/bin/sh -ex
2 2
 
3 3
 type sudo >/dev/null 2>&1 && sudo=sudo || sudo=
4
-common_packages='autoconf automake file gawk gcc-multilib git gzip libunwind8-dev make xz-utils'
4
+common_packages='autoconf automake faketime file gawk gcc-multilib git gzip libunwind8-dev make xz-utils'
5 5
 
6 6
 updated=
7 7
 apt_get_install()

+ 6
- 2
strace.c View File

@@ -664,9 +664,13 @@ printleader(struct tcb *tcp)
664 664
 				(long long) ts.tv_sec, (long) ts.tv_nsec / 1000);
665 665
 		} else {
666 666
 			time_t local = ts.tv_sec;
667
-			char str[sizeof("HH:MM:SS")];
667
+			char str[MAX(sizeof("HH:MM:SS"), sizeof(ts.tv_sec) * 3)];
668
+			struct tm *tm = localtime(&local);
668 669
 
669
-			strftime(str, sizeof(str), "%T", localtime(&local));
670
+			if (tm)
671
+				strftime(str, sizeof(str), "%T", tm);
672
+			else
673
+				xsprintf(str, "%lld", (long long) local);
670 674
 			if (tflag > 1)
671 675
 				tprintf("%s.%06ld ",
672 676
 					str, (long) ts.tv_nsec / 1000);

+ 1
- 0
tests/.gitignore View File

@@ -173,6 +173,7 @@ link
173 173
 linkat
174 174
 list_sigaction_signum
175 175
 llseek
176
+localtime
176 177
 lookup_dcookie
177 178
 lseek
178 179
 lstat

+ 3
- 0
tests/Makefile.am View File

@@ -119,6 +119,7 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
119 119
 	is_linux_mips_n64 \
120 120
 	ksysent \
121 121
 	list_sigaction_signum \
122
+	localtime \
122 123
 	mmsg-silent \
123 124
 	mmsg_name-v \
124 125
 	msg_control-v \
@@ -175,6 +176,7 @@ filter_unavailable_LDADD = -lpthread $(LDADD)
175 176
 fstat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
176 177
 fstatat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
177 178
 ftruncate64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
179
+localtime_LDADD = $(clock_LIBS) $(LDADD)
178 180
 lstat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
179 181
 mmap64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
180 182
 mq_LDADD = $(mq_LIBS) $(LDADD)
@@ -289,6 +291,7 @@ MISC_TESTS = \
289 291
 	inject-nf.test \
290 292
 	interactive_block.test \
291 293
 	ksysent.test \
294
+	localtime.test \
292 295
 	opipe.test \
293 296
 	options-syntax.test \
294 297
 	pc.test \

+ 57
- 0
tests/localtime.c View File

@@ -0,0 +1,57 @@
1
+/*
2
+ * Check handling of localtime() returning NULL in printleader().
3
+ *
4
+ * Copyright (c) 2018 The strace developers.
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions
9
+ * are met:
10
+ * 1. Redistributions of source code must retain the above copyright
11
+ *    notice, this list of conditions and the following disclaimer.
12
+ * 2. Redistributions in binary form must reproduce the above copyright
13
+ *    notice, this list of conditions and the following disclaimer in the
14
+ *    documentation and/or other materials provided with the distribution.
15
+ * 3. The name of the author may not be used to endorse or promote products
16
+ *    derived from this software without specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ */
29
+
30
+#include "tests.h"
31
+
32
+#include <assert.h>
33
+#include <stdio.h>
34
+#include <time.h>
35
+#include <unistd.h>
36
+
37
+#include <asm/unistd.h>
38
+
39
+int
40
+main(void)
41
+{
42
+	struct timespec ts;
43
+	int pid;
44
+
45
+	assert(!clock_gettime(CLOCK_REALTIME, &ts));
46
+
47
+	pid = syscall(__NR_gettid);
48
+
49
+	/* We expect localtime to fail here */
50
+	printf("%lld.%06ld gettid() = %d\n",
51
+	       (long long) ts.tv_sec, (long) (ts.tv_nsec / 1000), pid);
52
+
53
+	printf("%lld.%06ld +++ exited with 0 +++\n",
54
+	       (long long) ts.tv_sec, (long) (ts.tv_nsec / 1000));
55
+
56
+	return 0;
57
+}

+ 38
- 0
tests/localtime.test View File

@@ -0,0 +1,38 @@
1
+#!/bin/sh
2
+#
3
+# Check handling of localtime() failure in printleader().
4
+#
5
+# Copyright (c) 2018 The strace developers.
6
+# All rights reserved.
7
+#
8
+# Redistribution and use in source and binary forms, with or without
9
+# modification, are permitted provided that the following conditions
10
+# are met:
11
+# 1. Redistributions of source code must retain the above copyright
12
+#    notice, this list of conditions and the following disclaimer.
13
+# 2. Redistributions in binary form must reproduce the above copyright
14
+#    notice, this list of conditions and the following disclaimer in the
15
+#    documentation and/or other materials provided with the distribution.
16
+# 3. The name of the author may not be used to endorse or promote products
17
+#    derived from this software without specific prior written permission.
18
+#
19
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+. "${srcdir=.}/init.sh"
31
+
32
+check_prog faketime
33
+faketime -f +1000000000000000000x0 ../$NAME | grep -q '^100000000' \
34
+	|| framework_skip_ 'faketime failed to fake time'
35
+
36
+STRACE="faketime -f +1000000000000000000x0 $STRACE"
37
+
38
+run_strace_match_diff -tt -a10 -e trace=gettid

Loading…
Cancel
Save