aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-security/optee-imx/optee-client/0001-libteec-refactor-_dprintf.patch
blob: 1c053f38d4c3c57e3ebf3a1483b6a3dc5bdd1882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
Upstream-Status: Backport 3.3.0

Signed-off-by: Peter Griffin <peter.griffin@linaro.org>
---
From 0361f9b21bb1acfaf23323a121f542fe03dcd2c8 Mon Sep 17 00:00:00 2001
From: Jerome Forissier <jerome.forissier@linaro.org>
Date: Thu, 5 Jul 2018 15:15:31 +0200
Subject: [PATCH] libteec: refactor _dprintf()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

GCC8.1 gives an error when compiling _dprintf():

src/teec_trace.c: In function ‘_dprintf’:
src/teec_trace.c:110:5: error: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size 246 [-Werror=format-truncation=]
     "%s [%d] %s:%s:%d: %s",
     ^~~~~~~~~~~~~~~~~~~~~~
src/teec_trace.c:112:11:
     line, raw);
           ~~~
src/teec_trace.c:109:3: note: ‘snprintf’ output 11 or more bytes (assuming 266) into a destination of size 256
   snprintf(prefixed, MAX_PRINT_SIZE,
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     "%s [%d] %s:%s:%d: %s",
     ~~~~~~~~~~~~~~~~~~~~~~~
     trace_level_strings[level], thread_id, prefix, func,
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     line, raw);
     ~~~~~~~~~~

Fix this error by using a single output buffer, printing the prefix first
then the other arguments with the supplied format.

In addition, further simplify the function by getting rid of things that
do not make much sense:
- Remove the 'flen' parameter, which is only ever set to zero or
  strlen(__func__).
- Remove the TRACE_FUNC_LENGTH_CST macro which is not set by default and
  does not seem very useful.
- Change the return type to void because callers do not care about success
  or failure.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Reviewed-by: Joakim Bech <joakim.bech@linaro.org>
---
 libteec/src/teec_trace.c | 63 +++++++++++++++---------------------------------
 public/teec_trace.h      |  8 +++---
 2 files changed, 23 insertions(+), 48 deletions(-)

diff --git a/libteec/src/teec_trace.c b/libteec/src/teec_trace.c
index 78b79d6..3a2a0da 100644
--- a/libteec/src/teec_trace.c
+++ b/libteec/src/teec_trace.c
@@ -47,7 +47,6 @@
  * PPPP: MMMMM [FFFFFFFFFFFFFFF : LLLLL]
  */
 #define MAX_PRINT_SIZE 256
-#define MAX_FUNC_PRINT_SIZE 32
 
 #ifdef TEEC_LOG_FILE
 static void log_to_file(const char *buffer)
@@ -69,57 +68,33 @@ static const char * const trace_level_strings[] = {
 	"", "ERR", "INF", "DBG", "FLW"
 };
 
-int _dprintf(const char *function, int flen, int line, int level,
-	     const char *prefix, const char *fmt, ...)
+void _dprintf(const char *function, int line, int level, const char *prefix,
+	      const char *fmt, ...)
 {
-	char raw[MAX_PRINT_SIZE];
-	char prefixed[MAX_PRINT_SIZE];
-	char *to_print = NULL;
-	const char *func;
-	int err;
+	char msg[MAX_PRINT_SIZE];
+	int n = 0;
 	va_list ap;
 
-	va_start(ap, fmt);
-	err = vsnprintf(raw, sizeof(raw), fmt, ap);
-	va_end(ap);
-
 	if (function) {
-#ifdef TRACE_FUNC_LENGTH_CST
-		char func_buf[MAX_FUNC_PRINT_SIZE];
-		/* Limit the function name to MAX_FUNC_PRINT_SIZE characters. */
-		strncpy(func_buf, function, flen > MAX_FUNC_PRINT_SIZE ?
-			(MAX_FUNC_PRINT_SIZE - 1) : flen);
-		if (flen < (MAX_FUNC_PRINT_SIZE - 1)) {
-			memset(func_buf + flen, 0x20,
-			       (MAX_FUNC_PRINT_SIZE - flen));
-		}
-		func_buf[MAX_FUNC_PRINT_SIZE - 1] = '\0';
-		func = func_buf;
-#else
-		(void)flen;
-		func = function;
-#endif
+		int thread_id = syscall(SYS_gettid);
 
-		/*
-		 * pthread_self returns the POSIX tid which is different from
-		 * the kernel id
-		 */
-		int thread_id = syscall(SYS_gettid);	/* perf issue ? */
-
-		snprintf(prefixed, MAX_PRINT_SIZE,
-			 "%s [%d] %s:%s:%d: %s",
-			 trace_level_strings[level], thread_id, prefix, func,
-			 line, raw);
-		to_print = prefixed;
-	} else {
-		to_print = raw;
+		n = snprintf(msg, sizeof(msg), "%s [%d] %s:%s:%d: ",
+			trace_level_strings[level], thread_id, prefix,
+			function, line);
+		if (n < 0)
+			return;
 	}
 
-	fprintf(stdout, "%s", to_print);
-
-	log_to_file(to_print);
+	if ((size_t)n < sizeof(msg)) {
+		va_start(ap, fmt);
+		n = vsnprintf(msg + n, sizeof(msg) - n, fmt, ap);
+		va_end(ap);
+		if (n < 0)
+			return;
+	}
 
-	return err;
+	fprintf(stdout, "%s", msg);
+	log_to_file(msg);
 }
 
 #if (defined(DEBUGLEVEL_3) || defined(DEBUGLEVEL_true) || defined(DEBUGLEVEL_4))
diff --git a/public/teec_trace.h b/public/teec_trace.h
index 28e290c..f75358f 100644
--- a/public/teec_trace.h
+++ b/public/teec_trace.h
@@ -91,12 +91,12 @@ extern "C" {
 #define __PRINTFLIKE(__fmt, __varargs) __attribute__\
 	((__format__(__printf__, __fmt, __varargs)))
 
-int _dprintf(const char *function, int flen, int line, int level,
-	     const char *prefix, const char *fmt, ...) __PRINTFLIKE(6, 7);
+void _dprintf(const char *function, int line, int level, const char *prefix,
+	      const char *fmt, ...) __PRINTFLIKE(5, 6);
 
 #define dprintf(level, x...) do { \
 		if ((level) <= DEBUGLEVEL) { \
-			_dprintf(__func__, strlen(__func__), __LINE__, level, \
+			_dprintf(__func__, __LINE__, level, \
 				 BINARY_PREFIX, x); \
 		} \
 	} while (0)
@@ -118,7 +118,7 @@ int _dprintf(const char *function, int flen, int line, int level,
 
 #define dprintf_raw(level, x...) do { \
 		if ((level) <= DEBUGLEVEL) \
-			_dprintf(0, 0, 0, (level), BINARY_PREFIX, x); \
+			_dprintf(0, 0, (level), BINARY_PREFIX, x); \
 	} while (0)
 
 #define EMSG_RAW(fmt, ...)   dprintf_raw(TRACE_ERROR, fmt, ##__VA_ARGS__)
-- 
2.7.4