Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d4b9e425 authored by Tobin C. Harding's avatar Tobin C. Harding Committed by Rob Herring
Browse files

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: default avatarTobin C. Harding <me@tobin.cc>
Signed-off-by: default avatarRob Herring <robh@kernel.org>
parent a514266b
Loading
Loading
Loading
Loading
+11 −4
Original line number Original line Diff line number Diff line
@@ -254,12 +254,18 @@ static void __init of_unittest_check_tree_linkage(void)
static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
					  const char *expected)
					  const char *expected)
{
{
	unsigned char buf[strlen(expected)+10];
	unsigned char *buf;
	int buf_size;
	int size, i;
	int size, i;


	buf_size = strlen(expected) + 10;
	buf = kmalloc(buf_size, GFP_KERNEL);
	if (!buf)
		return;

	/* Baseline; check conversion with a large size limit */
	/* Baseline; check conversion with a large size limit */
	memset(buf, 0xff, sizeof(buf));
	memset(buf, 0xff, buf_size);
	size = snprintf(buf, sizeof(buf) - 2, fmt, np);
	size = snprintf(buf, buf_size - 2, fmt, np);


	/* use strcmp() instead of strncmp() here to be absolutely sure strings match */
	/* use strcmp() instead of strncmp() here to be absolutely sure strings match */
	unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
	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
	size++;
	size++;
	for (i = 0; i < 2; i++, size--) {
	for (i = 0; i < 2; i++, size--) {
		/* Clear the buffer, and make sure it works correctly still */
		/* Clear the buffer, and make sure it works correctly still */
		memset(buf, 0xff, sizeof(buf));
		memset(buf, 0xff, buf_size);
		snprintf(buf, size+1, fmt, np);
		snprintf(buf, size+1, fmt, np);
		unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
		unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
			"snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
			"snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
			size, fmt, expected, buf);
			size, fmt, expected, buf);
	}
	}
	kfree(buf);
}
}


static void __init of_unittest_printf(void)
static void __init of_unittest_printf(void)