kselftest: arm64: Add a null pointer check
authorKunwu Chan <chentao@kylinos.cn>
Tue, 23 Apr 2024 08:21:02 +0000 (16:21 +0800)
committerWill Deacon <will@kernel.org>
Sun, 28 Apr 2024 07:42:58 +0000 (08:42 +0100)
There is a 'malloc' call, which can be unsuccessful.
This patch will add the malloc failure checking
to avoid possible null dereference and give more information
about test fail reasons.

Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Link: https://lore.kernel.org/r/20240423082102.2018886-1-chentao@kylinos.cn
Signed-off-by: Will Deacon <will@kernel.org>
tools/testing/selftests/arm64/tags/tags_test.c

index 5701163460ef7ff43e9d138f941821a22e3f3d32..955f87c1170d762c55b673c7b1f56c31e316dc3e 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdint.h>
 #include <sys/prctl.h>
 #include <sys/utsname.h>
+#include "../../kselftest.h"
 
 #define SHIFT_TAG(tag)         ((uint64_t)(tag) << 56)
 #define SET_TAG(ptr, tag)      (((uint64_t)(ptr) & ~SHIFT_TAG(0xff)) | \
@@ -21,6 +22,9 @@ int main(void)
        if (prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, 0, 0, 0) == 0)
                tbi_enabled = 1;
        ptr = (struct utsname *)malloc(sizeof(*ptr));
+       if (!ptr)
+               ksft_exit_fail_msg("Failed to allocate utsname buffer\n");
+
        if (tbi_enabled)
                tag = 0x42;
        ptr = (struct utsname *)SET_TAG(ptr, tag);