Skip to content

Commit 7b4481c

Browse files
rfvirgilshuahkh
authored andcommitted
kunit: Don't use a managed alloc in is_literal()
There is no need to use a test-managed alloc in is_literal(). The function frees the temporary buffer before returning. This removes the only use of the test and gfp members of struct string_stream outside of the string_stream implementation. Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 1f58cdb commit 7b4481c

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

lib/kunit/assert.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
8989
EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
9090

9191
/* Checks if `text` is a literal representing `value`, e.g. "5" and 5 */
92-
static bool is_literal(struct kunit *test, const char *text, long long value,
93-
gfp_t gfp)
92+
static bool is_literal(const char *text, long long value)
9493
{
9594
char *buffer;
9695
int len;
@@ -100,14 +99,15 @@ static bool is_literal(struct kunit *test, const char *text, long long value,
10099
if (strlen(text) != len)
101100
return false;
102101

103-
buffer = kunit_kmalloc(test, len+1, gfp);
102+
buffer = kmalloc(len+1, GFP_KERNEL);
104103
if (!buffer)
105104
return false;
106105

107106
snprintf(buffer, len+1, "%lld", value);
108107
ret = strncmp(buffer, text, len) == 0;
109108

110-
kunit_kfree(test, buffer);
109+
kfree(buffer);
110+
111111
return ret;
112112
}
113113

@@ -125,14 +125,12 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
125125
binary_assert->text->left_text,
126126
binary_assert->text->operation,
127127
binary_assert->text->right_text);
128-
if (!is_literal(stream->test, binary_assert->text->left_text,
129-
binary_assert->left_value, stream->gfp))
128+
if (!is_literal(binary_assert->text->left_text, binary_assert->left_value))
130129
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)\n",
131130
binary_assert->text->left_text,
132131
binary_assert->left_value,
133132
binary_assert->left_value);
134-
if (!is_literal(stream->test, binary_assert->text->right_text,
135-
binary_assert->right_value, stream->gfp))
133+
if (!is_literal(binary_assert->text->right_text, binary_assert->right_value))
136134
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)",
137135
binary_assert->text->right_text,
138136
binary_assert->right_value,

0 commit comments

Comments
 (0)