summaryrefslogtreecommitdiff
path: root/lib/memcpy_kunit.c
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2021-05-17 20:16:57 -0700
committerKees Cook <keescook@chromium.org>2021-10-18 12:28:52 -0700
commit6dbefad40815a61aecbcf9b552e87ef57ab8cc7d (patch)
tree30f48300a013f5a629a0e624818689f950587426 /lib/memcpy_kunit.c
parentcaf283d040f53bc4fd81ce3d2a1a364b069cfd7d (diff)
string.h: Introduce memset_startat() for wiping trailing members and padding
A common idiom in kernel code is to wipe the contents of a structure starting from a given member. These open-coded cases are usually difficult to read and very sensitive to struct layout changes. Like memset_after(), introduce a new helper, memset_startat() that takes the target struct instance, the byte to write, and the member name where zeroing should start. Note that this doesn't zero padding preceding the target member. For those cases, memset_after() should be used on the preceding member. Cc: Steffen Klassert <steffen.klassert@secunet.com> Cc: Herbert Xu <herbert@gondor.apana.org.au> Cc: "David S. Miller" <davem@davemloft.net> Cc: Jakub Kicinski <kuba@kernel.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Francis Laniel <laniel_francis@privacyrequired.com> Cc: Vincenzo Frascino <vincenzo.frascino@arm.com> Cc: Daniel Axtens <dja@axtens.net> Cc: netdev@vger.kernel.org Signed-off-by: Kees Cook <keescook@chromium.org>
Diffstat (limited to 'lib/memcpy_kunit.c')
-rw-r--r--lib/memcpy_kunit.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/memcpy_kunit.c b/lib/memcpy_kunit.c
index 5c5b4f3221d9..62f8ffcbbaa3 100644
--- a/lib/memcpy_kunit.c
+++ b/lib/memcpy_kunit.c
@@ -222,6 +222,13 @@ static void memset_test(struct kunit *test)
0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
},
};
+ struct some_bytes startat = {
+ .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
+ 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
+ 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
+ 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
+ },
+ };
struct some_bytes dest = { };
int count, value;
u8 *ptr;
@@ -258,6 +265,10 @@ static void memset_test(struct kunit *test)
memset_after(&dest, 0x72, three);
compare("memset_after()", dest, after);
+ /* Verify memset_startat() */
+ dest = control;
+ memset_startat(&dest, 0x79, four);
+ compare("memset_startat()", dest, startat);
#undef TEST_OP
}