aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobin C. Harding <me@tobin.cc>2018-03-12 00:27:23 -0400
committerRob Herring <robh@kernel.org>2018-03-17 19:52:41 -0400
commitd4b9e425d23a2c38e2cc52f62c79773ccff0ecc4 (patch)
treefcca145013ecfb441fa9b6fca94b84ad3c9042b3
parenta514266ba675b28ff223ecd1fc431810ec828337 (diff)
of: unittest: Remove VLA stack usage
The kernel would like to have all stack VLA usage removed[1]. This is a test function so the execution speed is not critical. We can allocate memory for this buffer instead of using a VLA. If kmalloc() fails just return. Allocate buffer with kmalloc(). [1]: https://lkml.org/lkml/2018/3/7/621 Signed-off-by: Tobin C. Harding <me@tobin.cc> Signed-off-by: Rob Herring <robh@kernel.org>
-rw-r--r--drivers/of/unittest.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index 070caefb3b39..df93149d6146 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -254,12 +254,18 @@ static void __init of_unittest_check_tree_linkage(void)
254static void __init of_unittest_printf_one(struct device_node *np, const char *fmt, 254static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
255 const char *expected) 255 const char *expected)
256{ 256{
257 unsigned char buf[strlen(expected)+10]; 257 unsigned char *buf;
258 int buf_size;
258 int size, i; 259 int size, i;
259 260
261 buf_size = strlen(expected) + 10;
262 buf = kmalloc(buf_size, GFP_KERNEL);
263 if (!buf)
264 return;
265
260 /* Baseline; check conversion with a large size limit */ 266 /* Baseline; check conversion with a large size limit */
261 memset(buf, 0xff, sizeof(buf)); 267 memset(buf, 0xff, buf_size);
262 size = snprintf(buf, sizeof(buf) - 2, fmt, np); 268 size = snprintf(buf, buf_size - 2, fmt, np);
263 269
264 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */ 270 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
265 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff), 271 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
@@ -270,12 +276,13 @@ static void __init of_unittest_printf_one(struct device_node *np, const char *fm
270 size++; 276 size++;
271 for (i = 0; i < 2; i++, size--) { 277 for (i = 0; i < 2; i++, size--) {
272 /* Clear the buffer, and make sure it works correctly still */ 278 /* Clear the buffer, and make sure it works correctly still */
273 memset(buf, 0xff, sizeof(buf)); 279 memset(buf, 0xff, buf_size);
274 snprintf(buf, size+1, fmt, np); 280 snprintf(buf, size+1, fmt, np);
275 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff), 281 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
276 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n", 282 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
277 size, fmt, expected, buf); 283 size, fmt, expected, buf);
278 } 284 }
285 kfree(buf);
279} 286}
280 287
281static void __init of_unittest_printf(void) 288static void __init of_unittest_printf(void)