kasprintf() returns a pointer to dynamically allocated memory
which can be NULL upon failure. Ensure the allocation was successful
by checking the pointer validity.
Signed-off-by: Kunwu Chan <[email protected]>
Reported-by: kernel test robot <[email protected]>
Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
Suggested-by: Markus Elfring <[email protected]>
---
Changes in v3:
- Remove rc initialization
- Simply error paths by adding a new label 'fail_mem'
Changes in v2:
- Initial rc and return errno in error paths
---
arch/x86/xen/smp.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/arch/x86/xen/smp.c b/arch/x86/xen/smp.c
index 4b0d6fff88de..1fb9a1644d94 100644
--- a/arch/x86/xen/smp.c
+++ b/arch/x86/xen/smp.c
@@ -65,6 +65,8 @@ int xen_smp_intr_init(unsigned int cpu)
char *resched_name, *callfunc_name, *debug_name;
resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
+ if (!resched_name)
+ goto fail_mem;
per_cpu(xen_resched_irq, cpu).name = resched_name;
rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
cpu,
@@ -77,6 +79,8 @@ int xen_smp_intr_init(unsigned int cpu)
per_cpu(xen_resched_irq, cpu).irq = rc;
callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
+ if (!callfunc_name)
+ goto fail_mem;
per_cpu(xen_callfunc_irq, cpu).name = callfunc_name;
rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
cpu,
@@ -90,6 +94,9 @@ int xen_smp_intr_init(unsigned int cpu)
if (!xen_fifo_events) {
debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
+ if (!debug_name)
+ goto fail_mem;
+
per_cpu(xen_debug_irq, cpu).name = debug_name;
rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu,
xen_debug_interrupt,
@@ -101,6 +108,9 @@ int xen_smp_intr_init(unsigned int cpu)
}
callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
+ if (!callfunc_name)
+ goto fail_mem;
+
per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
cpu,
@@ -114,6 +124,8 @@ int xen_smp_intr_init(unsigned int cpu)
return 0;
+ fail_mem:
+ rc = -ENOMEM;
fail:
xen_smp_intr_free(cpu);
return rc;
--
2.39.2
> kasprintf() returns a pointer to dynamically allocated memory
> which can be NULL upon failure. Ensure the allocation was successful
> by checking the pointer validity.
…
> ---
> Changes in v3:
> - Remove rc initialization
> - Simply error paths by adding a new label 'fail_mem'
…
I became curious if you would like to simplify further source code places.
> +++ b/arch/x86/xen/smp.c
> @@ -65,6 +65,8 @@ int xen_smp_intr_init(unsigned int cpu)
> char *resched_name, *callfunc_name, *debug_name;
>
> resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
> + if (!resched_name)
> + goto fail_mem;
Would you like to add a blank line after such a statement?
> per_cpu(xen_resched_irq, cpu).name = resched_name;
…
Please compare with your subsequent suggestion.
…
> @@ -101,6 +108,9 @@ int xen_smp_intr_init(unsigned int cpu)
> }
>
> callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
> + if (!callfunc_name)
> + goto fail_mem;
> +
> per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
…
Regards,
Markus
> kasprintf() returns a pointer to dynamically allocated memory
> which can be NULL upon failure. Ensure the allocation was successful
> by checking the pointer validity.
How do you think about to refer to the function name
instead of the file name in the patch subject?
…
> +++ b/arch/x86/xen/smp.c
…
> @@ -114,6 +124,8 @@ int xen_smp_intr_init(unsigned int cpu)
>
> return 0;
>
> + fail_mem:
> + rc = -ENOMEM;
> fail:
> xen_smp_intr_free(cpu);
> return rc;
Is it currently preferred to start labels in the first text column?
Regards,
Markus
On 2024/1/20 22:45, Markus Elfring wrote:
>> kasprintf() returns a pointer to dynamically allocated memory
>> which can be NULL upon failure. Ensure the allocation was successful
>> by checking the pointer validity.
>
> How do you think about to refer to the function name
> instead of the file name in the patch subject?
>
The main goal is to assign a errno to rc. So use 'fail_mem is good to
understand.
>
> …
>> +++ b/arch/x86/xen/smp.c
> …
>> @@ -114,6 +124,8 @@ int xen_smp_intr_init(unsigned int cpu)
>>
>> return 0;
>>
>> + fail_mem:
>> + rc = -ENOMEM;
>> fail:
>> xen_smp_intr_free(cpu);
>> return rc;
>
> Is it currently preferred to start labels in the first text column?
Just the same as the old one. I could fix it if necessary.
>
> Regards,
> Markus
--
Thanks,
Kunwu
On 2024/1/19 18:40, Markus Elfring wrote:
>> kasprintf() returns a pointer to dynamically allocated memory
>> which can be NULL upon failure. Ensure the allocation was successful
>> by checking the pointer validity.
> …
>> ---
>> Changes in v3:
>> - Remove rc initialization
>> - Simply error paths by adding a new label 'fail_mem'
> …
>
> I became curious if you would like to simplify further source code places.
This function hasn't changed in years, so it's OK for now.
>
>
>> +++ b/arch/x86/xen/smp.c
>> @@ -65,6 +65,8 @@ int xen_smp_intr_init(unsigned int cpu)
>> char *resched_name, *callfunc_name, *debug_name;
>>
>> resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
>> + if (!resched_name)
>> + goto fail_mem;
>
> Would you like to add a blank line after such a statement?
Sure, I could do it in next patch.
>
>
>> per_cpu(xen_resched_irq, cpu).name = resched_name;
> …
>
> Please compare with your subsequent suggestion.
I’ve seend a reply.
>
> …
>> @@ -101,6 +108,9 @@ int xen_smp_intr_init(unsigned int cpu)
>> }
>>
>> callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
>> + if (!callfunc_name)
>> + goto fail_mem;
>> +
>> per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
> …
>
> Regards,
> Markus
--
Thanks,
Kunwu
>> How do you think about to refer to the function name
>> instead of the file name in the patch subject?
>>
> The main goal is to assign a errno to rc. So use 'fail_mem is good to understand.
You responded with information which can fit to the patch body.
How do you think about consequences for a subject variant like the following?
x86/xen: Add some null pointer checks in xen_smp_intr_init()
Regards,
Markus
On 19.01.24 10:49, Kunwu Chan wrote:
> kasprintf() returns a pointer to dynamically allocated memory
> which can be NULL upon failure. Ensure the allocation was successful
> by checking the pointer validity.
>
> Signed-off-by: Kunwu Chan <[email protected]>
> Reported-by: kernel test robot <[email protected]>
> Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
> Suggested-by: Markus Elfring <[email protected]>
Reviewed-by: Juergen Gross <[email protected]>
Juergen