Skip to contentSkip to conversation

Checking pointer dereferences with SmPL

cocci In review Markus Elfring 28 participants 151 messages Apr 9, 2023 – Jul 13 1 message not in the archive

Subsystems (34)
lore
Share & tools
lore
#1 lore raw

[cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Hello,

I tried the following SmPL script out also on the source files from the software “Linux next-20230406”.

@display@
expression action, input, target;
identifier member, var;
type t;
@@
(
*t var = \( &input->member \| action(..., &input->member, ...) \);
... when != input
when any
|
*target = \( &input->member \| action(..., &input->member, ...) \);
... when != input
when any
)
*if (input == NULL || ...)

    return ...;

31 source files were found where it was tried to determine the address of a data structure member (which includes a pointer dereference)
before a null pointer check.
I imagine that such code should be reconsidered once more and improved accordingly.

How do you think about to achieve any adjustments in this design direction?

Regards,
Markus

#2 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Quoted 23 lines · Markus Elfring

Hello,

I tried the following SmPL script out also on the source files from the software “Linux next-20230406”.

@display@
expression action, input, target;
identifier member, var;
type t;
@@
(
*t var = \( &input->member \| action(..., &input->member, ...) \);
... when != input
when any
|
*target = \( &input->member \| action(..., &input->member, ...) \);
... when != input
when any
)
*if (input == NULL || ...)

    return ...;
Quoted 6 lines

31 source files were found where it was tried to determine the address of a data structure member (which includes a pointer dereference)
before a null pointer check.
I imagine that such code should be reconsidered once more and improved accordingly.

How do you think about to achieve any adjustments in this design direction?

Setting var to &input->member does not cause any immediate problem. If there is a dereference of &input->member that can happen at run time that would be a problem.

julia

#3 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Quoted 1 line

Setting var to &input->member does not cause any immediate problem.

Undefined behaviour would be involved if the expression variable “input” will actually be resolved to a null pointer, wouldn't it?

Some implementation details should be reconsidered if null pointer checks are occasionally performed a bit too late.
How do you think about to move variable assignments accordingly?

Regards,
Markus

#4 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Quoted 4 lines · Markus Elfring
Quoted 1 line

Setting var to &input->member does not cause any immediate problem.

Undefined behaviour would be involved if the expression variable “input” will actually be resolved to a null pointer, wouldn't it?

No. &input->member adds an offset to the address of input.

julia

#5 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Setting var to &input->member does not cause any immediate problem.

Undefined behaviour would be involved if the expression variable “input” will actually be resolved to a null pointer, wouldn't it?

No. &input->member adds an offset to the address of input.

It seems that we represent different development views according to the question “Does taking address of member variable through a null pointer yield undefined behavior?”.
https://stackoverflow.com/questions/25725286/does-taking-address-of-member-variable-through-a-null-pointer-yield-undefined-be
https://en.cppreference.com/w/c/language/behavior

Regards,
Markus

#6 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Quoted 11 lines · Markus Elfring
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Setting var to &input->member does not cause any immediate problem.

Undefined behaviour would be involved if the expression variable “input” will actually be resolved to a null pointer, wouldn't it?

No. &input->member adds an offset to the address of input.

It seems that we represent different development views according to the question “Does taking address of member variable through a null pointer yield undefined behavior?”.
https://stackoverflow.com/questions/25725286/does-taking-address-of-member-variable-through-a-null-pointer-yield-undefined-be
https://en.cppreference.com/w/c/language/behavior

The statement:

"... &((*ptr).second) which dereferences an object instance pointer"

is not correct. &((*ptr).second) does not dereference *ptr any more than &x deferences x. The semantics of & doesn't work like that.

julia

Quoted 5 lines

Regards,
Markus

#7 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Quoted 9 lines
Quoted 2 lines

https://stackoverflow.com/questions/25725286/does-taking-address-of-member-variable-through-a-null-pointer-yield-undefined-be
https://en.cppreference.com/w/c/language/behavior

The statement:

"... &((*ptr).second) which dereferences an object instance pointer"

is not correct. &((*ptr).second) does not dereference *ptr any more than &x deferences x. The semantics of & doesn't work like that.

Does the usage of the operator “member access through pointer” (arrow) mean also a dereference in comparison to the asterisk operator in the C programming language?

Regards,
Markus

A message that is not in the archive (sent off-list or not yet received): alpine.DEB.2.22.394.2304101415040.2875@hadrien

#9 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Quoted 4 lines
Quoted 2 lines

Does the usage of the operator “member access through pointer” (arrow) mean also a dereference in comparison to the asterisk operator in the C programming language?

Not when there is a & in front.

Is such an interpretation of programming language specification questionable?

Quoted 1 line

you can just look at the generated assembly code to see that.

I would like to be more sure about the expected software behaviour according to programming language standards (than a concrete output from a selected compiler version).

Quoted 1 line

The goal of & is to take the address of something,

Usually, yes.

Should null pointers be excluded from “something”?

Quoted 1 line

not to evaluate the thing.

Does such an expectation fit better to the operator “sizeof”?

Regards,
Markus

#10 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

In reply to a message not in the archive
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 2 lines

https://stackoverflow.com/questions/25725286/does-taking-address-of-member-variable-through-a-null-pointer-yield-undefined-be
https://en.cppreference.com/w/c/language/behavior

The statement:

"... &((*ptr).second) which dereferences an object instance pointer"

is not correct. &((*ptr).second) does not dereference *ptr any more than &x deferences x. The semantics of & doesn't work like that.

Quoted 6 lines
Quoted 2 lines

Does the usage of the operator “member access through pointer” (arrow) mean also a dereference in comparison to the asterisk operator in the C programming language?

Not when there is a & in front. you can just look at the generated assembly code to see that. The goal of & is to take the address of something, not to evaluate the thing.

Would you like to take another look at recent responses by David Svoboda?
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers?focusedCommentId=405504153#comment-405504153

Regards,
Markus

#11 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Quoted 13 lines · Markus Elfring
Quoted 13 lines
Quoted 12 lines
Quoted 9 lines
Quoted 2 lines

https://stackoverflow.com/questions/25725286/does-taking-address-of-member-variable-through-a-null-pointer-yield-undefined-be
https://en.cppreference.com/w/c/language/behavior

The statement:

"... &((*ptr).second) which dereferences an object instance pointer"

is not correct. &((*ptr).second) does not dereference *ptr any more than &x deferences x. The semantics of & doesn't work like that.

Does the usage of the operator “member access through pointer” (arrow) mean also a dereference in comparison to the asterisk operator in the C programming language?

Quoted 6 lines
Quoted 3 lines

Not when there is a & in front. you can just look at the generated assembly code to see that. The goal of & is to take the address of something, not to evaluate the thing.

Would you like to take another look at recent responses by David Svoboda?
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers?focusedCommentId=405504153#comment-405504153

His previous comment says that the standard doesn't mention &a->b so it is a problem. He is surely more of an expert on the C standard than I am.

julia

#12 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Quoted 21 lines · Julia Lawall
Quoted 17 lines · Markus Elfring
Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

https://stackoverflow.com/questions/25725286/does-taking-address-of-member-variable-through-a-null-pointer-yield-undefined-be
https://en.cppreference.com/w/c/language/behavior

The statement:

Quoted 7 lines
Quoted 4 lines

"... &((*ptr).second) which dereferences an object instance pointer"

is not correct. &((*ptr).second) does not dereference *ptr any more than &x deferences x. The semantics of & doesn't work like that.

Does the usage of the operator “member access through pointer” (arrow) mean also a dereference in comparison to the asterisk operator in the C programming language?

Not when there is a & in front. you can just look at the generated assembly code to see that. The goal of & is to take the address of something, not to evaluate the thing.

Quoted 5 lines
Quoted 2 lines

Would you like to take another look at recent responses by David Svoboda?
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers?focusedCommentId=405504153#comment-405504153

His previous comment says that the standard doesn't mention &a->b so it is a problem. He is surely more of an expert on the C standard than I am.

In the linux-kernel we do a lot of things that are not in the C standard. The linux-kernel is written with specific versions of GCC and Clang in mind. It's not expected that other compilers will be able to compile the kernel.

So &a->b is just fine in the linux kernel.

regards,
dan carpenter

#13 lore raw

Re: [cocci] Reconsidering pointer dereferences before null pointer checks (with SmPL)

Quoted 1 line

So &a->b is just fine in the linux kernel.

I find it occasionally nicer and safe to use such an expression only after special condition checks.
How do you think about to move any misplaced code parts to other places?

Regards,
Markus

#14 lore raw

cover drm/amd: Adjustments for three function implementations

In reply to Markus Elfring

Date: Tue, 11 Apr 2023 14:36:36 +0200

Some update suggestions were taken into account
from static source code analysis.

Markus Elfring (5)
amdgpu: Move a variable assignment behind a null pointer check in amdgpu_ras_interrupt_dispatch()
display: Move three variable assignments behind condition checks in trigger_hotplug()
display: Delete three unnecessary variable initialisations in trigger_hotplug()
display: Delete a redundant statement in trigger_hotplug()
display: Move an expression into a return statement in dcn201_link_encoder_create()

 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c       |  3 ++-
 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c | 19 ++++++++++---------
 .../amd/display/dc/dcn201/dcn201_resource.c   |  4 +---
 3 files changed, 13 insertions(+), 13 deletions(-)

--
2.40.0

#15 lore raw

PATCH 1/5 drm/amdgpu: Move a variable assignment behind a null pointer check in amdgpu_ras_interrupt_dispatch()

Date: Tue, 11 Apr 2023 10:52:48 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “amdgpu_ras_interrupt_dispatch”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “data” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes c030f2e4166c3f5597c7e7a70bcd9ab383695de4 ("drm/amdgpu: add amdgpu_ras.c to support ras (v2)")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +2 -1
index 4069bce9479f..a920c7888d07 100644
@@ -1730,11 +1730,12 @@ int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
 		struct ras_dispatch_if *info)
 {
 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
-	struct ras_ih_data *data = &obj->ih_data;
+	struct ras_ih_data *data;
 
 	if (!obj)
 		return -EINVAL;
 
+	data = &obj->ih_data;
 	if (data->inuse == 0)
 		return 0;
 

--
2.40.0

#16 lore raw

Re: [PATCH 1/5] drm/amdgpu: Move a variable assignment behind a null pointer check in amdgpu_ras_interrupt_dispatch()

Am 2023-04-11 um 09:42 schrieb Markus Elfring:

Quoted 22 lines

Date: Tue, 11 Apr 2023 10:52:48 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “amdgpu_ras_interrupt_dispatch”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “data” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes c030f2e4166c3f5597c7e7a70bcd9ab383695de4 ("drm/amdgpu: add amdgpu_ras.c to support ras (v2)")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
index 4069bce9479f..a920c7888d07 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev, · line 1733

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
@@ -1730,11 +1730,12 @@ int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
  		struct ras_dispatch_if *info)
  {
  	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
-	struct ras_ih_data *data = &obj->ih_data;
+	struct ras_ih_data *data;

I'm curious, this only takes the address of obj->ih_data. It doesn't dereference the pointer until after the !obj check below. How is this undefined behaviour? Is this about the compiler being free to reorder stuff for optimization, unaware of the dependency? Is there a link to an explanation that could be added to the commit description?

Thanks,
  Felix

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
  	if (!obj)
  		return -EINVAL;
 
+	data = &obj->ih_data;
  	if (data->inuse == 0)
  		return 0;
 
--

2.40.0

#17 lore raw

Re: [PATCH 1/5] drm/amdgpu: Move a variable assignment behind a null pointer check in amdgpu_ras_interrupt_dispatch()

Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev, · line 1733

Quoted 7 lines

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
@@ -1730,11 +1730,12 @@ int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
           struct ras_dispatch_if *info)
   {
       struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
-    struct ras_ih_data *data = &obj->ih_data;
+    struct ras_ih_data *data;

I'm curious, this only takes the address of obj->ih_data.

Even if a null pointer would accidentally be returned by a call of the function “amdgpu_ras_find_obj”?
https://elixir.bootlin.com/linux/v6.3-rc6/source/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c#L618

Quoted 1 line

It doesn't dereference the pointer until after the !obj check below.

Does the used arrow operator indicate a pointer dereference?

Quoted 1 line

How is this undefined behaviour?

I guess that another information source can be helpful for such an issue.
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers?focusedCommentId=405504153#comment-405504153

Regards,
Markus

#18 lore raw

Re: [PATCH 1/5] drm/amdgpu: Move a variable assignment behind a null pointer check in amdgpu_ras_interrupt_dispatch()

In reply to Markus Elfring
Quoted 25 lines

Date: Tue, 11 Apr 2023 10:52:48 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “amdgpu_ras_interrupt_dispatch”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “data” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes c030f2e4166c3f5597c7e7a70bcd9ab383695de4 ("drm/amdgpu: add amdgpu_ras.c to support ras (v2)")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
index 4069bce9479f..a920c7888d07 100644
@@ -1730,11 +1730,12 @@ int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
 		struct ras_dispatch_if *info)
 {
 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev, · line 1741

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +2 -1
-	struct ras_ih_data *data = &obj->ih_data;
+	struct ras_ih_data *data;
 
 	if (!obj)
 		return -EINVAL;
 
+	data = &obj->ih_data;
 	if (data->inuse == 0)
 		return 0;
 

I would like to point out that another software adjustment got the desired development attention (on 2024-05-11).

See also:
Commit 4c11d30c95576937c6c35e6f29884761f2dddb43 ("drm/amdgpu:
Fix the null pointer dereference to ras_manager")

Regards,
Markus

#19 lore raw

PATCH 2/5 drm/amd/display: Move three variable assignments behind condition checks in trigger_hotplug()

In reply to Markus Elfring

Date: Tue, 11 Apr 2023 11:39:02 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “trigger_hotplug”.

Thus avoid the risk for undefined behaviour by moving the assignment for three local variables behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 6f77b2ac628073f647041a92b36c824ae3aef16e ("drm/amd/display: Add connector HPD trigger debugfs entry")
---
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c  | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +7 -3
index 827fcb4fb3b3..b3cfd7dfbb28 100644
@@ -1205,10 +1205,10 @@ static ssize_t trigger_hotplug(struct file *f, const char __user *buf,
 							size_t size, loff_t *pos)
 {
 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
-	struct drm_connector *connector = &aconnector->base;
+	struct drm_connector *connector;
 	struct dc_link *link = NULL;
-	struct drm_device *dev = connector->dev;
-	struct amdgpu_device *adev = drm_to_adev(dev);
+	struct drm_device *dev;
+	struct amdgpu_device *adev;
 	enum dc_connection_type new_connection_type = dc_connection_none;
 	char *wr_buf = NULL;
 	uint32_t wr_buf_size = 42;
@@ -1253,12 +1253,16 @@ static ssize_t trigger_hotplug(struct file *f, const char __user *buf,
 		return -EINVAL;
 	}
 
+	connector = &aconnector->base;
+	dev = connector->dev;
+
 	if (param[0] == 1) {
 
 		if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type) &&
 			new_connection_type != dc_connection_none)
 			goto unlock;
 
+		adev = drm_to_adev(dev);
 		mutex_lock(&adev->dm.dc_lock);
 		ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
 		mutex_unlock(&adev->dm.dc_lock);

--
2.40.0

maintainer
#20 lore raw

Re: [PATCH 2/5] drm/amd/display: Move three variable assignments behind condition checks in trigger_hotplug()

Am 11.04.23 um 15:43 schrieb Markus Elfring:

Quoted 8 lines

Date: Tue, 11 Apr 2023 11:39:02 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “trigger_hotplug”.

Thus avoid the risk for undefined behaviour by moving the assignment for three local variables behind some condition checks.

It might be that the NULL check doesn't make sense in the first place, but since I'm not an expert for this code I can't fully judge.

On the other hand the patches clearly look like nice cleanups to me, so feel free to add an Acked-by: Christian König <[email protected]> to the series.

Thanks,
Christian.

Quoted 47 lines

This issue was detected by using the Coccinelle software.

Fixes 6f77b2ac628073f647041a92b36c824ae3aef16e ("drm/amd/display: Add connector HPD trigger debugfs entry")
---
  .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c  | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +7 -4
index 827fcb4fb3b3..b3cfd7dfbb28 100644
@@ -1205,10 +1205,10 @@ static ssize_t trigger_hotplug(struct file *f, const char __user *buf,
  							size_t size, loff_t *pos)
  {
  	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
-	struct drm_connector *connector = &aconnector->base;
+	struct drm_connector *connector;
  	struct dc_link *link = NULL;
-	struct drm_device *dev = connector->dev;
-	struct amdgpu_device *adev = drm_to_adev(dev);
+	struct drm_device *dev;
+	struct amdgpu_device *adev;
  	enum dc_connection_type new_connection_type = dc_connection_none;
  	char *wr_buf = NULL;
  	uint32_t wr_buf_size = 42;
@@ -1253,12 +1253,16 @@ static ssize_t trigger_hotplug(struct file *f, const char __user *buf,
  		return -EINVAL;
  	}
 
+	connector = &aconnector->base;
+	dev = connector->dev;
+
  	if (param[0] == 1) {
 
  		if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type) &&
  			new_connection_type != dc_connection_none)
  			goto unlock;
 
+		adev = drm_to_adev(dev);
  		mutex_lock(&adev->dm.dc_lock);
  		ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
  		mutex_unlock(&adev->dm.dc_lock);
--

2.40.0

#21 lore raw

Re: [PATCH 2/5] drm/amd/display: Move three variable assignments behind condition checks in trigger_hotplug()

Quoted 8 lines
Quoted 6 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “trigger_hotplug”.

Thus avoid the risk for undefined behaviour by moving the assignment for three local variables behind some condition checks.

It might be that the NULL check doesn't make sense in the first place, but since I'm not an expert for this code I can't fully judge.

Will the source code and patch review evolve any more?

Quoted 1 line

On the other hand the patches clearly look like nice cleanups to me, so feel free to add an Acked-by: Christian König <[email protected]> to the series.

Will such a positive feedback trigger any further collateral evolution?

Regards,
Markus

#22 lore raw

PATCH 3/5 drm/amd/display: Delete three unnecessary variable initialisations in trigger_hotplug()

In reply to Markus Elfring

Date: Tue, 11 Apr 2023 12:34:42 +0200

The variables “link”, “wr_buf” and “ret” will eventually be set to appropriate values a bit later.
Thus omit the explicit initialisation at the beginning.

---
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +3 -3
index b3cfd7dfbb28..a37d23a13d7b 100644
@@ -1206,16 +1206,16 @@ static ssize_t trigger_hotplug(struct file *f, const char __user *buf,
 {
 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
 	struct drm_connector *connector;
-	struct dc_link *link = NULL;
+	struct dc_link *link;
 	struct drm_device *dev;
 	struct amdgpu_device *adev;
 	enum dc_connection_type new_connection_type = dc_connection_none;
-	char *wr_buf = NULL;
+	char *wr_buf;
 	uint32_t wr_buf_size = 42;
 	int max_param_num = 1;
 	long param[1] = {0};
 	uint8_t param_nums = 0;
-	bool ret = false;
+	bool ret;
 
 	if (!aconnector || !aconnector->dc_link)
 		return -EINVAL;

--
2.40.0

#23 lore raw

PATCH 4/5 drm/amd/display: Delete a redundant statement in trigger_hotplug()

In reply to Markus Elfring

Date: Tue, 11 Apr 2023 13:26:35 +0200

An immediate return is performed by this function after a null pointer was detected for the member “dc_link” in the data
structure “amdgpu_dm_connector”.
This check was repeated within one if branch.

Thus omit such a redundant statement.

---
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +0 -3
index a37d23a13d7b..4805a482dc49 100644
@@ -1278,9 +1278,6 @@ static ssize_t trigger_hotplug(struct file *f, const char __user *buf,
 
 		drm_kms_helper_connector_hotplug_event(connector);
 	} else if (param[0] == 0) {
-		if (!aconnector->dc_link)
-			goto unlock;
-
 		link = aconnector->dc_link;
 
 		if (link->local_sink) {

--
2.40.0

Am 11.04.23 um 15:36 schrieb Markus Elfring:

Quoted 17 lines

Date: Tue, 11 Apr 2023 14:36:36 +0200

Some update suggestions were taken into account
from static source code analysis.

Markus Elfring (5)
amdgpu: Move a variable assignment behind a null pointer check in amdgpu_ras_interrupt_dispatch()
display: Move three variable assignments behind condition checks in trigger_hotplug()
display: Delete three unnecessary variable initialisations in trigger_hotplug()
display: Delete a redundant statement in trigger_hotplug()
display: Move an expression into a return statement in dcn201_link_encoder_create()

 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c       |  3 ++-
 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c | 19 ++++++++++---------
 .../amd/display/dc/dcn201/dcn201_resource.c   |  4 +---
 3 files changed, 13 insertions(+), 13 deletions(-)
#24 lore raw

PATCH 5/5 drm/amd/display: Move an expression into a return statement in dcn201_link_encoder_create()

In reply to Markus Elfring

Date: Tue, 11 Apr 2023 14:04:57 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “dcn201_link_encoder_create”.

Thus avoid the risk for undefined behaviour by moving the usage of an expression into a return statement.

This issue was detected by using the Coccinelle software.

Fixes 3f68c01be9a2227de1e190317fe34a6fb835a094 ("drm/amd/display: add cyan_skillfish display support")
---
 drivers/gpu/drm/amd/display/dc/dcn201/dcn201_resource.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/gpu/drm/amd/display/dc/dcn201/dcn201_resource.c +1 -3
index 6ea70da28aaa..a1b44c7bd34b 100644
@@ -791,7 +791,6 @@ static struct link_encoder *dcn201_link_encoder_create(
 {
 	struct dcn20_link_encoder *enc20 =
 		kzalloc(sizeof(struct dcn20_link_encoder), GFP_ATOMIC);
-	struct dcn10_link_encoder *enc10 = &enc20->enc10;
 
 	if (!enc20)
 		return NULL;
@@ -804,8 +803,7 @@ static struct link_encoder *dcn201_link_encoder_create(
 			&link_enc_hpd_regs[enc_init_data->hpd_source],
 			&le_shift,
 			&le_mask);
-
-	return &enc10->base;
+	return &enc20->enc10.base;
 }
 
 static struct clock_source *dcn201_clock_source_create(

--
2.40.0

#25 lore raw

Re: [PATCH 0/5] drm/amd: Adjustments for three function implementations

In reply to Markus Elfring
Quoted 12 lines

Date: Tue, 11 Apr 2023 14:36:36 +0200

Some update suggestions were taken into account
from static source code analysis.

Markus Elfring (5)
amdgpu: Move a variable assignment behind a null pointer check in amdgpu_ras_interrupt_dispatch()
display: Move three variable assignments behind condition checks in trigger_hotplug()
display: Delete three unnecessary variable initialisations in trigger_hotplug()
display: Delete a redundant statement in trigger_hotplug()
display: Move an expression into a return statement in dcn201_link_encoder_create()

Quoted 4 lines
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c       |  3 ++-
 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c | 19 ++++++++++---------
 .../amd/display/dc/dcn201/dcn201_resource.c   |  4 +---
 3 files changed, 13 insertions(+), 13 deletions(-)

Is this patch series still in review queues?

See also:
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2023-04/msg00034.html

Regards,
Markus

#26 lore raw

PATCH 1/1 drm/msm/dpu: Delete a variable initialisation before a null pointer check in two functions

In reply to Markus Elfring

Date: Tue, 11 Apr 2023 18:24:24 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

This issue was detected by using the Coccinelle software.

Fixes 25fdd5933e4c0f5fe2ea5cd59994f8ac5fbe90ef ("drm/msm: Add SDM845 DPU support")
---
 drivers/gpu/drm/msm/disp/dpu1/dpu_hw_pingpong.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_hw_pingpong.c +2 -2
index 0fcad9760b6f..870ab3ebbc94 100644
@@ -176,7 +176,7 @@ static int dpu_hw_pp_enable_te(struct dpu_hw_pingpong *pp, bool enable)
 static int dpu_hw_pp_connect_external_te(struct dpu_hw_pingpong *pp,
 		bool enable_external_te)
 {
-	struct dpu_hw_blk_reg_map *c = &pp->hw;
+	struct dpu_hw_blk_reg_map *c;
 	u32 cfg;
 	int orig;
 
@@ -221,7 +221,7 @@ static int dpu_hw_pp_get_vsync_info(struct dpu_hw_pingpong *pp,
 
 static u32 dpu_hw_pp_get_line_count(struct dpu_hw_pingpong *pp)
 {
-	struct dpu_hw_blk_reg_map *c = &pp->hw;
+	struct dpu_hw_blk_reg_map *c;
 	u32 height, init;
 	u32 line = 0xFFFF;
 

--
2.40.0

#27 lore raw

Re: [PATCH] drm/msm/dpu: Delete a variable initialisation before a null pointer check in two functions

Quoted 6 lines · Markus Elfring

Date: Tue, 11 Apr 2023 18:24:24 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

This issue was detected by using the Coccinelle software.

Fixes 25fdd5933e4c0f5fe2ea5cd59994f8ac5fbe90ef ("drm/msm: Add SDM845 DPU support")

Plese follow the format for the Fixes tags and limit the hash to 12 chars. Proper tag:

Fixes 25fdd5933e4c ("drm/msm: Add SDM845 DPU support")

Other than that LGTM.

Quoted 30 lines
---
  drivers/gpu/drm/msm/disp/dpu1/dpu_hw_pingpong.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_hw_pingpong.c +2 -3
index 0fcad9760b6f..870ab3ebbc94 100644
@@ -176,7 +176,7 @@ static int dpu_hw_pp_enable_te(struct dpu_hw_pingpong *pp, bool enable)
  static int dpu_hw_pp_connect_external_te(struct dpu_hw_pingpong *pp,
  		bool enable_external_te)
  {
-	struct dpu_hw_blk_reg_map *c = &pp->hw;
+	struct dpu_hw_blk_reg_map *c;
  	u32 cfg;
  	int orig;
 
@@ -221,7 +221,7 @@ static int dpu_hw_pp_get_vsync_info(struct dpu_hw_pingpong *pp,
 
  static u32 dpu_hw_pp_get_line_count(struct dpu_hw_pingpong *pp)
  {
-	struct dpu_hw_blk_reg_map *c = &pp->hw;
+	struct dpu_hw_blk_reg_map *c;
  	u32 height, init;
  	u32 line = 0xFFFF;
 
--

2.40.0

Signature · 2 lines
With best wishes
Dmitry
Reviewed-by on v1 1/1
#28 lore raw

Re: [PATCH] drm/msm/dpu: Delete a variable initialisation before a null pointer check in two functions

In reply to Markus Elfring
Quoted 6 lines · Markus Elfring

Date: Tue, 11 Apr 2023 18:24:24 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

This issue was detected by using the Coccinelle software.

Fixes 25fdd5933e4c0f5fe2ea5cd59994f8ac5fbe90ef ("drm/msm: Add SDM845 DPU support")
Fixes 25fdd5933e4c ("drm/msm: Add SDM845 DPU support")

We usually have 12 chars of the hash. Other than that,

Quoted 30 lines
---
  drivers/gpu/drm/msm/disp/dpu1/dpu_hw_pingpong.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_hw_pingpong.c +2 -3
index 0fcad9760b6f..870ab3ebbc94 100644
@@ -176,7 +176,7 @@ static int dpu_hw_pp_enable_te(struct dpu_hw_pingpong *pp, bool enable)
  static int dpu_hw_pp_connect_external_te(struct dpu_hw_pingpong *pp,
  		bool enable_external_te)
  {
-	struct dpu_hw_blk_reg_map *c = &pp->hw;
+	struct dpu_hw_blk_reg_map *c;
  	u32 cfg;
  	int orig;
 
@@ -221,7 +221,7 @@ static int dpu_hw_pp_get_vsync_info(struct dpu_hw_pingpong *pp,
 
  static u32 dpu_hw_pp_get_line_count(struct dpu_hw_pingpong *pp)
  {
-	struct dpu_hw_blk_reg_map *c = &pp->hw;
+	struct dpu_hw_blk_reg_map *c;
  	u32 height, init;
  	u32 line = 0xFFFF;
 
--

2.40.0

#29 lore raw

PATCH RESEND 1/1 drm/msm/dpu: Delete a variable initialisation before a null pointer check in two functions

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 11 Apr 2023 18:24:24 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

This issue was detected by using the Coccinelle software.

Fixes 25fdd5933e4c ("drm/msm: Add SDM845 DPU support")
---
 drivers/gpu/drm/msm/disp/dpu1/dpu_hw_pingpong.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_hw_pingpong.c +2 -2
index 0fcad9760b6f..870ab3ebbc94 100644
@@ -176,7 +176,7 @@ static int dpu_hw_pp_enable_te(struct dpu_hw_pingpong *pp, bool enable)
 static int dpu_hw_pp_connect_external_te(struct dpu_hw_pingpong *pp,
 		bool enable_external_te)
 {
-	struct dpu_hw_blk_reg_map *c = &pp->hw;
+	struct dpu_hw_blk_reg_map *c;
 	u32 cfg;
 	int orig;
 
@@ -221,7 +221,7 @@ static int dpu_hw_pp_get_vsync_info(struct dpu_hw_pingpong *pp,
 
 static u32 dpu_hw_pp_get_line_count(struct dpu_hw_pingpong *pp)
 {
-	struct dpu_hw_blk_reg_map *c = &pp->hw;
+	struct dpu_hw_blk_reg_map *c;
 	u32 height, init;
 	u32 line = 0xFFFF;
 

--
2.40.0

#30 lore raw

Re: [PATCH RESEND] drm/msm/dpu: Delete a variable initialisation before a null pointer check in two functions

Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 11 Apr 2023 18:24:24 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Quoted 5 lines

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

This issue was detected by using the Coccinelle software.

Please don't send resends and/or new iterations in response to your previous patchsets. Otherwise they have a pretty high chance to be ignored by the maintainers. Use a fresh git-send-email command to send new patchset.

Quoted 32 lines
Fixes 25fdd5933e4c ("drm/msm: Add SDM845 DPU support")
---
 drivers/gpu/drm/msm/disp/dpu1/dpu_hw_pingpong.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_hw_pingpong.c +2 -3
index 0fcad9760b6f..870ab3ebbc94 100644
@@ -176,7 +176,7 @@ static int dpu_hw_pp_enable_te(struct dpu_hw_pingpong *pp, bool enable)
 static int dpu_hw_pp_connect_external_te(struct dpu_hw_pingpong *pp,
 		bool enable_external_te)
 {
-	struct dpu_hw_blk_reg_map *c = &pp->hw;
+	struct dpu_hw_blk_reg_map *c;
 	u32 cfg;
 	int orig;
 
@@ -221,7 +221,7 @@ static int dpu_hw_pp_get_vsync_info(struct dpu_hw_pingpong *pp,
 
 static u32 dpu_hw_pp_get_line_count(struct dpu_hw_pingpong *pp)
 {
-	struct dpu_hw_blk_reg_map *c = &pp->hw;
+	struct dpu_hw_blk_reg_map *c;
 	u32 height, init;
 	u32 line = 0xFFFF;
 
--

2.40.0

Signature · 2 lines
With best wishes
Dmitry
#31 lore raw

Re: [PATCH RESEND] drm/msm/dpu: Delete a variable initialisation before a null pointer check in two functions

Quoted 11 lines · Dmitry Baryshkov
Quoted 10 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 11 Apr 2023 18:24:24 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

There is no undefined behavior here.

Quoted 10 lines
Quoted 2 lines

This issue was detected by using the Coccinelle software.

Please don't send resends and/or new iterations in response to your previous patchsets. Otherwise they have a pretty high chance to be ignored by the maintainers. Use a fresh git-send-email command to send new patchset.

Quoted 2 lines
Fixes 25fdd5933e4c ("drm/msm: Add SDM845 DPU support")

Remove the Fixes tag. This patch is fine as a clean up.

Quoted 1 line
Quoted 1 line

regards,
dan carpenter

#32 lore raw

Re: [RESEND] drm/msm/dpu: Delete a variable initialisation before a null pointer check in two functions

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

There is no undefined behavior here.

Will any software development concerns evolve further also according to undesirable null pointer dereferences?
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers

Regards,
Markus

#33 lore raw

Re: [RESEND] drm/msm/dpu: Delete a variable initialisation before a null pointer check in two functions

Quoted 4 lines · Markus Elfring
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

There is no undefined behavior here.

Will any software development concerns evolve further also according to undesirable null pointer dereferences?
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers

It's not a NULL pointer dereference. It's just pointer math. It was a common way to implement offsetof() before we had a builtin for that.

samples/bpf/test_lru_dist.c

# define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)

regards,
dan carpenter

#34 lore raw

Re: [RFC] Clarification for “undefined behaviour”?

In reply to Dan Carpenter
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

There is no undefined behavior here.

Is there a need to improve the wording precision?

There are words which denote a special meaning according to aspects of the programming language “C”.
https://en.cppreference.com/w/c/language/behavior

Dereferences of null pointers are treated in special ways.
The system might be configurable to collaborate also with data accesses together with the address “zero”.
Would you like to distinguish supported software functionality any further?

Regards,
Markus

#35 lore raw

Re: [RFC] Clarification for “undefined behaviour”?

Quoted 9 lines · Markus Elfring
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

There is no undefined behavior here.

Quoted 7 lines

Is there a need to improve the wording precision?

There are words which denote a special meaning according to aspects of the programming language “C”.
https://en.cppreference.com/w/c/language/behavior

Dereferences of null pointers are treated in special ways.

This not a dereference. It's just pointer math.

regards,
dan carpenter

#36 lore raw

Re: [RFC] Clarification for “undefined behaviour”?

Quoted 3 lines
Quoted 1 line

Dereferences of null pointers are treated in special ways.

This not a dereference. It's just pointer math.

Do you prefer the wording “member access through pointer” occasionally?
https://en.cppreference.com/w/c/language/operator_member_access

How do you tend to describe (and detect) null pointer dereferences so far?
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers#EXP34C.Donotdereferencenullpointers-AutomatedDetection

Regards,
Markus

#37 lore raw

Re: [RFC] Clarification for “undefined behaviour”?

In reply to Dan Carpenter
Quoted 12 lines · On Wed, 5 Mar 2025 11:51:59 +0300 Dan Carpenter <[email protected]>…
Quoted 11 lines · Markus Elfring
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

There is no undefined behavior here.

Is there a need to improve the wording precision?

Quoted 7 lines
Quoted 5 lines

There are words which denote a special meaning according to aspects of the programming language “C”.
https://en.cppreference.com/w/c/language/behavior

Dereferences of null pointers are treated in special ways.

This not a dereference. It's just pointer math.

And the 'fun' starts because NULL isn't required to use the all-zero bit pattern.
Regardless of the bit-pattern, things like (void *)(1 - 1) are valid NULL pointers.

Of course, while C allows this, I doubt NULL has ever been other than 0. (It was 0 on a system I used many years ago where the O/S invalid pointer was ~0.)

I know Clang has started warning about arithmetic on NULL. I wonder when it is going to start warning about memset(p, 0, sz) for anything that contains a pointer - equally invalid.

David

#38 lore raw

Re: [RFC] Clarification for “undefined behaviour”?

Quoted 23 lines · David Laight
Quoted 12 lines · On Wed, 5 Mar 2025 11:51:59 +0300 Dan Carpenter <[email protected]>…
Quoted 11 lines · Markus Elfring
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.

Thus avoid the risk for undefined behaviour by removing extra initialisations for the variable “c” (also because it was already reassigned with the same value behind this pointer check).

There is no undefined behavior here.

Is there a need to improve the wording precision?

Quoted 7 lines
Quoted 5 lines

There are words which denote a special meaning according to aspects of the programming language “C”.
https://en.cppreference.com/w/c/language/behavior

Dereferences of null pointers are treated in special ways.

This not a dereference. It's just pointer math.

Quoted 8 lines

And the 'fun' starts because NULL isn't required to use the all-zero bit pattern.
Regardless of the bit-pattern, things like (void *)(1 - 1) are valid NULL pointers.

Of course, while C allows this, I doubt NULL has ever been other than 0. (It was 0 on a system I used many years ago where the O/S invalid pointer was ~0.)

Kernel style guidelines don't even allow if (p == NULL) so we would be screwed. :P

Quoted 2 lines

I know Clang has started warning about arithmetic on NULL.

Huh. You're right.

$ clang -Weverything test.c
test.c:13:22: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
   13 |         printf("%p\n", NULL + 1);
      |                        ~~~~ ^
test.c:13:22: warning: arithmetic on a pointer to void is a GNU extension [-Wgnu-pointer-arith]
   13 |         printf("%p\n", NULL + 1);
      |                        ~~~~ ^
test.c:11:14: warning: unused parameter 'argc' [-Wunused-parameter]
   11 | int main(int argc, char *argv[])
      |              ^
test.c:11:26: warning: unused parameter 'argv' [-Wunused-parameter]
   11 | int main(int argc, char *argv[])
      |                          ^
test.c:13:17: warning: unsafe pointer arithmetic [-Wunsafe-buffer-usage]
   13 |         printf("%p\n", NULL + 1);
      |                        ^~~~
/usr/lib/llvm-19/lib/clang/19/include/__stddef_null.h:26:14: note: expanded from macro 'NULL'
   26 | #define NULL ((void*)0)
      |              ^~~~~~~~~~
5 warnings generated.

Well, that's stupid.

regards,
dan carpenter

#39 lore raw

Re: [RFC] Clarification for “undefined behaviour”?

Quoted 1 line · On Wed, 5 Mar 2025 17:30:28 +0300 Dan Carpenter <[email protected]>…

On Wed, Mar 05, 2025 at 02:17:32PM +0000, David Laight wrote:

...

Quoted 11 lines
Quoted 8 lines

And the 'fun' starts because NULL isn't required to use the all-zero bit pattern.
Regardless of the bit-pattern, things like (void *)(1 - 1) are valid NULL pointers.

Of course, while C allows this, I doubt NULL has ever been other than 0. (It was 0 on a system I used many years ago where the O/S invalid pointer was ~0.)

Kernel style guidelines don't even allow if (p == NULL) so we would be screwed. :P

Doesn't matter:

	if (!p) ...
	if (p == 0) ...
	if (p == (void *)0) ...
	if (p == NULL) ...
	if (p == (void *)(constant integer expression with value 0)) ...
and the equivalent assignments all behave the same regardless of the
bit-pattern use for NULL.
So:
	union { long l; void *p; } lpu;
	lpu.p = 0;
	return lpu.l;

Returns ABI (implementation) defined constant value.
I think the only requirement is that it can never be the address of a valid variable.

David

#40 lore raw

PATCH 1/1 qed: Move a variable assignment behind a null pointer check in two functions

In reply to Markus Elfring

Date: Tue, 11 Apr 2023 19:33:53 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “qed_ll2_rxq_completion” and “qed_ll2_txq_completion”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variables “p_rx” and “p_tx” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 0a7fb11c23c0fb8f5ad37f285f40348f1ab9ccbd ("qed: Add Light L2 support")
---
 drivers/net/ethernet/qlogic/qed/qed_ll2.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ethernet/qlogic/qed/qed_ll2.c +4 -2
index 717a0b3f89bd..941c02fccaaf 100644
@@ -346,7 +346,7 @@ static void qed_ll2_txq_flush(struct qed_hwfn *p_hwfn, u8 connection_handle)
 static int qed_ll2_txq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
 {
 	struct qed_ll2_info *p_ll2_conn = p_cookie;
-	struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
+	struct qed_ll2_tx_queue *p_tx;
 	u16 new_idx = 0, num_bds = 0, num_bds_in_packet = 0;
 	struct qed_ll2_tx_packet *p_pkt;
 	bool b_last_frag = false;
@@ -356,6 +356,7 @@ static int qed_ll2_txq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
 	if (!p_ll2_conn)
 		return rc;
 
+	p_tx = &p_ll2_conn->tx_queue;
 	spin_lock_irqsave(&p_tx->lock, flags);
 	if (p_tx->b_completing_packet) {
 		rc = -EBUSY;
@@ -523,7 +524,7 @@ qed_ll2_rxq_handle_completion(struct qed_hwfn *p_hwfn,
 static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie)
 {
 	struct qed_ll2_info *p_ll2_conn = (struct qed_ll2_info *)cookie;
-	struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
+	struct qed_ll2_rx_queue *p_rx;
 	union core_rx_cqe_union *cqe = NULL;
 	u16 cq_new_idx = 0, cq_old_idx = 0;
 	unsigned long flags = 0;
@@ -532,6 +533,7 @@ static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie)
 	if (!p_ll2_conn)
 		return rc;
 
+	p_rx = &p_ll2_conn->rx_queue;
 	spin_lock_irqsave(&p_rx->lock, flags);
 
 	if (!QED_LL2_RX_REGISTERED(p_ll2_conn)) {

--
2.40.0

#41 lore raw

PATCH RESEND 1/1 qed: Move a variable assignment behind a null pointer check in two functions

From: Markus Elfring <[email protected]>
Date: Tue, 11 Apr 2023 19:33:53 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “qed_ll2_rxq_completion” and “qed_ll2_txq_completion”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variables “p_rx” and “p_tx” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 0a7fb11c23c0 ("qed: Add Light L2 support")
---
 drivers/net/ethernet/qlogic/qed/qed_ll2.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ethernet/qlogic/qed/qed_ll2.c +4 -2
index 717a0b3f89bd..941c02fccaaf 100644
@@ -346,7 +346,7 @@ static void qed_ll2_txq_flush(struct qed_hwfn *p_hwfn, u8 connection_handle)
 static int qed_ll2_txq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
 {
 	struct qed_ll2_info *p_ll2_conn = p_cookie;
-	struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
+	struct qed_ll2_tx_queue *p_tx;
 	u16 new_idx = 0, num_bds = 0, num_bds_in_packet = 0;
 	struct qed_ll2_tx_packet *p_pkt;
 	bool b_last_frag = false;
@@ -356,6 +356,7 @@ static int qed_ll2_txq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
 	if (!p_ll2_conn)
 		return rc;
 
+	p_tx = &p_ll2_conn->tx_queue;
 	spin_lock_irqsave(&p_tx->lock, flags);
 	if (p_tx->b_completing_packet) {
 		rc = -EBUSY;
@@ -523,7 +524,7 @@ qed_ll2_rxq_handle_completion(struct qed_hwfn *p_hwfn,
 static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie)
 {
 	struct qed_ll2_info *p_ll2_conn = (struct qed_ll2_info *)cookie;
-	struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
+	struct qed_ll2_rx_queue *p_rx;
 	union core_rx_cqe_union *cqe = NULL;
 	u16 cq_new_idx = 0, cq_old_idx = 0;
 	unsigned long flags = 0;
@@ -532,6 +533,7 @@ static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie)
 	if (!p_ll2_conn)
 		return rc;
 
+	p_rx = &p_ll2_conn->rx_queue;
 	spin_lock_irqsave(&p_rx->lock, flags);
 
 	if (!QED_LL2_RX_REGISTERED(p_ll2_conn)) {

--
2.40.0

Reviewed-by on v1 1/1
#42 lore raw

Re: [PATCH RESEND] qed: Move a variable assignment behind a null pointer check in two functions

Quoted 49 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 11 Apr 2023 19:33:53 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “qed_ll2_rxq_completion” and “qed_ll2_txq_completion”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variables “p_rx” and “p_tx” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 0a7fb11c23c0 ("qed: Add Light L2 support")
---
 drivers/net/ethernet/qlogic/qed/qed_ll2.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ethernet/qlogic/qed/qed_ll2.c +3 -2
index 717a0b3f89bd..941c02fccaaf 100644
@@ -346,7 +346,7 @@ static void qed_ll2_txq_flush(struct qed_hwfn *p_hwfn, u8 connection_handle)
 static int qed_ll2_txq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
 {
 	struct qed_ll2_info *p_ll2_conn = p_cookie;
-	struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
+	struct qed_ll2_tx_queue *p_tx;
 	u16 new_idx = 0, num_bds = 0, num_bds_in_packet = 0;
 	struct qed_ll2_tx_packet *p_pkt;
 	bool b_last_frag = false;
@@ -356,6 +356,7 @@ static int qed_ll2_txq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
 	if (!p_ll2_conn)
 		return rc;
 
+	p_tx = &p_ll2_conn->tx_queue;
 	spin_lock_irqsave(&p_tx->lock, flags);
 	if (p_tx->b_completing_packet) {
 		rc = -EBUSY;
@@ -523,7 +524,7 @@ qed_ll2_rxq_handle_completion(struct qed_hwfn *p_hwfn,
 static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie)
 {
 	struct qed_ll2_info *p_ll2_conn = (struct qed_ll2_info *)cookie;
-	struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
+	struct qed_ll2_rx_queue *p_rx;
 	union core_rx_cqe_union *cqe = NULL;
 	u16 cq_new_idx = 0, cq_old_idx = 0;
 	unsigned long flags = 0;

drivers/net/ethernet/qlogic/qed/qed_ll2.c static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie) · line 539

Quoted 7 lines
drivers/net/ethernet/qlogic/qed/qed_ll2.c +1 -0
@@ -532,6 +533,7 @@ static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie)
 	if (!p_ll2_conn)
 		return rc;
 
+	p_rx = &p_ll2_conn->rx_queue;
 	spin_lock_irqsave(&p_rx->lock, flags);
 
 	if (!QED_LL2_RX_REGISTERED(p_ll2_conn)) {

For future submission plase specify the target kernel [PATCH net] for fixes, [PATCH net-next] for other.

Looking at the code callback is always set together with cookie (which is pointing to p_ll2_conn. I am not sure if this is fixing real issue, but maybe there are a cases when callback is still connected and cookie is NULL.

Anyway, with heaving this check for p_ll2_conn it is good to move assigment after this check.

Quoted 2 lines

--
2.40.0

#43 lore raw

Re: [PATCH RESEND] qed: Move a variable assignment behind a null pointer check in two functions

Quoted 21 lines · Michal Swiatkowski
Quoted 10 lines
drivers/net/ethernet/qlogic/qed/qed_ll2.c +1 -1
@@ -523,7 +524,7 @@ qed_ll2_rxq_handle_completion(struct qed_hwfn *p_hwfn,
 static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie)
 {
 	struct qed_ll2_info *p_ll2_conn = (struct qed_ll2_info *)cookie;
-	struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
+	struct qed_ll2_rx_queue *p_rx;
 	union core_rx_cqe_union *cqe = NULL;
 	u16 cq_new_idx = 0, cq_old_idx = 0;
 	unsigned long flags = 0;

drivers/net/ethernet/qlogic/qed/qed_ll2.c static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie) · line 539

Quoted 7 lines
drivers/net/ethernet/qlogic/qed/qed_ll2.c +1 -0
@@ -532,6 +533,7 @@ static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie)
 	if (!p_ll2_conn)
 		return rc;
 
+	p_rx = &p_ll2_conn->rx_queue;
 	spin_lock_irqsave(&p_rx->lock, flags);
 
 	if (!QED_LL2_RX_REGISTERED(p_ll2_conn)) {

For future submission plase specify the target kernel [PATCH net] for fixes, [PATCH net-next] for other.

Quoted 4 lines

Looking at the code callback is always set together with cookie (which is pointing to p_ll2_conn. I am not sure if this is fixing real issue, but maybe there are a cases when callback is still connected and cookie is NULL.

The assignment:

	p_rx = &p_ll2_conn->rx_queue;

does not dereference "p_ll2_conn". It just does pointer math. So the original code works fine.

The question is, did the original author write it that way intentionally? I've had this conversation with people before and they eventually are convinced that "Okay, the code works, but only by sheer chance." In my experiences, most of the time, the authors of this type of code are so used to weirdnesses of C that they write code like this without even thinking about it. It never even occurs to them how it could be confusing.

This commit message is misleading and there should not be a Fixes tag.

regards,
dan carpenter

#45 lore raw

Re: [RESEND] qed: Move a variable assignment behind a null pointer check in two functions

Quoted 9 lines · Markus Elfring
Quoted 6 lines

The assignment:

	p_rx = &p_ll2_conn->rx_queue;

does not dereference "p_ll2_conn". It just does pointer math. So the original code works fine.

Is there a need to clarify affected implementation details any more?
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers

This is not a NULL dereference. It's just pointer math.

regards,
dan carpenter

#46 lore raw

Re: qed: Move a variable assignment behind a null pointer check in two functions

Quoted 1 line

This is not a NULL dereference. It's just pointer math.

How does your view fit to information in an article like “Fun with NULL pointers, part 1” (by Jonathan Corbet from 2009-07-20)?
https://lwn.net/Articles/342330/

Regards,
Markus

#47 lore raw

Re: [RESEND] qed: Move a variable assignment behind a null pointer check in two functions

In reply to Dan Carpenter
Quoted 8 lines · On Mon, 3 Mar 2025 11:28:29 +0300 Dan Carpenter <[email protected]>…
Quoted 7 lines · Markus Elfring
Quoted 6 lines

The assignment:

	p_rx = &p_ll2_conn->rx_queue;

does not dereference "p_ll2_conn". It just does pointer math. So the original code works fine.

Quoted 8 lines
Quoted 3 lines

Is there a need to clarify affected implementation details any more?
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers

This is not a NULL dereference. It's just pointer math.

regards,
dan carpenter

Feel free to ignore Markus, he has a long history of sending unhelpful reviews, comments or patches and continues to ignore repeated requests to stop. He is already on the ignore lists of several maintainers.
There is a lot of chance that he is a bot.

Regards,

Signature · 3 lines
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com
#48 lore raw

Re: qed: Move a variable assignment behind a null pointer check in two functions

Quoted 1 line

There is a lot of chance that he is a bot.

I hope that corresponding software development discussions can become more constructive again.

Regards,
Markus

#49 lore raw

PATCH 1/1 ASoC: SOF: topology: Move a variable assignment behind condition checks in sof_dai_load()

In reply to Markus Elfring

Date: Thu, 13 Apr 2023 13:56:44 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “sof_dai_load”.

Thus avoid the risk for undefined behaviour by moving the assignment for the local variable “private” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes c5232c0171428f005a3204e1c264231fb5999b28 ("ASoC: SOF: topology: parse and store d0i3_compatible flag")
---
 sound/soc/sof/topology.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
sound/soc/sof/topology.c +2 -1
index d3d536b0a8f5..3fffe3826160 100644
@@ -1680,7 +1680,7 @@ static int sof_dai_load(struct snd_soc_component *scomp, int index,
 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
 	const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm);
 	struct snd_soc_tplg_stream_caps *caps;
-	struct snd_soc_tplg_private *private = &pcm->priv;
+	struct snd_soc_tplg_private *private;
 	struct snd_sof_pcm *spcm;
 	int stream;
 	int ret;
@@ -1716,6 +1716,7 @@ static int sof_dai_load(struct snd_soc_component *scomp, int index,
 	dai_drv->dobj.private = spcm;
 	list_add(&spcm->list, &sdev->pcm_list);
 
+	private = &pcm->priv;
 	ret = sof_parse_tokens(scomp, spcm, stream_tokens,
 			       ARRAY_SIZE(stream_tokens), private->array,
 			       le32_to_cpu(private->size));

--
2.40.0

#50 lore raw

PATCH 1/1 perf map: Delete two variable initialisations before null pointer checks in sort__sym_from_cmp()

In reply to Markus Elfring

Date: Thu, 13 Apr 2023 14:46:39 +0200

Addresses of two data structure members were determined before corresponding null pointer checks in the implementation of the function “sort__sym_from_cmp”.

Thus avoid the risk for undefined behaviour by removing extra initialisations for the local variables “from_l” and “from_r” (also because they were already reassigned with the same value behind this pointer check).

This issue was detected by using the Coccinelle software.

Fixes 1b9e97a2a95e4941dcfa968c4b2e04022e9a343e ("perf tools: Fix report -F symbol_from for data without branch info")
---
 tools/perf/util/sort.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/perf/util/sort.c +1 -2
index 80c9960c37e5..f2ffaf90648e 100644
@@ -1020,8 +1020,7 @@ static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
 static int64_t
 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
 {
-	struct addr_map_symbol *from_l = &left->branch_info->from;
-	struct addr_map_symbol *from_r = &right->branch_info->from;
+	struct addr_map_symbol *from_l, *from_r;
 
 	if (!left->branch_info || !right->branch_info)
 		return cmp_null(left->branch_info, right->branch_info);

--
2.40.0

reviewer Acked-by on v1 1/1
#51 lore raw

Re: [PATCH] perf map: Delete two variable initialisations before null pointer checks in sort__sym_from_cmp()

Quoted 12 lines · Markus Elfring

Date: Thu, 13 Apr 2023 14:46:39 +0200

Addresses of two data structure members were determined before corresponding null pointer checks in the implementation of the function “sort__sym_from_cmp”.

Thus avoid the risk for undefined behaviour by removing extra initialisations for the local variables “from_l” and “from_r” (also because they were already reassigned with the same value behind this pointer check).

Quoted 4 lines

This issue was detected by using the Coccinelle software.

Fixes 1b9e97a2a95e4941dcfa968c4b2e04022e9a343e ("perf tools: Fix report -F symbol_from for data without branch info")

Thanks,
Ian

Quoted 21 lines
---
 tools/perf/util/sort.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/perf/util/sort.c +1 -3
index 80c9960c37e5..f2ffaf90648e 100644
@@ -1020,8 +1020,7 @@ static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
 static int64_t
 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
 {
-       struct addr_map_symbol *from_l = &left->branch_info->from;
-       struct addr_map_symbol *from_r = &right->branch_info->from;
+       struct addr_map_symbol *from_l, *from_r;
 
        if (!left->branch_info || !right->branch_info)
                return cmp_null(left->branch_info, right->branch_info);
--

2.40.0

#52 lore raw

PATCH 1/1 tipc: Reduce scope for the variable “fdefq” in tipc_link_tnl_prepare()

In reply to Markus Elfring

Date: Thu, 13 Apr 2023 17:00:11 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “tipc_link_tnl_prepare”.

Thus avoid the risk for undefined behaviour by moving the definition for the local variable “fdefq” into an if branch at the end.

This issue was detected by using the Coccinelle software.

Fixes 58ee86b8c7750a6b67d665a031aa3ff13a9b6863 ("tipc: adapt link failover for new Gap-ACK algorithm")
---
 net/tipc/link.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/tipc/link.c +2 -1
index b3ce24823f50..5aa645e3cb35 100644
@@ -1973,7 +1973,6 @@ void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
 void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
 			   int mtyp, struct sk_buff_head *xmitq)
 {
-	struct sk_buff_head *fdefq = &tnl->failover_deferdq;
 	struct sk_buff *skb, *tnlskb;
 	struct tipc_msg *hdr, tnlhdr;
 	struct sk_buff_head *queue = &l->transmq;
@@ -2100,6 +2099,8 @@ void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
 	tipc_link_xmit(tnl, &tnlq, xmitq);
 
 	if (mtyp == FAILOVER_MSG) {
+		struct sk_buff_head *fdefq = &tnl->failover_deferdq;
+
 		tnl->drop_point = l->rcv_nxt;
 		tnl->failover_reasm_skb = l->reasm_buf;
 		l->reasm_buf = NULL;

--
2.40.0

#53 lore raw

PATCH RESEND 1/1 tipc: Reduce scope for the variable “fdefq” in tipc_link_tnl_prepare()

From: Markus Elfring <[email protected]>
Date: Thu, 13 Apr 2023 17:00:11 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “tipc_link_tnl_prepare”.

Thus avoid the risk for undefined behaviour by moving the definition for the local variable “fdefq” into an if branch at the end.

This issue was detected by using the Coccinelle software.

Fixes 58ee86b8c775 ("tipc: adapt link failover for new Gap-ACK algorithm")
---
 net/tipc/link.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/tipc/link.c +2 -1
index b3ce24823f50..5aa645e3cb35 100644
@@ -1973,7 +1973,6 @@ void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
 void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
 			   int mtyp, struct sk_buff_head *xmitq)
 {
-	struct sk_buff_head *fdefq = &tnl->failover_deferdq;
 	struct sk_buff *skb, *tnlskb;
 	struct tipc_msg *hdr, tnlhdr;
 	struct sk_buff_head *queue = &l->transmq;
@@ -2100,6 +2099,8 @@ void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
 	tipc_link_xmit(tnl, &tnlq, xmitq);
 
 	if (mtyp == FAILOVER_MSG) {
+		struct sk_buff_head *fdefq = &tnl->failover_deferdq;
+
 		tnl->drop_point = l->rcv_nxt;
 		tnl->failover_reasm_skb = l->reasm_buf;
 		l->reasm_buf = NULL;

--
2.40.0

maintainer
#54 lore raw

Re: [PATCH RESEND] tipc: Reduce scope for the variable “fdefq” in tipc_link_tnl_prepare()

Quoted 7 lines · On Sun, 2 Mar 2025 21:30:27 +0100 Markus Elfring wrote:

From: Markus Elfring <[email protected]>
Date: Thu, 13 Apr 2023 17:00:11 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “tipc_link_tnl_prepare”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by moving the definition for the local variable “fdefq” into an if branch at the end.

This issue was detected by using the Coccinelle software.

Fixes 58ee86b8c775 ("tipc: adapt link failover for new Gap-ACK algorithm")

Applied without the Fixes tag, thanks.

#55 lore raw

PATCH 1/1 gfs2: Move a variable assignment behind a null pointer check in inode_go_dump()

In reply to Markus Elfring

Date: Thu, 13 Apr 2023 20:54:30 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function ?inode_go_dump?.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable ?inode? behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 27a2660f1ef944724956d92e8a312b6da0936fae ("gfs2: Dump nrpages for inodes and their glocks")
---
 fs/gfs2/glops.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/gfs2/glops.c +2 -1
index b65950e76be5..6e33c8058059 100644
@@ -535,12 +535,13 @@ static void inode_go_dump(struct seq_file *seq, struct gfs2_glock *gl,
 			  const char *fs_id_buf)
 {
 	struct gfs2_inode *ip = gl->gl_object;
-	struct inode *inode = &ip->i_inode;
+	struct inode *inode;
 	unsigned long nrpages;
 
 	if (ip == NULL)
 		return;
 
+	inode = &ip->i_inode;
 	xa_lock_irq(&inode->i_data.i_pages);
 	nrpages = inode->i_data.nrpages;
 	xa_unlock_irq(&inode->i_data.i_pages);

--
2.40.0

#56 lore raw

Re: [PATCH] gfs2: Move a variable assignment behind a null pointer check in inode_go_dump()

Hi Markus,

Quoted 6 lines · On Thu, Apr 13, 2023 at 9:23?PM Markus Elfring <[email protected]> wrote:

Date: Thu, 13 Apr 2023 20:54:30 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function ?inode_go_dump?.

Quoted 6 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable ?inode? behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 27a2660f1ef944724956d92e8a312b6da0936fae ("gfs2: Dump nrpages for inodes and their glocks")

Okay, that's a worthwhile cleanup. It doesn't actually fix a bug, so I'm not going to add a Fixes tag, though.

Quoted 25 lines
---
 fs/gfs2/glops.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/gfs2/glops.c +2 -2
index b65950e76be5..6e33c8058059 100644
@@ -535,12 +535,13 @@ static void inode_go_dump(struct seq_file *seq, struct gfs2_glock *gl,
                          const char *fs_id_buf)
 {
        struct gfs2_inode *ip = gl->gl_object;
-       struct inode *inode = &ip->i_inode;
+       struct inode *inode;
        unsigned long nrpages;
 
        if (ip == NULL)
                return;
 
+       inode = &ip->i_inode;
        xa_lock_irq(&inode->i_data.i_pages);
        nrpages = inode->i_data.nrpages;
        xa_unlock_irq(&inode->i_data.i_pages);
--
Quoted 2 lines

2.40.0

Thanks,
Andreas

#57 lore raw

Re: [PATCH] gfs2: Move a variable assignment behind a null pointer check in inode_go_dump()

Quoted 4 lines
Quoted 4 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “inode_go_dump”.

Quoted 8 lines
Quoted 6 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “inode” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 27a2660f1ef944724956d92e8a312b6da0936fae ("gfs2: Dump nrpages for inodes and their glocks")

Okay, that's a worthwhile cleanup.

Thanks for a bit of positive feedback.

Why is this implementation detail still questionable so far?
https://elixir.bootlin.com/linux/v7.2-rc2/source/fs/gfs2/glops.c#L541-L565

Quoted 2 lines

It doesn't actually fix a bug, so
I'm not going to add a Fixes tag, though.

How does your view fit to information in an article like “Fun with NULL pointers, part 1” (by Jonathan Corbet from 2009-07-20)?
https://lwn.net/Articles/342330/

Regards,
Markus

#58 lore raw

PATCH 1/1 video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

In reply to Markus Elfring

Date: Thu, 13 Apr 2023 21:35:36 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “au1100fb_setmode”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “info” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 3b495f2bb749b828499135743b9ddec46e34fda8 ("Au1100 FB driver uplift for 2.6.")
---
 drivers/video/fbdev/au1100fb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/video/fbdev/au1100fb.c +3 -1
index cb317398e71a..fcb47b350bc9 100644
@@ -137,13 +137,15 @@ static int au1100fb_fb_blank(int blank_mode, struct fb_info *fbi)
 	 */
 int au1100fb_setmode(struct au1100fb_device *fbdev)
 {
-	struct fb_info *info = &fbdev->info;
+	struct fb_info *info;
 	u32 words;
 	int index;
 
 	if (!fbdev)
 		return -EINVAL;
 
+	info = &fbdev->info;
+
 	/* Update var-dependent FB info */
 	if (panel_is_active(fbdev->panel) || panel_is_color(fbdev->panel)) {
 		if (info->var.bits_per_pixel <= 8) {

--
2.40.0

#59 lore raw

PATCH RESEND 1/1 video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

From: Markus Elfring <[email protected]>
Date: Thu, 13 Apr 2023 21:35:36 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “au1100fb_setmode”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “info” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 3b495f2bb749 ("Au1100 FB driver uplift for 2.6.")
---
 drivers/video/fbdev/au1100fb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/video/fbdev/au1100fb.c +3 -1
index cb317398e71a..fcb47b350bc9 100644
@@ -137,13 +137,15 @@ static int au1100fb_fb_blank(int blank_mode, struct fb_info *fbi)
 	 */
 int au1100fb_setmode(struct au1100fb_device *fbdev)
 {
-	struct fb_info *info = &fbdev->info;
+	struct fb_info *info;
 	u32 words;
 	int index;
 
 	if (!fbdev)
 		return -EINVAL;
 
+	info = &fbdev->info;
+
 	/* Update var-dependent FB info */
 	if (panel_is_active(fbdev->panel) || panel_is_color(fbdev->panel)) {
 		if (info->var.bits_per_pixel <= 8) {

--
2.40.0

Acked-by on v1 1/1
#60 lore raw

Re: [PATCH RESEND] video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

Hello,

Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 13 Apr 2023 21:35:36 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “au1100fb_setmode”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “info” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 3b495f2bb749 ("Au1100 FB driver uplift for 2.6.")

Should also get

to ensure this is backported to stable.

Best regards
Uwe

#61 lore raw

Re: [PATCH RESEND] video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

Quoted 18 lines · Uwe Kleine-König

Hello,

Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 13 Apr 2023 21:35:36 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “au1100fb_setmode”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “info” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 3b495f2bb749 ("Au1100 FB driver uplift for 2.6.")
Quoted 7 lines

Should also get

to ensure this is backported to stable.

It's not a bugfix, it's a cleanup. That's not a dereference, it's just pointer math. It shouldn't have a Fixes tag.

Real bugs where we dereference a pointer and then check for NULL don't last long in the kernel. Most of the stuff Markus is sending is false positives like this.

regards,
dan carpenter

#62 lore raw

Re: [PATCH RESEND] video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

Quoted 3 lines · Dan Carpenter

Real bugs where we dereference a pointer and then check for NULL don't last long in the kernel. Most of the stuff Markus is sending is false positives like this.

Maybe I was too optimistic. Here are the Smatch warnings from the Friday's linux-next.

Common false positives are that the pointer can sometimes be NULL but there are other ways to determine without checking explicitly. For example, maybe the caller passes a flag which means it's non-NULL.

regards,
dan carpenter

arch/arm64/kvm/../../../virt/kvm/kvm_main.c:1639 kvm_prepare_memory_region() warn: variable dereferenced before check 'new' (see line 1622) arch/x86/kernel/fpu/xstate.c:1567 fpstate_realloc() warn: variable dereferenced before check 'curfps' (see line 1546) arch/x86/kvm/../../../virt/kvm/kvm_main.c:1639 kvm_prepare_memory_region() warn: variable dereferenced before check 'new' (see line 1622)
drivers/base/dd.c:696 really_probe() warn: variable dereferenced before check 'dev->bus' (see line 640)
drivers/base/dd.c:720 really_probe() warn: variable dereferenced before check 'dev->bus' (see line 640)
drivers/block/drbd/drbd_worker.c:588 make_resync_request() warn: variable dereferenced before check 'peer_device' (see line 587) drivers/gpu/drm/amd/amdgpu/../display/amdgpu_dm/amdgpu_dm.c:12341 parse_edid_displayid_vrr() warn: variable dereferenced before check 'edid_ext' (see line 12337) drivers/gpu/drm/amd/amdgpu/../display/dc/clk_mgr/dcn21/rn_clk_mgr.c:775 rn_clk_mgr_construct() warn: variable dereferenced before check 'ctx->dc_bios->integrated_info' (see line 743) drivers/gpu/drm/amd/amdgpu/../display/dc/clk_mgr/dcn301/vg_clk_mgr.c:734 vg_clk_mgr_construct() warn: variable dereferenced before check 'ctx->dc_bios->integrated_info' (see line 720) drivers/gpu/drm/amd/amdgpu/../display/dc/clk_mgr/dcn314/dcn314_clk_mgr.c:899 dcn314_clk_mgr_construct() warn: variable dereferenced before check 'ctx->dc_bios->integrated_info' (see line 838) drivers/gpu/drm/amd/amdgpu/../display/dc/clk_mgr/dcn315/dcn315_clk_mgr.c:715 dcn315_clk_mgr_construct() warn: variable dereferenced before check 'ctx->dc_bios->integrated_info' (see line 654) drivers/gpu/drm/amd/amdgpu/../display/dc/clk_mgr/dcn316/dcn316_clk_mgr.c:655 dcn316_clk_mgr_construct() warn: variable dereferenced before check 'ctx->dc_bios->integrated_info' (see line 634) drivers/gpu/drm/amd/amdgpu/../display/dc/clk_mgr/dcn31/dcn31_clk_mgr.c:789 dcn31_clk_mgr_construct() warn: variable dereferenced before check 'ctx->dc_bios->integrated_info' (see line 728) drivers/gpu/drm/amd/amdgpu/../display/dc/clk_mgr/dcn35/dcn35_clk_mgr.c:1380 dcn35_clk_mgr_construct() warn: variable dereferenced before check 'ctx->dc_bios->integrated_info' (see line 1307) drivers/gpu/drm/i915/gem/i915_gem_shmem.c:174 shmem_sg_alloc_table() warn: variable dereferenced before check 'sg' (see line 163) drivers/gpu/drm/i915/gt/gen6_ppgtt.c:274 gen6_ppgtt_cleanup() warn: variable dereferenced before check 'ppgtt->base.pd' (see line 271) drivers/gpu/drm/i915/gt/intel_execlists_submission.c:3649 rcu_virtual_context_destroy() warn: variable dereferenced before check 've->base.sched_engine' (see line 3623) drivers/gpu/drm/nouveau/nouveau_dp.c:494 nouveau_dp_irq() warn: variable dereferenced before check 'outp' (see line 489)
drivers/hid/hid-debug.c:3727 hid_debug_events_read() warn: variable dereferenced before check 'list->hdev' (see line 3713) drivers/net/ethernet/amd/pcnet32.c:1923 pcnet32_probe1() warn: variable dereferenced before check 'pdev' (see line 1843) drivers/net/ethernet/apm/xgene/xgene_enet_main.c:267 xgene_enet_tx_completion() warn: variable dereferenced before check 'skb' (see line 243) drivers/net/ethernet/natsemi/ns83820.c:884 rx_irq() warn: variable dereferenced before check 'skb' (see line 883) drivers/net/ethernet/pensando/ionic/ionic_txrx.c:205 ionic_rx_build_skb() warn: variable dereferenced before check 'buf_info->page' (see line 188) drivers/net/wireless/intel/iwlwifi/dvm/mac80211.c:1236 iwl_setup_interface() warn: variable dereferenced before check 'priv->lib->bt_params' (see line 1229) drivers/net/wireless/intel/iwlwifi/dvm/main.c:1114 iwl_init_drv() warn: variable dereferenced before check 'priv->lib->bt_params' (see line 1109) drivers/net/wireless/mediatek/mt76/mt76_connac_mac.c:307 mt76_connac2_mac_tx_rate_val() warn: variable dereferenced before check 'conf' (see line 300) drivers/net/wireless/mediatek/mt76/mt7925/mac.c:851 mt7925_tx_check_aggr() warn: variable dereferenced before check 'sta' (see line 847) drivers/nvme/host/ioctl.c:173 nvme_map_user_request() warn: variable dereferenced before check 'bio' (see line 162)
drivers/platform/mellanox/mlxreg-lc.c:903 mlxreg_lc_probe() warn: variable dereferenced before check 'data->notifier' (see line 828) drivers/scsi/aacraid/commsup.c:2344 aac_command_thread() warn: variable dereferenced before check 'dev->queues' (see line 2332) drivers/scsi/aic7xxx/aic79xx_osm.c:1837 ahd_done() warn: variable dereferenced before check 'cmd' (see line 1793)
drivers/scsi/csiostor/csio_mb.c:932 csio_fcoe_vnp_alloc_init_mb() warn: variable dereferenced before check 'vnport_wwnn' (see line 929)
drivers/scsi/ips.c:2560 ips_next() warn: variable dereferenced before check 'scb->scsi_cmd' (see line 2554)
drivers/scsi/ips.c:2568 ips_next() warn: variable dereferenced before check 'scb->scsi_cmd' (see line 2554)
drivers/scsi/ips.c:2593 ips_next() warn: variable dereferenced before check 'scb->scsi_cmd' (see line 2554)
drivers/scsi/libsas/sas_scsi_host.c:119 sas_scsi_task_done() warn: variable dereferenced before check 'sc' (see line 110)
drivers/scsi/lpfc/lpfc_bsg.c:1332 lpfc_bsg_hba_get_event() warn: variable dereferenced before check 'evt_dat' (see line 1299) drivers/slimbus/qcom-ngd-ctrl.c:884 qcom_slim_ngd_xfer_msg() warn: variable dereferenced before check 'txn->msg' (see line 808)
drivers/spi/spi-offload.c:186 spi_offload_trigger_get() warn: variable dereferenced before check 'trigger->ops' (see line 176) drivers/staging/media/atomisp/pci/isp/kernels/output/output_1.0/ia_css_output.host.c:58 ia_css_output_config() warn: variable dereferenced before check 'from->info' (see line 53) drivers/usb/gadget/udc/tegra-xudc.c:2681 tegra_xudc_handle_transfer_completion() warn: variable dereferenced before check 'ep->desc' (see line 2679) drivers/usb/musb/musb_core.c:964 musb_handle_intr_disconnect() warn: variable dereferenced before check 'musb->hcd' (see line 963)
fs/bcachefs/journal.c:1187 __bch2_set_nr_journal_buckets() warn: variable dereferenced before check 'c' (see line 1184)
fs/efs/inode.c:299 efs_map_block() warn: variable dereferenced before check 'bh' (see line 292)
fs/efs/inode.c:304 efs_map_block() warn: variable dereferenced before check 'bh' (see line 292)
fs/efs/inode.c:309 efs_map_block() warn: variable dereferenced before check 'bh' (see line 292)
fs/nfs/write.c:808 nfs_inode_remove_request() warn: variable dereferenced before check 'folio' (see line 805)
fs/ocfs2/dlm/dlmrecovery.c:1590 dlm_mig_lockres_worker() warn: variable dereferenced before check 'res' (see line 1563)
fs/ocfs2/move_extents.c:322 ocfs2_defrag_extent() warn: variable dereferenced before check 'context->data_ac' (see line 279)
fs/ocfs2/namei.c:1455 ocfs2_rename() warn: variable dereferenced before check 'newfe_bh' (see line 1452)
fs/ocfs2/namei.c:1637 ocfs2_rename() warn: variable dereferenced before check 'old_dir_bh' (see line 1618)
fs/smb/client/file.c:3085 cifs_oplock_break() warn: variable dereferenced before check 'inode' (see line 3056)
lib/reed_solomon/decode_rs.c:315 decode_rs16() warn: variable dereferenced before check 'par' (see line 81)
net/core/failover.c:85 failover_slave_register() warn: variable dereferenced before check 'fops' (see line 66)
net/mpls/mpls_iptunnel.c:156 mpls_xmit() warn: variable dereferenced before check 'out_dev' (see line 56)

#63 lore raw

Re: [PATCH RESEND] video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

In reply to Dan Carpenter
Quoted 25 lines · Dan Carpenter
Quoted 24 lines · Uwe Kleine-König

Hello,

Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 13 Apr 2023 21:35:36 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “au1100fb_setmode”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “info” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 3b495f2bb749 ("Au1100 FB driver uplift for 2.6.")

Should also get

Quoted 8 lines
Quoted 1 line

to ensure this is backported to stable.

It's not a bugfix, it's a cleanup. That's not a dereference, it's just pointer math. It shouldn't have a Fixes tag.

Real bugs where we dereference a pointer and then check for NULL don't last long in the kernel. Most of the stuff Markus is sending is false positives like this.

I thought a compiler translating the code

	struct fb_info *info = &fbdev->info;

	if (!fbdev)
		return -EINVAL;

is free (and expected) to just drop the if block. I wasn't aware that this only applies when the pointer is actually dereferenced. Testing that with arm-linux-gnueabihf-gcc 14.2.0 seems to confirm what you're saying.

Thanks for letting me know. With that learned I agree that the Fixes tag should be dropped (and Cc: stable not added).

Best regards
Uwe

#64 lore raw

Re: video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

Quoted 4 lines
	struct fb_info *info = &fbdev->info;

	if (!fbdev)
		return -EINVAL;

Is such a null pointer check still relevant for the discussed function implementation?

Regards,
Markus

#65 lore raw

Re: [PATCH RESEND] video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

In reply to Uwe Kleine-König
Quoted 35 lines · Uwe Kleine-König
Quoted 25 lines · Dan Carpenter
Quoted 24 lines · Uwe Kleine-König

Hello,

Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 13 Apr 2023 21:35:36 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “au1100fb_setmode”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “info” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 3b495f2bb749 ("Au1100 FB driver uplift for 2.6.")

Should also get

Quoted 8 lines
Quoted 1 line

to ensure this is backported to stable.

It's not a bugfix, it's a cleanup. That's not a dereference, it's just pointer math. It shouldn't have a Fixes tag.

Real bugs where we dereference a pointer and then check for NULL don't last long in the kernel. Most of the stuff Markus is sending is false positives like this.

Quoted 8 lines

I thought a compiler translating the code

	struct fb_info *info = &fbdev->info;

	if (!fbdev)
		return -EINVAL;

is free (and expected) to just drop the if block.

If you dereference a pointer then it doesn't make sense to have a NULL check after that because the kernel would already have crashed.

In 2009, we had the famous tun.c bug where there was a dereference followed by a NULL check and the compiler deleted it as you say. And then, it turned out that you could remap the NULL pointer to and so the NULL dereference didn't lead to a crash and the missing NULL meant the kernel kept running happily. The remapped memory was full of function pointers to get root.

We changed min_mmap_addr so that we can't remap the NULL pointer and we started using the -fno-delete-null-pointer-checks compiler option so that it wouldn't remove the NULL check even in places where it didn't make sense. We also started doing more static analysis. We've also tried to randomize where functions are in the memory so it's trickier to hardcode function pointers.

A couple years later we had another bug where it turned out you could still remap NULL. I forget how the details. No one wrote an exploit and it wasn't publicized as much.

Anyway, none of that applies here, because this is just pointer math.

regards,
dan carpenter

#66 lore raw

Re: video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

Quoted 1 line

Anyway, none of that applies here, because this is just pointer math.

Which data processing do you expect to be generally supported at the discussed source code place (according to the rules of the programming language “C”)?
https://en.cppreference.com/w/c/language/behavior

Regards,
Markus

maintainer
#67 lore raw

Re: video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

Quoted 4 lines · Markus Elfring
Quoted 1 line

Anyway, none of that applies here, because this is just pointer math.

Which data processing do you expect to be generally supported at the discussed source code place (according to the rules of the programming language “C”)?
https://en.cppreference.com/w/c/language/behavior

There is nothing to discuss.
Dan is correct.

We have:

struct au1100fb_device {
        struct fb_info info;

...

so:

struct fb_info *info = &fbdev->info;
gets translated by the compiler to a trivial pointer math:
info = <value of fbdev> + 0 # 0 is there offset of "info" in the struct.

No crash or anything can or will happen here.

Markus, maybe you missed the "&" in front of "&fbdev->info" ?

Helge

#68 lore raw

Re: video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

Quoted 3 lines

No crash or anything can or will happen here.

Markus, maybe you missed the "&" in front of "&fbdev->info" ?

Would you get into the mood to adjust development views if you would take any feedback and further background information (by David Svoboda for example) better into account?
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers?focusedCommentId=405504139#comment-405504139

Regards,
Markus

maintainer Applied
#69 lore raw

Re: [PATCH RESEND] video: au1100fb: Move a variable assignment behind a null pointer check in au1100fb_setmode()

In reply to Uwe Kleine-König
Quoted 13 lines · Uwe Kleine-König

Hello,

Quoted 10 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 13 Apr 2023 21:35:36 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “au1100fb_setmode”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “info” behind the null pointer check.

Quoted 6 lines
Quoted 4 lines

This issue was detected by using the Coccinelle software.

Fixes 3b495f2bb749 ("Au1100 FB driver uplift for 2.6.")

applied to fbdev git tree (with minor modifications to commit message and without stable tags).

Helge

#70 lore raw

PATCH 1/1 media: atomisp: Move a variable assignment behind a null pointer check in atomisp_cp_general_isp_parameters()

In reply to Markus Elfring

Date: Thu, 13 Apr 2023 22:08:42 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “atomisp_cp_general_isp_parameters”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “cur_config” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes ad85094b293e40e7a2f831b0311a389d952ebd5e ("Revert 'media: staging: atomisp: Remove driver'")
---
 drivers/staging/media/atomisp/pci/atomisp_cmd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/staging/media/atomisp/pci/atomisp_cmd.c +3 -1
index 47f18ac5e40e..733a239b5d8a 100644
@@ -2723,11 +2723,13 @@ int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd,
 				      struct atomisp_css_params *css_param,
 				      bool from_user)
 {
-	struct atomisp_parameters *cur_config = &css_param->update_flag;
+	struct atomisp_parameters *cur_config;
 
 	if (!arg || !asd || !css_param)
 		return -EINVAL;
 
+	cur_config = &css_param->update_flag;
+
 	if (arg->wb_config && (from_user || !cur_config->wb_config)) {
 		if (copy_from_compatible(&css_param->wb_config, arg->wb_config,
 					 sizeof(struct ia_css_wb_config),

--
2.40.0

maintainer
#71 lore raw

Re: [PATCH] media: atomisp: Move a variable assignment behind a null pointer check in atomisp_cp_general_isp_parameters()

Quoted 8 lines · Markus Elfring

Date: Thu, 13 Apr 2023 22:08:42 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “atomisp_cp_general_isp_parameters”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “cur_config” behind the null pointer check.

I don't think this is what is happening here. The check might be removed by optimizer in the compiler.

Quoted 3 lines

This issue was detected by using the Coccinelle software.

Fixes ad85094b293e40e7a2f831b0311a389d952ebd5e ("Revert 'media: staging: atomisp: Remove driver'")

Wrong tag format.

Code-wise I'm not against this, but it's up to Hans.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#72 lore raw

Re: [PATCH] media: atomisp: Move a variable assignment behind a null pointer check in atomisp_cp_general_isp_parameters()

Hi,

Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Markus Elfring

Date: Thu, 13 Apr 2023 22:08:42 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “atomisp_cp_general_isp_parameters”.

Quoted 5 lines
Quoted 2 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “cur_config” behind the null pointer check.

I don't think this is what is happening here. The check might be removed by optimizer in the compiler.

Right, Markus thank you for the patch.

Instead of this patch, can you please audit the callers of this function? I expect that you will likely find that the callers already unconditionally deref css_param themselves, or they guarantee a non NULL value in other ways (e.g. address of local variable).

So I expect that the check can simply be dropped completely, which would be a much better fix.

Regards,

Hans

Quoted 9 lines
Quoted 4 lines

This issue was detected by using the Coccinelle software.

Fixes ad85094b293e40e7a2f831b0311a389d952ebd5e ("Revert 'media: staging: atomisp: Remove driver'")

Wrong tag format.

Code-wise I'm not against this, but it's up to Hans.

#73 lore raw

Re: [cocci] [PATCH] media: atomisp: Move a variable assignment behind a null pointer check in atomisp_cp_general_isp_parameters()

Quoted 1 line

Instead of this patch, can you please audit the callers of this function?

Did any analysis tool publish a call tree already (according to your enquiry)?
https://elixir.bootlin.com/linux/v6.3-rc6/source/drivers/staging/media/atomisp/pci/atomisp_cmd.c#L2721

Quoted 7 lines

I expect that you will likely find that the callers already unconditionally deref css_param themselves, or they guarantee a non NULL value in other ways (e.g. address of local variable).

So I expect that the check can simply be dropped completely, which would be a much better fix.

Are you really looking for ways to omit null pointer checks for three input parameters here?

Would you like to increase the application of any annotations or API attributes?

Regards,
Markus

#74 lore raw

Re: [cocci] media: atomisp: Move a variable assignment behind a null pointer check in atomisp_cp_general_isp_parameters()

In reply to Hans de Goede
Quoted 1 line

Instead of this patch, can you please audit the callers of this function?

How will the determination evolve further if null pointer checks would be still relevant for three input parameters according to this function implementation at the moment?
https://elixir.bootlin.com/linux/v6.3-rc7/source/drivers/staging/media/atomisp/pci/atomisp_cmd.c#L2721

Quoted 7 lines

I expect that you will likely find that the callers already unconditionally deref css_param themselves, or they guarantee a non NULL value in other ways (e.g. address of local variable).

So I expect that the check can simply be dropped completely, which would be a much better fix.

Will affected implementation details be reconsidered any more?

Regards,
Markus

#75 lore raw

Re: [cocci] [PATCH] media: atomisp: Move a variable assignment behind a null pointer check in atomisp_cp_general_isp_parameters()

In reply to Hans de Goede
Quoted 1 line

Instead of this patch, can you please audit the callers of this function?

Would you like to add any advices for a similarly affected implementation of the function “atomisp_set_parameters”?
https://elixir.bootlin.com/linux/v6.3-rc7/source/drivers/staging/media/atomisp/pci/atomisp_cmd.c#L3619

Regards,
Markus

#76 lore raw

PATCH 1/1 scsi: hpsa: Move two variable assignments behind condition checks in hpsa_scsi_ioaccel_raid_map()

In reply to Markus Elfring

Date: Fri, 14 Apr 2023 11:00:40 +0200

Addresses of two data structure members were determined before a corresponding null pointer check in the implementation of the function “hpsa_scsi_ioaccel_raid_map”.

Thus avoid the risk for undefined behaviour by moving the assignment for two local variables behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 283b4a9b98b192ebc0e15351fd6fb60e1be78c5d ("[SCSI] hpsa: add ioaccell mode 1 RAID offload support.")
---
 drivers/scsi/hpsa.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/scsi/hpsa.c +5 -2
index af18d20f3079..562bb5eab134 100644
@@ -5104,8 +5104,8 @@ static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
 {
 	struct scsi_cmnd *cmd = c->scsi_cmd;
 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
-	struct raid_map_data *map = &dev->raid_map;
-	struct raid_map_disk_data *dd = &map->data[0];
+	struct raid_map_data *map;
+	struct raid_map_disk_data *dd;
 	int is_write = 0;
 	u32 map_index;
 	u64 first_block, last_block;
@@ -5209,6 +5209,8 @@ static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
 	if (is_write && dev->raid_level != 0)
 		return IO_ACCEL_INELIGIBLE;
 
+	map = &dev->raid_map;
+
 	/* check for invalid block or wraparound */
 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
 		last_block < first_block)
@@ -5397,6 +5399,7 @@ static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
 	if (!c->phys_disk)
 		return IO_ACCEL_INELIGIBLE;
 
+	dd = &map->data[0];
 	disk_handle = dd[map_index].ioaccel_handle;
 	disk_block = le64_to_cpu(map->disk_starting_blk) +
 			first_row * le16_to_cpu(map->strip_size) +

--
2.40.0

#77 lore raw

PATCH RESEND 1/1 scsi: hpsa: Move two variable assignments behind condition checks in hpsa_scsi_ioaccel_raid_map()

From: Markus Elfring <[email protected]>
Date: Fri, 14 Apr 2023 11:00:40 +0200

Addresses of two data structure members were determined before a corresponding null pointer check in the implementation of the function “hpsa_scsi_ioaccel_raid_map”.

Thus avoid the risk for undefined behaviour by moving the assignment for two local variables behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 283b4a9b98b1 ("[SCSI] hpsa: add ioaccell mode 1 RAID offload support.")
---
 drivers/scsi/hpsa.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/scsi/hpsa.c +5 -2
index af18d20f3079..562bb5eab134 100644
@@ -5104,8 +5104,8 @@ static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
 {
 	struct scsi_cmnd *cmd = c->scsi_cmd;
 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
-	struct raid_map_data *map = &dev->raid_map;
-	struct raid_map_disk_data *dd = &map->data[0];
+	struct raid_map_data *map;
+	struct raid_map_disk_data *dd;
 	int is_write = 0;
 	u32 map_index;
 	u64 first_block, last_block;
@@ -5209,6 +5209,8 @@ static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
 	if (is_write && dev->raid_level != 0)
 		return IO_ACCEL_INELIGIBLE;
 
+	map = &dev->raid_map;
+
 	/* check for invalid block or wraparound */
 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
 		last_block < first_block)
@@ -5397,6 +5399,7 @@ static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
 	if (!c->phys_disk)
 		return IO_ACCEL_INELIGIBLE;
 
+	dd = &map->data[0];
 	disk_handle = dd[map_index].ioaccel_handle;
 	disk_block = le64_to_cpu(map->disk_starting_blk) +
 			first_row * le16_to_cpu(map->strip_size) +

--
2.40.0

#78 lore raw

PATCH 1/1 nvdimm: Replace the usage of a variable by a direct function call in nd_pfn_validate()

In reply to Markus Elfring

Date: Fri, 14 Apr 2023 12:01:15 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “nd_pfn_validate”.

Thus avoid the risk for undefined behaviour by replacing the usage of the local variable “parent_uuid” by a direct function call within a later condition check.

This issue was detected by using the Coccinelle software.

Fixes d1c6e08e7503649e4a4f3f9e700e2c05300b6379 ("libnvdimm/labels: Add uuid helpers")
---
 drivers/nvdimm/pfn_devs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/nvdimm/pfn_devs.c +1 -2
index af7d9301520c..f14cbfa500ed 100644
@@ -456,7 +456,6 @@ int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
 	unsigned long align, start_pad;
 	struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
 	struct nd_namespace_common *ndns = nd_pfn->ndns;
-	const uuid_t *parent_uuid = nd_dev_to_uuid(&ndns->dev);
 
 	if (!pfn_sb || !ndns)
 		return -ENODEV;
@@ -476,7 +475,7 @@ int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
 		return -ENODEV;
 	pfn_sb->checksum = cpu_to_le64(checksum);
 
-	if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
+	if (memcmp(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16) != 0)
 		return -ENODEV;
 
 	if (__le16_to_cpu(pfn_sb->version_minor) < 1) {

--
2.40.0

maintainer
#79 lore raw

Re: [PATCH] nvdimm: Replace the usage of a variable by a direct function call in nd_pfn_validate()

Quoted 9 lines · Markus Elfring

Date: Fri, 14 Apr 2023 12:01:15 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “nd_pfn_validate”.

Thus avoid the risk for undefined behaviour by replacing the usage of the local variable “parent_uuid” by a direct function call within a later condition check.

Hi Markus,

I think I understand what you are saying above, but I don't follow how that applies here. This change seems to be a nice simplification, parent_uuid, is used once, just grab it when needed.

What is the risk of undefined behavior?

Quoted 2 lines

This issue was detected by using the Coccinelle software.

Which cocci script?

Quoted 2 lines
Fixes d1c6e08e7503649e4a4f3f9e700e2c05300b6379 ("libnvdimm/labels: Add uuid helpers")

This fixes tag seems to be the wrong tag. It is a tag from when the uuid helpers were introduce, not where parent_uuid was first introduced and used. I'm not clear this warrants a Fixes tag anyway. Is there really a bug here? Perhaps I'm missing something in the previous explanation of risk.

checkpatch is WARNING on the tag format:
WARNING: Please use correct Fixes: style 'Fixes: <12 chars of sha1> ("<title line>")' - ie: 'Fixes: d1c6e08e7503 ("libnvdimm/labels: Add uuid helpers")' #17:

    Fixes: d1c6e08e7503649e4a4f3f9e700e2c05300b6379 ("libnvdimm/labels: Add uuid helpers")

checkpatch is also WARNING on the commit msg:
WARNING: Possible unwrapped commit description (prefer a maximum 75 chars per line) #5:
nvdimm: Replace the usage of a variable by a direct function call in nd_pfn_validate()

Also, possible only my pet peeve, the long commit message spoils my pretty 80 column view. Please trim it to not wrap here:

$git log --oneline pfn_devs.c
52b639e56a46 nvdimm: Replace the usage of a variable by a direct function call in nd_pfn_validate() c91d71363084 nvdimm: Support sizeof(struct page) > MAX_STRUCT_PAGE_SIZE 6e9f05dc66f9 libnvdimm/pfn_dev: increase MAX_STRUCT_PAGE_SIZE
81beea55cb74 nvdimm: Drop nd_device_lock()
4a0079bc7aae nvdimm: Replace lockdep_mutex with local lock classes
322cbb50de71 block: remove genhd.h

Alison

Quoted 29 lines
---
 drivers/nvdimm/pfn_devs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/nvdimm/pfn_devs.c +1 -3
index af7d9301520c..f14cbfa500ed 100644
@@ -456,7 +456,6 @@ int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
 	unsigned long align, start_pad;
 	struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
 	struct nd_namespace_common *ndns = nd_pfn->ndns;
-	const uuid_t *parent_uuid = nd_dev_to_uuid(&ndns->dev);
 
 	if (!pfn_sb || !ndns)
 		return -ENODEV;
@@ -476,7 +475,7 @@ int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
 		return -ENODEV;
 	pfn_sb->checksum = cpu_to_le64(checksum);
 
-	if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
+	if (memcmp(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16) != 0)
 		return -ENODEV;
 
 	if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
--

2.40.0

#80 lore raw

Re: [cocci] [PATCH] nvdimm: Replace the usage of a variable by a direct function call in nd_pfn_validate()

Quoted 8 lines
Quoted 7 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “nd_pfn_validate”.

Thus avoid the risk for undefined behaviour by replacing the usage of the local variable “parent_uuid” by a direct function call within a later condition check.

Quoted 5 lines

Hi Markus,

I think I understand what you are saying above, but I don't follow how that applies here. This change seems to be a nice simplification, parent_uuid, is used once, just grab it when needed.

Thanks for your positive feedback.

Quoted 1 line

What is the risk of undefined behavior?

See also:
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers?focusedCommentId=405504137#comment-405504137

Quoted 2 lines
Quoted 1 line

This issue was detected by using the Coccinelle software.

Which cocci script?

See also:
Reconsidering pointer dereferences before null pointer checks (with SmPL)
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2023-04/msg00021.html

How do you think about to review and improve any similarly affected software components?

Regards,
Markus

maintainer
#81 lore raw

Re: [PATCH] nvdimm: Replace the usage of a variable by a direct function call in nd_pfn_validate()

Quoted 8 lines · Markus Elfring
Quoted 8 lines
Quoted 7 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “nd_pfn_validate”.

Thus avoid the risk for undefined behaviour by replacing the usage of the local variable “parent_uuid” by a direct function call within a later condition check.

Quoted 7 lines
Quoted 5 lines

Hi Markus,

I think I understand what you are saying above, but I don't follow how that applies here. This change seems to be a nice simplification, parent_uuid, is used once, just grab it when needed.

Thanks for your positive feedback.

Hi Markus,

FYI - I'm a tiny bit taken aback that in response to me applying, and providing feedback, on your patch, you respond with 2 links for me to follow and cut off a chunk of my feedback.

Seems like it would taken the same amount of time to just answer my two questions directly.

Was this part of a larger patch set? Andy's comment seems to indicate that. Would have been nice to be CC'd on the cover letter.

More below...

Quoted 6 lines
Quoted 3 lines

What is the risk of undefined behavior?

See also:
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers?focusedCommentId=405504137#comment-405504137

Where is the NULL pointer dereference here?

Quoted 10 lines
Quoted 4 lines
Quoted 3 lines

This issue was detected by using the Coccinelle software.

Which cocci script?

See also:
Reconsidering pointer dereferences before null pointer checks (with SmPL)
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2023-04/msg00021.html

The cocci script linked above does not seem to apply here.

Quoted 6 lines

How do you think about to review and improve any similarly affected software components?

Regards,
Markus

#82 lore raw

Re: [cocci] nvdimm: Replace the usage of a variable by a direct function call in nd_pfn_validate()

Quoted 2 lines

FYI - I'm a tiny bit taken aback that in response to me applying, and providing feedback, on your patch,

This will probably trigger collateral evolution, won't it?

Quoted 1 line

you respond with 2 links for me to follow

I offered another bit of background information according to your enquiry.

Quoted 1 line

and cut off a chunk of my feedback.

Will this part become relevant for a subsequent patch?

Quoted 2 lines

Seems like it would taken the same amount of time to just answer my two questions directly.

Do you find linked information sources also helpful?

Quoted 1 line

Was this part of a larger patch set?

Not for this software module.

But one of my scripts for the semantic patch language pointed several update candidates out. Thus I sent 19 patches according to these change possibilities so far.
(Would you become interested to take another look by the means of mailing list archives?)

Quoted 1 line

Andy's comment seems to indicate that.

Andy Shevchenko was informed because he is involved also in the evolution of other components.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

What is the risk of undefined behavior?

See also:
https://wiki.sei.cmu.edu/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers?focusedCommentId=405504137#comment-405504137

Where is the NULL pointer dereference here?

I hope that you can become more aware that access attempts for data structure members (also by using the arrow operator) can occasionally be problematic before null pointer checks.

Quoted 9 lines
Quoted 7 lines
Quoted 2 lines
Quoted 1 line

This issue was detected by using the Coccinelle software.

Which cocci script?

See also:
Reconsidering pointer dereferences before null pointer checks (with SmPL)
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2023-04/msg00021.html

The cocci script linked above does not seem to apply here.

Which command did you try out?

Do you find the following data processing result reasonable?

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> spatch …/Projekte/Coccinelle/janitor/show_pointer_dereferences_before_check7.cocci drivers/nvdimm/pfn_devs.c …
@@ -456,9 +456,7 @@ int nd_pfn_validate(struct nd_pfn *nd_pf

        unsigned long align, start_pad;
        struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
        struct nd_namespace_common *ndns = nd_pfn->ndns;

- const uuid_t *parent_uuid = nd_dev_to_uuid(&ndns->dev);

- if (!pfn_sb || !ndns)

                return -ENODEV;

        if (!is_memory(nd_pfn->dev.parent))

Regards,
Markus

#83 lore raw

Re: [PATCH] nvdimm: Replace the usage of a variable by a direct function call in nd_pfn_validate()

In reply to Markus Elfring
Quoted 6 lines · Markus Elfring

Date: Fri, 14 Apr 2023 12:01:15 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “nd_pfn_validate”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by replacing the usage of the local variable “parent_uuid” by a direct function call within a later condition check.

This issue was detected by using the Coccinelle software.

Fixes d1c6e08e7503649e4a4f3f9e700e2c05300b6379 ("libnvdimm/labels: Add uuid helpers")

Same issues as per patch 1.

...

drivers/nvdimm/pfn_devs.c int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig) · line 478

Quoted 2 lines
drivers/nvdimm/pfn_devs.c +1 -1
-	if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
+	if (memcmp(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16) != 0)

If parent_uuid is of uuid_t type, you better to replace memcmp() with uuid_equal().

Quoted 1 line

return -ENODEV;

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#84 lore raw

PATCH 1/1 media: adv748x: Move a variable assignment behind condition checks in adv748x_hdmi_query_dv_timings()

In reply to Markus Elfring

Date: Fri, 14 Apr 2023 16:56:07 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “adv748x_hdmi_query_dv_timings”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “bt” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 3e89586a64dfd2860d596db0c84ec999d2eb5591 ("media: i2c: adv748x: add adv748x driver")
---
 drivers/media/i2c/adv748x/adv748x-hdmi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/adv748x/adv748x-hdmi.c +2 -2
index 52fa7bd75660..28757a5efd6d 100644
@@ -274,7 +274,7 @@ static int adv748x_hdmi_query_dv_timings(struct v4l2_subdev *sd,
 {
 	struct adv748x_hdmi *hdmi = adv748x_sd_to_hdmi(sd);
 	struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
-	struct v4l2_bt_timings *bt = &timings->bt;
+	struct v4l2_bt_timings *bt;
 	int pixelclock;
 	int polarity;
 
@@ -291,7 +291,7 @@ static int adv748x_hdmi_query_dv_timings(struct v4l2_subdev *sd,
 		return -ENODATA;
 
 	timings->type = V4L2_DV_BT_656_1120;
-
+	bt = &timings->bt;
 	bt->pixelclock = pixelclock;
 	bt->interlaced = hdmi_read(state, ADV748X_HDMI_F1H1) &
 				ADV748X_HDMI_F1H1_INTERLACED ?

--
2.40.0

#85 lore raw

v2 cover media: adv748x: Adjustments for adv748x_hdmi_query_dv_timings()

Date: Tue, 18 Apr 2023 11:56:11 +0200

The deletion of an if statement was requested by Hans Verkuil. This update triggered further change possibilities.

Markus Elfring (3):
Delete a null pointer check
Move a variable assignment behind condition checks Improve a size determination

 drivers/media/i2c/adv748x/adv748x-hdmi.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

--
2.40.0

#86 lore raw

PATCH v2 1/3 media: adv748x: Delete a null pointer check in adv748x_hdmi_query_dv_timings()

Date: Tue, 18 Apr 2023 09:11:39 +0200

An extra null pointer check became unwanted
for the input parameter validation in the implementation of the function “adv748x_hdmi_query_dv_timings”.
Thus omit an if statement at the beginning.

---
 drivers/media/i2c/adv748x/adv748x-hdmi.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/media/i2c/adv748x/adv748x-hdmi.c +0 -3
index 400d71c2745c..b10263f4c0c0 100644
@@ -278,9 +278,6 @@ static int adv748x_hdmi_query_dv_timings(struct v4l2_subdev *sd,
 	int pixelclock;
 	int polarity;
 
-	if (!timings)
-		return -EINVAL;
-
 	memset(timings, 0, sizeof(struct v4l2_dv_timings));
 
 	/*

--
2.40.0

#87 lore raw

PATCH v2 2/3 media: adv748x: Move a variable assignment behind condition checks in adv748x_hdmi_query_dv_timings()

In reply to Markus Elfring

Date: Tue, 18 Apr 2023 09:30:25 +0200

Convert the explicit initialisation of the local variable “bt” at the beginning to an assignment statement directly before the source code place where this pointer is used.

---

v2:
The change was rebased on source files from Linux next-20230417.

 drivers/media/i2c/adv748x/adv748x-hdmi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/adv748x/adv748x-hdmi.c +2 -2
index b10263f4c0c0..14ff27aabe89 100644
@@ -274,7 +274,7 @@ static int adv748x_hdmi_query_dv_timings(struct v4l2_subdev *sd,
 {
 	struct adv748x_hdmi *hdmi = adv748x_sd_to_hdmi(sd);
 	struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
-	struct v4l2_bt_timings *bt = &timings->bt;
+	struct v4l2_bt_timings *bt;
 	int pixelclock;
 	int polarity;
 
@@ -298,7 +298,7 @@ static int adv748x_hdmi_query_dv_timings(struct v4l2_subdev *sd,
 		return -ENODATA;
 
 	timings->type = V4L2_DV_BT_656_1120;
-
+	bt = &timings->bt;
 	bt->pixelclock = pixelclock;
 	bt->interlaced = hdmi_read(state, ADV748X_HDMI_F1H1) &
 				ADV748X_HDMI_F1H1_INTERLACED ?

--
2.40.0

#88 lore raw

PATCH v2 3/3 media: adv748x: Improve a size determination in adv748x_hdmi_query_dv_timings()

In reply to Markus Elfring

Date: Tue, 18 Apr 2023 11:00:13 +0200

Replace the specification of a data structure by a pointer dereference as the parameter for the operator "sizeof" to make the corresponding size determination a bit safer according to the Linux coding style convention.

---
 drivers/media/i2c/adv748x/adv748x-hdmi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/adv748x/adv748x-hdmi.c +1 -1
index 14ff27aabe89..13bf9983a674 100644
@@ -278,7 +278,7 @@ static int adv748x_hdmi_query_dv_timings(struct v4l2_subdev *sd,
 	int pixelclock;
 	int polarity;
 
-	memset(timings, 0, sizeof(struct v4l2_dv_timings));
+	memset(timings, 0, sizeof(*timings));
 
 	/*
 	 * If the pattern generator is enabled the device shall not be queried

--
2.40.0

#89 lore raw

PATCH 1/1 media: adv7604: Move a variable assignment behind condition checks in adv76xx_query_dv_timings()

In reply to Markus Elfring

Date: Fri, 14 Apr 2023 18:26:08 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “adv76xx_query_dv_timings”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “bt” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 54450f591c9927496b3d41c041fa802d0ef96885 ("[media] adv7604: driver for the Analog Devices ADV7604 video decoder")
---
 drivers/media/i2c/adv7604.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/media/i2c/adv7604.c +3 -1
index 9d218962d7c8..ed4ec2151d68 100644
@@ -1562,7 +1562,7 @@ static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
 {
 	struct adv76xx_state *state = to_state(sd);
 	const struct adv76xx_chip_info *info = state->info;
-	struct v4l2_bt_timings *bt = &timings->bt;
+	struct v4l2_bt_timings *bt;
 	struct stdi_readback stdi;
 
 	if (!timings)
@@ -1581,6 +1581,8 @@ static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
 		v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
 		return -ENOLINK;
 	}
+
+	bt = &timings->bt;
 	bt->interlaced = stdi.interlaced ?
 		V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
 

--
2.40.0

#90 lore raw

v2 cover media: adv7604: Adjustments for two function implementations

Date: Tue, 18 Apr 2023 19:32:19 +0200

The deletion of an if statement was requested by Hans Verkuil. This update triggered further change possibilities.

Markus Elfring (4):
Delete a null pointer check
Move a variable assignment behind condition checks Improve three size determinations
Reduce scope for the variable “info”

 drivers/media/i2c/adv7604.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

--
2.40.0

#91 lore raw

PATCH v2 1/4 media: adv7604: Delete a null pointer check in adv76xx_query_dv_timings()

Date: Tue, 18 Apr 2023 18:21:00 +0200

An extra null pointer check became unwanted
for the input parameter validation in the implementation of the function “adv76xx_query_dv_timings”.
Thus omit an if statement at the beginning.

---
 drivers/media/i2c/adv7604.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/media/i2c/adv7604.c +0 -3
index 3d0898c4175e..d92a681f9176 100644
@@ -1565,9 +1565,6 @@ static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
 	struct v4l2_bt_timings *bt = &timings->bt;
 	struct stdi_readback stdi;
 
-	if (!timings)
-		return -EINVAL;
-
 	memset(timings, 0, sizeof(struct v4l2_dv_timings));
 
 	if (no_signal(sd)) {

--
2.40.0

#92 lore raw

PATCH v2 2/4 media: adv7604: Move a variable assignment behind condition checks in adv76xx_query_dv_timings()

In reply to Markus Elfring

Date: Tue, 18 Apr 2023 18:30:41 +0200

Convert the explicit initialisation of the local variable “bt” at the beginning to an assignment statement directly before the source code place where this pointer is used.

---
 drivers/media/i2c/adv7604.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/media/i2c/adv7604.c +3 -1
index d92a681f9176..1f743b89f31b 100644
@@ -1562,7 +1562,7 @@ static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
 {
 	struct adv76xx_state *state = to_state(sd);
 	const struct adv76xx_chip_info *info = state->info;
-	struct v4l2_bt_timings *bt = &timings->bt;
+	struct v4l2_bt_timings *bt;
 	struct stdi_readback stdi;
 
 	memset(timings, 0, sizeof(struct v4l2_dv_timings));
@@ -1578,6 +1578,8 @@ static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
 		v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
 		return -ENOLINK;
 	}
+
+	bt = &timings->bt;
 	bt->interlaced = stdi.interlaced ?
 		V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
 

--
2.40.0

#93 lore raw

PATCH v2 3/4 media: adv7604: Improve three size determinations

In reply to Markus Elfring

Date: Tue, 18 Apr 2023 18:45:22 +0200

Replace the specification of data structures by pointer dereferences as the parameter for the operator "sizeof" to make the corresponding size determination a bit safer according to the Linux coding style convention.

---
 drivers/media/i2c/adv7604.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/media/i2c/adv7604.c +3 -3
index 1f743b89f31b..4d0df7f325db 100644
@@ -1464,7 +1464,7 @@ static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
 
 	if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
 		v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
-		memset(stdi, 0, sizeof(struct stdi_readback));
+		memset(stdi, 0, sizeof(*stdi));
 		return -1;
 	}
 
@@ -1565,7 +1565,7 @@ static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
 	struct v4l2_bt_timings *bt;
 	struct stdi_readback stdi;
 
-	memset(timings, 0, sizeof(struct v4l2_dv_timings));
+	memset(timings, 0, sizeof(*timings));
 
 	if (no_signal(sd)) {
 		state->restart_stdi_once = true;
@@ -1668,7 +1668,7 @@ static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
 
 	if (no_signal(sd)) {
 		v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
-		memset(timings, 0, sizeof(struct v4l2_dv_timings));
+		memset(timings, 0, sizeof(*timings));
 		return -ENOLINK;
 	}
 

--
2.40.0

#94 lore raw

PATCH v2 4/4 media: adv7604: Reduce scope for the variable “info” in adv76xx_query_dv_timings()

In reply to Markus Elfring

Date: Tue, 18 Apr 2023 19:00:10 +0200

A local variable was used only within an if branch. Thus move the definition for the variable “info” into the corresponding code block.

---
 drivers/media/i2c/adv7604.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/adv7604.c +1 -1
index 4d0df7f325db..a693427e8f45 100644
@@ -1561,7 +1561,6 @@ static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
 			struct v4l2_dv_timings *timings)
 {
 	struct adv76xx_state *state = to_state(sd);
-	const struct adv76xx_chip_info *info = state->info;
 	struct v4l2_bt_timings *bt;
 	struct stdi_readback stdi;
 
@@ -1586,6 +1585,7 @@ static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
 	if (is_digital_input(sd)) {
 		bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
 		u8 vic = 0;
+		const struct adv76xx_chip_info *info = state->info;
 		u32 w, h;
 
 		w = hdmi_read16(sd, 0x07, info->linewidth_mask);

--
2.40.0

#95 lore raw

PATCH 1/1 media: mediatek: vcodec: Move a variable assignment behind condition checks in vdec_vp9_slice_single_decode()

In reply to Markus Elfring

Date: Fri, 14 Apr 2023 20:07:01 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “vdec_vp9_slice_single_decode”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “pfc” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes b0f407c19648ae9110c932c91d6e1b9381ec0aeb ("media: mediatek: vcodec: add vp9 decoder driver for mt8186")
---
 .../media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c  | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c +2 -1
index cf16cf2807f0..22b27f7b57bf 100644
@@ -1990,7 +1990,7 @@ static int vdec_vp9_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
 					struct vdec_fb *fb, bool *res_chg)
 {
 	struct vdec_vp9_slice_instance *instance = h_vdec;
-	struct vdec_vp9_slice_pfc *pfc = &instance->sc_pfc;
+	struct vdec_vp9_slice_pfc *pfc;
 	struct vdec_vp9_slice_vsi *vsi;
 	struct mtk_vcodec_ctx *ctx;
 	int ret;
@@ -2007,6 +2007,7 @@ static int vdec_vp9_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
 	if (!fb)
 		return -EBUSY;
 
+	pfc = &instance->sc_pfc;
 	vsi = &pfc->vsi;
 
 	ret = vdec_vp9_slice_setup_single(instance, bs, fb, pfc);

--
2.40.0

maintainer Reviewed-by on v1 1/1
#96 lore raw

Re: [PATCH] media: mediatek: vcodec: Move a variable assignment behind condition checks in vdec_vp9_slice_single_decode()

Il 14/04/23 20:30, Markus Elfring ha scritto:

Quoted 6 lines

Date: Fri, 14 Apr 2023 20:07:01 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “vdec_vp9_slice_single_decode”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “pfc” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes b0f407c19648ae9110c932c91d6e1b9381ec0aeb ("media: mediatek: vcodec: add vp9 decoder driver for mt8186")
#97 lore raw

Re: [PATCH] media: mediatek: vcodec: Move a variable assignment behind condition checks in vdec_vp9_slice_single_decode()

In reply to Markus Elfring
Quoted 6 lines · Markus Elfring

Date: Fri, 14 Apr 2023 20:07:01 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “vdec_vp9_slice_single_decode”.

Quoted 6 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “pfc” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes b0f407c19648ae9110c932c91d6e1b9381ec0aeb ("media: mediatek: vcodec: add vp9 decoder driver for mt8186")

Not a fix, it was never broken.

drivers/media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c static int vdec_vp9_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs, · line 1992

Quoted 13 lines
---
 .../media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c  | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c +0 -0
index cf16cf2807f0..22b27f7b57bf 100644
@@ -1990,7 +1990,7 @@ static int vdec_vp9_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
 					struct vdec_fb *fb, bool *res_chg)
 {
 	struct vdec_vp9_slice_instance *instance = h_vdec;

Just drop these lines instead:

        if (!instance || !instance->ctx)
                return -EINVAL;

That can never happen.

Regards,

Hans

Quoted 16 lines
drivers/media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c +2 -2
-	struct vdec_vp9_slice_pfc *pfc = &instance->sc_pfc;
+	struct vdec_vp9_slice_pfc *pfc;
 	struct vdec_vp9_slice_vsi *vsi;
 	struct mtk_vcodec_ctx *ctx;
 	int ret;
@@ -2007,6 +2007,7 @@ static int vdec_vp9_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
 	if (!fb)
 		return -EBUSY;
 
+	pfc = &instance->sc_pfc;
 	vsi = &pfc->vsi;
 
 	ret = vdec_vp9_slice_setup_single(instance, bs, fb, pfc);
--

2.40.0

#98 lore raw

cover media: mediatek: vcodec: Adjustments for vdec_vp9_slice_single_decode()

Date: Mon, 17 Apr 2023 14:32:14 +0200

The deletion of an if statement was requested by Hans Verkuil. This update triggered another change possibility.

Markus Elfring (2):
Delete null pointer checks
Move variable assignments behind null pointer checks

 .../mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c        | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

--
2.40.0

#99 lore raw

PATCH 1/2 media: mediatek: vcodec: Delete null pointer checks in vdec_vp9_slice_single_decode()

Date: Mon, 17 Apr 2023 13:43:31 +0200

Extra null pointer checks became unwanted for the input parameter validation in the implementation of the function “vdec_vp9_slice_single_decode”. Thus omit an if statement at the beginning.

---
 .../media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c   | 2 --
 1 file changed, 2 deletions(-)
drivers/media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c +0 -2
index cf16cf2807f0..8430098ed0b1 100644
@@ -1995,8 +1995,6 @@ static int vdec_vp9_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
 	struct mtk_vcodec_ctx *ctx;
 	int ret;
 
-	if (!instance || !instance->ctx)
-		return -EINVAL;
 	ctx = instance->ctx;
 
 	/* bs NULL means flush decoder */

--
2.40.0

#100 lore raw

PATCH 2/2 media: mediatek: vcodec: Move variable assignments behind null pointer checks in vdec_vp9_slice_single_decode()

In reply to Markus Elfring

Date: Mon, 17 Apr 2023 14:19:13 +0200

The addresses which were assigned to the variables “instance”, “pfc” and “ctx” would not be used if a null pointer was passed by the input parameter “bs”.
Thus move these assignments behind corresponding pointer checks.

---
 .../platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c  | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c +5 -4
index 8430098ed0b1..562fe07d3731 100644
@@ -1989,22 +1989,23 @@ static int vdec_vp9_slice_get_param(void *h_vdec, enum vdec_get_param_type type,
 static int vdec_vp9_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
 					struct vdec_fb *fb, bool *res_chg)
 {
-	struct vdec_vp9_slice_instance *instance = h_vdec;
-	struct vdec_vp9_slice_pfc *pfc = &instance->sc_pfc;
+	struct vdec_vp9_slice_instance *instance;
+	struct vdec_vp9_slice_pfc *pfc;
 	struct vdec_vp9_slice_vsi *vsi;
 	struct mtk_vcodec_ctx *ctx;
 	int ret;
 
-	ctx = instance->ctx;
-
 	/* bs NULL means flush decoder */
 	if (!bs)
 		return vdec_vp9_slice_flush(h_vdec, bs, fb, res_chg);
 
+	instance = h_vdec;
+	ctx = instance->ctx;
 	fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
 	if (!fb)
 		return -EBUSY;
 
+	pfc = &instance->sc_pfc;
 	vsi = &pfc->vsi;
 
 	ret = vdec_vp9_slice_setup_single(instance, bs, fb, pfc);

--
2.40.0

#101 lore raw

PATCH 1/1 media: au0828: Move a variable assignment behind condition checks in au0828_isoc_copy()

In reply to Markus Elfring

Date: Fri, 14 Apr 2023 21:00:45 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “au0828_isoc_copy”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “vbi_dma_q” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 7f8eacd2162a39ca7fc1240883118a786f147ccb ("V4L/DVB: Add closed captioning support for the HVR-950q")
---
 drivers/media/usb/au0828/au0828-video.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/media/usb/au0828/au0828-video.c +3 -1
index fd9fc43d47e0..c0c5f2ed65e3 100644
@@ -491,7 +491,7 @@ static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 	struct au0828_buffer    *buf;
 	struct au0828_buffer    *vbi_buf;
 	struct au0828_dmaqueue  *dma_q = urb->context;
-	struct au0828_dmaqueue  *vbi_dma_q = &dev->vbiq;
+	struct au0828_dmaqueue  *vbi_dma_q;
 	unsigned char *outp = NULL;
 	unsigned char *vbioutp = NULL;
 	int i, len = 0, rc = 1;
@@ -521,6 +521,8 @@ static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 	if (vbi_buf != NULL)
 		vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
 
+	vbi_dma_q = &dev->vbiq;
+
 	for (i = 0; i < urb->number_of_packets; i++) {
 		int status = urb->iso_frame_desc[i].status;
 

--
2.40.0

#102 lore raw

Re: [PATCH] media: au0828: Move a variable assignment behind condition checks in au0828_isoc_copy()

Quoted 8 lines · Markus Elfring

Date: Fri, 14 Apr 2023 21:00:45 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “au0828_isoc_copy”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “vbi_dma_q” behind some condition checks.

Just drop these lines instead:

        if (!dev)
                return 0;

If you analyze the code then you'll see that dev is never NULL.

Quoted 4 lines

This issue was detected by using the Coccinelle software.

Fixes 7f8eacd2162a39ca7fc1240883118a786f147ccb ("V4L/DVB: Add closed captioning support for the HVR-950q")

This doesn't actually fix anything since nothing was wrong. Drop the Fixes tag. Same for all the other patches.

Regards,

Hans

Quoted 30 lines
---
 drivers/media/usb/au0828/au0828-video.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/media/usb/au0828/au0828-video.c +3 -2
index fd9fc43d47e0..c0c5f2ed65e3 100644
@@ -491,7 +491,7 @@ static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 	struct au0828_buffer    *buf;
 	struct au0828_buffer    *vbi_buf;
 	struct au0828_dmaqueue  *dma_q = urb->context;
-	struct au0828_dmaqueue  *vbi_dma_q = &dev->vbiq;
+	struct au0828_dmaqueue  *vbi_dma_q;
 	unsigned char *outp = NULL;
 	unsigned char *vbioutp = NULL;
 	int i, len = 0, rc = 1;
@@ -521,6 +521,8 @@ static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 	if (vbi_buf != NULL)
 		vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
 
+	vbi_dma_q = &dev->vbiq;
+
 	for (i = 0; i < urb->number_of_packets; i++) {
 		int status = urb->iso_frame_desc[i].status;
 
--

2.40.0

#103 lore raw

cover media: au0828: Adjustments for four function implementations

Date: Mon, 17 Apr 2023 18:32:28 +0200

The deletion of an if statement was requested by Hans Verkuil. This update triggered further change possibilities.

Markus Elfring (5):
Delete a null pointer check in au0828_isoc_copy()
Move variable assignments behind condition checks in au0828_isoc_copy() Return a status code only as a constant in au0828_isoc_copy() Delete an unnecessary return statement in two functions
Use common error handling code in au0828_init_isoc()

 drivers/media/usb/au0828/au0828-video.c | 40 +++++++++++--------------
 1 file changed, 17 insertions(+), 23 deletions(-)

--
2.40.0

#104 lore raw

PATCH 1/5 media: au0828: Delete a null pointer check in au0828_isoc_copy()

Date: Mon, 17 Apr 2023 16:05:04 +0200

An extra null pointer check became unwanted for the input parameter validation in the implementation of the function “au0828_isoc_copy”. Thus omit an if statement at the beginning.

---
 drivers/media/usb/au0828/au0828-video.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/media/usb/au0828/au0828-video.c +0 -3
index fd9fc43d47e0..ce25ddc7fc7b 100644
@@ -500,9 +500,6 @@ static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 	unsigned int vbi_field_size;
 	unsigned int remain, lencopy;
 
-	if (!dev)
-		return 0;
-
 	if (test_bit(DEV_DISCONNECTED, &dev->dev_state) ||
 	    test_bit(DEV_MISCONFIGURED, &dev->dev_state))
 		return 0;

--
2.40.0

#105 lore raw

PATCH 2/5 media: au0828: Move variable assignments behind condition checks in au0828_isoc_copy()

In reply to Markus Elfring

Date: Mon, 17 Apr 2023 16:47:24 +0200

Some local variables will be set to an appropriate value before usage. Thus omit explicit initialisations at the beginning of this function.

---
 drivers/media/usb/au0828/au0828-video.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/media/usb/au0828/au0828-video.c +7 -10
index ce25ddc7fc7b..27bb62e2b20c 100644
@@ -490,11 +490,9 @@ static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 {
 	struct au0828_buffer    *buf;
 	struct au0828_buffer    *vbi_buf;
-	struct au0828_dmaqueue  *dma_q = urb->context;
-	struct au0828_dmaqueue  *vbi_dma_q = &dev->vbiq;
-	unsigned char *outp = NULL;
-	unsigned char *vbioutp = NULL;
-	int i, len = 0, rc = 1;
+	struct au0828_dmaqueue *dma_q, *vbi_dma_q;
+	unsigned char *outp, *vbioutp;
+	int i, len, rc = 1;
 	unsigned char *p;
 	unsigned char fbyte;
 	unsigned int vbi_field_size;
@@ -511,12 +509,11 @@ static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 	}
 
 	buf = dev->isoc_ctl.buf;
-	if (buf != NULL)
-		outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
-
+	outp = (buf ? vb2_plane_vaddr(&buf->vb.vb2_buf, 0) : NULL);
 	vbi_buf = dev->isoc_ctl.vbi_buf;
-	if (vbi_buf != NULL)
-		vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
+	vbioutp = (vbi_buf ? vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0) : NULL);
+	dma_q = urb->context;
+	vbi_dma_q = &dev->vbiq;
 
 	for (i = 0; i < urb->number_of_packets; i++) {
 		int status = urb->iso_frame_desc[i].status;

--
2.40.0

#106 lore raw

PATCH 3/5 media: au0828: Return a status code only as a constant in au0828_isoc_copy()

In reply to Markus Elfring

Date: Mon, 17 Apr 2023 17:06:00 +0200

* Return a status code without storing it in an intermediate variable.

* Delete the local variable “rc” which became unnecessary with this refactoring.

---
 drivers/media/usb/au0828/au0828-video.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/au0828/au0828-video.c +2 -2
index 27bb62e2b20c..448cc296f0f7 100644
@@ -492,7 +492,7 @@ static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 	struct au0828_buffer    *vbi_buf;
 	struct au0828_dmaqueue *dma_q, *vbi_dma_q;
 	unsigned char *outp, *vbioutp;
-	int i, len, rc = 1;
+	int i, len;
 	unsigned char *p;
 	unsigned char fbyte;
 	unsigned int vbi_field_size;
@@ -613,7 +613,7 @@ static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 		if (dev->vbi_read >= vbi_field_size && buf != NULL)
 			au0828_copy_video(dev, dma_q, buf, p, outp, len);
 	}
-	return rc;
+	return 1;
 }
 
 void au0828_usb_v4l2_media_release(struct au0828_dev *dev)

--
2.40.0

#107 lore raw

PATCH 4/5 media: au0828: Delete an unnecessary return statement in two functions

In reply to Markus Elfring

Date: Mon, 17 Apr 2023 18:14:10 +0200

The script "checkpatch.pl" pointed information out like the following.

WARNING: void function return statements are not generally useful

Thus remove such a statement in the affected functions.

---
 drivers/media/usb/au0828/au0828-video.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/media/usb/au0828/au0828-video.c +0 -3
index 448cc296f0f7..ce8050186a4c 100644
@@ -405,8 +405,6 @@ static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
 	dma_q->pos = 0;
 	(*buf)->vb_buf = (*buf)->mem;
 	dev->isoc_ctl.buf = *buf;
-
-	return;
 }
 
 static void au0828_copy_vbi(struct au0828_dev *dev,
@@ -480,7 +478,6 @@ static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
 	dma_q->pos = 0;
 	(*buf)->vb_buf = (*buf)->mem;
 	dev->isoc_ctl.vbi_buf = *buf;
-	return;
 }
 
 /*

--
2.40.0

#108 lore raw

PATCH 5/5 media: au0828: Use common error handling code in au0828_init_isoc()

In reply to Markus Elfring

Date: Mon, 17 Apr 2023 18:32:10 +0200

Add two jump targets so that a bit of exception handling can be better reused at the end of this function.

---
 drivers/media/usb/au0828/au0828-video.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/media/usb/au0828/au0828-video.c +9 -6
index ce8050186a4c..9ca20f411ce8 100644
@@ -232,8 +232,7 @@ static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
 		urb = usb_alloc_urb(max_packets, GFP_KERNEL);
 		if (!urb) {
 			au0828_isocdbg("cannot allocate URB\n");
-			au0828_uninit_isoc(dev);
-			return -ENOMEM;
+			goto e_nomem;
 		}
 		dev->isoc_ctl.urb[i] = urb;
 
@@ -241,8 +240,7 @@ static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
 			sb_size, GFP_KERNEL, &urb->transfer_dma);
 		if (!dev->isoc_ctl.transfer_buffer[i]) {
 			au0828_isocdbg("cannot allocate transfer buffer\n");
-			au0828_uninit_isoc(dev);
-			return -ENOMEM;
+			goto e_nomem;
 		}
 		memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
 
@@ -271,12 +269,17 @@ static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
 		if (rc) {
 			au0828_isocdbg("submit of urb %i failed (error=%i)\n",
 				       i, rc);
-			au0828_uninit_isoc(dev);
-			return rc;
+			goto uninit_isoc;
 		}
 	}
 
 	return 0;
+
+e_nomem:
+	rc = -ENOMEM;
+uninit_isoc:
+	au0828_uninit_isoc(dev);
+	return rc;
 }
 
 /*

--
2.40.0

#109 lore raw

cover GPU-DRM-nouveau: Adjustments for seven function implementations

In reply to Markus Elfring

Date: Sun, 16 Apr 2023 11:22:23 +0200

Several update suggestions were taken into account from static source code analysis.

Markus Elfring (9):
debugfs: Move an expression into a function call parameter
in nouveau_debugfs_pstate_set()
debugfs: Move a variable assignment behind a null pointer check
in nouveau_debugfs_pstate_get()
debugfs: Use seq_putc()
in nouveau_debugfs_pstate_get()
debugfs: Replace five seq_printf() calls by seq_puts()
in nouveau_debugfs_pstate_get()
power_budget: Move an expression into a macro call parameter
in nvbios_power_budget_header()
clk: Move a variable assignment behind a null pointer check
in nvkm_pstate_new()
pci: Move a variable assignment behind condition checks
in nvkm_pcie_set_link()
pci: Move an expression into a function call parameter
in nvkm_pcie_set_link()
therm: Move an assignment statement behind a null pointer check
in two functions

 drivers/gpu/drm/nouveau/nouveau_debugfs.c     | 19 ++++++++++---------
 .../nouveau/nvkm/subdev/bios/power_budget.c   |  3 +--
 .../gpu/drm/nouveau/nvkm/subdev/clk/base.c    |  2 +-
 .../gpu/drm/nouveau/nvkm/subdev/pci/pcie.c    |  7 +++----
 .../drm/nouveau/nvkm/subdev/therm/fanpwm.c    |  2 +-
 .../drm/nouveau/nvkm/subdev/therm/fantog.c    |  2 +-
 6 files changed, 17 insertions(+), 18 deletions(-)

--
2.40.0

#110 lore raw

PATCH 1/9 drm/nouveau/debugfs: Move an expression into a function call parameter in nouveau_debugfs_pstate_set()

Date: Sat, 15 Apr 2023 21:06:06 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “nouveau_debugfs_pstate_set”.

Thus avoid the risk for undefined behaviour by moving the usage of an expression into a parameter for a function call at the end.

This issue was detected by using the Coccinelle software.

Fixes 6e9fc177399f08446293fec7607913fdbc95e191 ("drm/nouveau/debugfs: add copy of sysfs pstate interface ported to debugfs")
---
 drivers/gpu/drm/nouveau/nouveau_debugfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nouveau_debugfs.c +2 -2
index 2a36d1ca8fda..44e26b6e74c7 100644
@@ -144,7 +144,6 @@ nouveau_debugfs_pstate_set(struct file *file, const char __user *ubuf,
 	struct seq_file *m = file->private_data;
 	struct drm_device *drm = m->private;
 	struct nouveau_debugfs *debugfs = nouveau_debugfs(drm);
-	struct nvif_object *ctrl = &debugfs->ctrl;
 	struct nvif_control_pstate_user_v0 args = { .pwrsrc = -EINVAL };
 	char buf[32] = {}, *tmp, *cur = buf;
 	long value, ret;
@@ -188,7 +187,8 @@ nouveau_debugfs_pstate_set(struct file *file, const char __user *ubuf,
 		return ret;
 	}
 
-	ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_USER, &args, sizeof(args));
+	ret = nvif_mthd(&debugfs->ctrl, NVIF_CONTROL_PSTATE_USER,
+			&args, sizeof(args));
 	pm_runtime_put_autosuspend(drm->dev);
 	if (ret < 0)
 		return ret;

--
2.40.0

#111 lore raw

PATCH 2/9 drm/nouveau/debugfs: Move a variable assignment behind a null pointer check in nouveau_debugfs_pstate_get()

In reply to Markus Elfring

Date: Sat, 15 Apr 2023 21:24:43 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “nouveau_debugfs_pstate_get”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “ctrl” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 6e9fc177399f08446293fec7607913fdbc95e191 ("drm/nouveau/debugfs: add copy of sysfs pstate interface ported to debugfs")
---
 drivers/gpu/drm/nouveau/nouveau_debugfs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_debugfs.c +2 -1
index 44e26b6e74c7..a859a086f308 100644
@@ -73,13 +73,14 @@ nouveau_debugfs_pstate_get(struct seq_file *m, void *data)
 {
 	struct drm_device *drm = m->private;
 	struct nouveau_debugfs *debugfs = nouveau_debugfs(drm);
-	struct nvif_object *ctrl = &debugfs->ctrl;
+	struct nvif_object *ctrl;
 	struct nvif_control_pstate_info_v0 info = {};
 	int ret, i;
 
 	if (!debugfs)
 		return -ENODEV;
 
+	ctrl = &debugfs->ctrl;
 	ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_INFO, &info, sizeof(info));
 	if (ret)
 		return ret;

--
2.40.0

#112 lore raw

PATCH 3/9 drm/nouveau/debugfs: Use seq_putc() in nouveau_debugfs_pstate_get()

In reply to Markus Elfring

Date: Sat, 15 Apr 2023 21:48:47 +0200

A single character (line break) should be put into a sequence. Thus use the corresponding function “seq_putc”.

This issue was detected by using the Coccinelle software.

---
 drivers/gpu/drm/nouveau/nouveau_debugfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_debugfs.c +1 -1
index a859a086f308..13c82eea8828 100644
@@ -132,7 +132,7 @@ nouveau_debugfs_pstate_get(struct seq_file *m, void *data)
 				seq_printf(m, " DC");
 		}
 
-		seq_printf(m, "\n");
+		seq_putc(m, '\n');
 	}
 
 	return 0;

--
2.40.0

#113 lore raw

PATCH 4/9 drm/nouveau/debugfs: Replace five seq_printf() calls by seq_puts() in nouveau_debugfs_pstate_get()

In reply to Markus Elfring

Date: Sat, 15 Apr 2023 22:02:31 +0200

Five strings which did not contain a data format specification should be put into a sequence. Thus use the corresponding function “seq_puts”.

This issue was detected by using the Coccinelle software.

---
 drivers/gpu/drm/nouveau/nouveau_debugfs.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/gpu/drm/nouveau/nouveau_debugfs.c +5 -5
index 13c82eea8828..99d022a91afc 100644
@@ -120,16 +120,16 @@ nouveau_debugfs_pstate_get(struct seq_file *m, void *data)
 
 		if (state >= 0) {
 			if (info.ustate_ac == state)
-				seq_printf(m, " AC");
+				seq_puts(m, " AC");
 			if (info.ustate_dc == state)
-				seq_printf(m, " DC");
+				seq_puts(m, " DC");
 			if (info.pstate == state)
-				seq_printf(m, " *");
+				seq_puts(m, " *");
 		} else {
 			if (info.ustate_ac < -1)
-				seq_printf(m, " AC");
+				seq_puts(m, " AC");
 			if (info.ustate_dc < -1)
-				seq_printf(m, " DC");
+				seq_puts(m, " DC");
 		}
 
 		seq_putc(m, '\n');

--
2.40.0

#114 lore raw

PATCH 5/9 drm/nouveau/bios/power_budget: Move an expression into a macro call parameter in nvbios_power_budget_header()

In reply to Markus Elfring

Date: Sat, 15 Apr 2023 22:30:30 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “nvbios_power_budget_header”.

Thus avoid the risk for undefined behaviour by moving the usage of an expression into a parameter for a macro call in one if branch.

This issue was detected by using the Coccinelle software.

Fixes e5f8eabc0077ea3f77b3362e28d3969ae62e70f0 ("drm/nouveau/bios/power_budget: Add basic power budget parsing")
---
 drivers/gpu/drm/nouveau/nvkm/subdev/bios/power_budget.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nvkm/subdev/bios/power_budget.c +1 -2
index 03d2f970a29f..2ba992bdb19d 100644
@@ -59,7 +59,6 @@ int
 nvbios_power_budget_header(struct nvkm_bios *bios,
                            struct nvbios_power_budget *budget)
 {
-	struct nvkm_subdev *subdev = &bios->subdev;
 	u8 ver, hdr, cnt, len, cap_entry;
 	u32 header;
 
@@ -82,7 +81,7 @@ nvbios_power_budget_header(struct nvkm_bios *bios,
 	}
 
 	if (cap_entry >= cnt && cap_entry != 0xff) {
-		nvkm_warn(subdev,
+		nvkm_warn(&bios->subdev,
 		          "invalid cap_entry in power budget table found\n");
 		budget->cap_entry = 0xff;
 		return -EINVAL;

--
2.40.0

#115 lore raw

PATCH 6/9 drm/nouveau/clk: Move a variable assignment behind a null pointer check in nvkm_pstate_new()

In reply to Markus Elfring

Date: Sun, 16 Apr 2023 07:45:54 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “nvkm_pstate_new”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “cstate” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 7c856522069755ab9d163a24ac332cd3cb35fe30 ("drm/nouveau/clk: implement power state and engine clock control in core")
---
 drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +1 -1
index da07a2fbef06..178dc56909c2 100644
@@ -417,7 +417,6 @@ nvkm_pstate_new(struct nvkm_clk *clk, int idx)
 		return 0;
 
 	pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
-	cstate = &pstate->base;
 	if (!pstate)
 		return -ENOMEM;
 
@@ -427,6 +426,7 @@ nvkm_pstate_new(struct nvkm_clk *clk, int idx)
 	pstate->fanspeed = perfE.fanspeed;
 	pstate->pcie_speed = perfE.pcie_speed;
 	pstate->pcie_width = perfE.pcie_width;
+	cstate = &pstate->base;
 	cstate->voltage = perfE.voltage;
 	cstate->domain[nv_clk_src_core] = perfE.core;
 	cstate->domain[nv_clk_src_shader] = perfE.shader;

--
2.40.0

#116 lore raw

PATCH 7/9 drm/nouveau/pci: Move a variable assignment behind condition checks in nvkm_pcie_set_link()

In reply to Markus Elfring

Date: Sun, 16 Apr 2023 08:18:40 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “nvkm_pcie_set_link”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “subdev” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes bcc19d9bf5cd8d49428c487adced1aa101271b18 ("drm/nouveau/pci: implement generic code for pcie speed change")
---
 drivers/gpu/drm/nouveau/nvkm/subdev/pci/pcie.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nvkm/subdev/pci/pcie.c +2 -1
index d71e5db5028a..b295f100e1c1 100644
@@ -114,7 +114,7 @@ nvkm_pcie_init(struct nvkm_pci *pci)
 int
 nvkm_pcie_set_link(struct nvkm_pci *pci, enum nvkm_pcie_speed speed, u8 width)
 {
-	struct nvkm_subdev *subdev = &pci->subdev;
+	struct nvkm_subdev *subdev;
 	enum nvkm_pcie_speed cur_speed, max_speed;
 	struct pci_bus *pbus;
 	int ret;
@@ -126,6 +126,7 @@ nvkm_pcie_set_link(struct nvkm_pci *pci, enum nvkm_pcie_speed speed, u8 width)
 	if (!pci->func->pcie.set_link)
 		return -ENOSYS;
 
+	subdev = &pci->subdev;
 	nvkm_trace(subdev, "requested %s\n", nvkm_pcie_speeds[speed]);
 
 	if (pci->func->pcie.version(pci) < 2) {

--
2.40.0

#117 lore raw

PATCH 8/9 drm/nouveau/pci: Move an expression into a function call parameter in nvkm_pcie_set_link()

In reply to Markus Elfring

Date: Sun, 16 Apr 2023 08:45:31 +0200

The variable “pbus” was read only once in the implementation of the function “nvkm_pcie_set_link”.
Thus move the usage of an expression into a parameter for a function call.

---
 drivers/gpu/drm/nouveau/nvkm/subdev/pci/pcie.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/gpu/drm/nouveau/nvkm/subdev/pci/pcie.c +1 -3
index b295f100e1c1..dd18d9d0bade 100644
@@ -116,12 +116,10 @@ nvkm_pcie_set_link(struct nvkm_pci *pci, enum nvkm_pcie_speed speed, u8 width)
 {
 	struct nvkm_subdev *subdev;
 	enum nvkm_pcie_speed cur_speed, max_speed;
-	struct pci_bus *pbus;
 	int ret;
 
 	if (!pci || !pci_is_pcie(pci->pdev))
 		return 0;
-	pbus = pci->pdev->bus;
 
 	if (!pci->func->pcie.set_link)
 		return -ENOSYS;
@@ -135,7 +133,7 @@ nvkm_pcie_set_link(struct nvkm_pci *pci, enum nvkm_pcie_speed speed, u8 width)
 	}
 
 	cur_speed = pci->func->pcie.cur_speed(pci);
-	max_speed = min(nvkm_pcie_speed(pbus->max_bus_speed),
+	max_speed = min(nvkm_pcie_speed(pci->pdev->bus->max_bus_speed),
 			pci->func->pcie.max_speed(pci));
 
 	nvkm_trace(subdev, "current speed: %s\n", nvkm_pcie_speeds[cur_speed]);

--
2.40.0

#118 lore raw

PATCH 9/9 drm/nouveau/therm: Move an assignment statement behind a null pointer check in two functions

In reply to Markus Elfring

Date: Sun, 16 Apr 2023 10:50:12 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the functions “nvkm_fanpwm_create” and “nvkm_fantog_create”.

Thus avoid the risk for undefined behaviour by moving the assignment for the data structure member “fan” behind two null pointer checks.

This issue was detected by using the Coccinelle software.

Fixes da06b46b720687117178d3ee85a601762f1c36b5 ("drm/nouveau/therm: cosmetic changes")
---
 drivers/gpu/drm/nouveau/nvkm/subdev/therm/fanpwm.c | 2 +-
 drivers/gpu/drm/nouveau/nvkm/subdev/therm/fantog.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nvkm/subdev/therm/fanpwm.c +1 -1
index 340f37a299dc..b13ba9b2f6be 100644
@@ -98,10 +98,10 @@ nvkm_fanpwm_create(struct nvkm_therm *therm, struct dcb_gpio_func *func)
 		return -ENODEV;
 
 	fan = kzalloc(sizeof(*fan), GFP_KERNEL);
-	therm->fan = &fan->base;
 	if (!fan)
 		return -ENOMEM;
 
+	therm->fan = &fan->base;
 	fan->base.type = "PWM";
 	fan->base.get = nvkm_fanpwm_get;
 	fan->base.set = nvkm_fanpwm_set;
drivers/gpu/drm/nouveau/nvkm/subdev/therm/fantog.c +1 -1
index ff9fbe7950e5..bfdf4ca5625c 100644
@@ -100,10 +100,10 @@ nvkm_fantog_create(struct nvkm_therm *therm, struct dcb_gpio_func *func)
 	}
 
 	fan = kzalloc(sizeof(*fan), GFP_KERNEL);
-	therm->fan = &fan->base;
 	if (!fan)
 		return -ENOMEM;
 
+	therm->fan = &fan->base;
 	fan->base.type = "toggle";
 	fan->base.get = nvkm_fantog_get;
 	fan->base.set = nvkm_fantog_set;

--
2.40.0

Reviewed-by on the v1 series
#119 lore raw

Re: [PATCH 0/9] GPU-DRM-nouveau: Adjustments for seven function implementations

In reply to Markus Elfring
Quoted 6 lines · Markus Elfring

Date: Sun, 16 Apr 2023 11:22:23 +0200

Several update suggestions were taken into account from static source code analysis.

Quoted 31 lines

Markus Elfring (9):
debugfs: Move an expression into a function call parameter
in nouveau_debugfs_pstate_set()
debugfs: Move a variable assignment behind a null pointer check
in nouveau_debugfs_pstate_get()
debugfs: Use seq_putc()
in nouveau_debugfs_pstate_get()
debugfs: Replace five seq_printf() calls by seq_puts()
in nouveau_debugfs_pstate_get()
power_budget: Move an expression into a macro call parameter
in nvbios_power_budget_header()
clk: Move a variable assignment behind a null pointer check
in nvkm_pstate_new()
pci: Move a variable assignment behind condition checks
in nvkm_pcie_set_link()
pci: Move an expression into a function call parameter
in nvkm_pcie_set_link()
therm: Move an assignment statement behind a null pointer check
in two functions

 drivers/gpu/drm/nouveau/nouveau_debugfs.c     | 19 ++++++++++---------
 .../nouveau/nvkm/subdev/bios/power_budget.c   |  3 +--
 .../gpu/drm/nouveau/nvkm/subdev/clk/base.c    |  2 +-
 .../gpu/drm/nouveau/nvkm/subdev/pci/pcie.c    |  7 +++----
 .../drm/nouveau/nvkm/subdev/therm/fanpwm.c    |  2 +-
 .../drm/nouveau/nvkm/subdev/therm/fantog.c    |  2 +-
 6 files changed, 17 insertions(+), 18 deletions(-)

--
2.40.0

#120 lore raw

PATCH 1/1 drm/bridge: it6505: Move a variable assignment behind a null pointer check in receive_timing_debugfs_show()

In reply to Markus Elfring

Date: Sun, 16 Apr 2023 17:30:46 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “receive_timing_debugfs_show”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “vid” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes b5c84a9edcd418cd055becad6a22439e7c5e3bf8 ("drm/bridge: add it6505 driver")
---
 drivers/gpu/drm/bridge/ite-it6505.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/bridge/ite-it6505.c +2 -1
index abaf6e23775e..45f579c365e7 100644
@@ -3207,7 +3207,7 @@ static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
 					   size_t len, loff_t *ppos)
 {
 	struct it6505 *it6505 = file->private_data;
-	struct drm_display_mode *vid = &it6505->video_info;
+	struct drm_display_mode *vid;
 	u8 read_buf[READ_BUFFER_SIZE];
 	u8 *str = read_buf, *end = read_buf + READ_BUFFER_SIZE;
 	ssize_t ret, count;
@@ -3216,6 +3216,7 @@ static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
 		return -ENODEV;
 
 	it6505_calc_video_info(it6505);
+	vid = &it6505->video_info;
 	str += scnprintf(str, end - str, "---video timing---\n");
 	str += scnprintf(str, end - str, "PCLK:%d.%03dMHz\n",
 			 vid->clock / 1000, vid->clock % 1000);

--
2.40.0

maintainer Reviewed-by on v1 1/1
#121 lore raw

Re: [PATCH] drm/bridge: it6505: Move a variable assignment behind a null pointer check in receive_timing_debugfs_show()

Hey Markus,

This patch seems to be a part of a series without being marked as such, this causes issues when importing this patch with maintainer tools like b4 which automatically pull in the entire series and not just the specific patch. Either label the patch as being part of a series ( [PATCH 1/XX] ), or submit it separately.

Quoted 7 lines · Markus Elfring

Date: Sun, 16 Apr 2023 17:30:46 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “receive_timing_debugfs_show”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “vid” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes b5c84a9edcd418cd055becad6a22439e7c5e3bf8 ("drm/bridge: add it6505 driver")

The email in the Signed-off tag should match the email of the sender, which it doesn't.

With the two above issues fixed, please add my r-b.

Quoted 28 lines
---
 drivers/gpu/drm/bridge/ite-it6505.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/bridge/ite-it6505.c +2 -2
index abaf6e23775e..45f579c365e7 100644
@@ -3207,7 +3207,7 @@ static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
                                           size_t len, loff_t *ppos)
 {
        struct it6505 *it6505 = file->private_data;
-       struct drm_display_mode *vid = &it6505->video_info;
+       struct drm_display_mode *vid;
        u8 read_buf[READ_BUFFER_SIZE];
        u8 *str = read_buf, *end = read_buf + READ_BUFFER_SIZE;
        ssize_t ret, count;
@@ -3216,6 +3216,7 @@ static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
                return -ENODEV;
 
        it6505_calc_video_info(it6505);
+       vid = &it6505->video_info;
        str += scnprintf(str, end - str, "---video timing---\n");
        str += scnprintf(str, end - str, "PCLK:%d.%03dMHz\n",
                         vid->clock / 1000, vid->clock % 1000);
--

2.40.0

#122 lore raw

Re: [cocci] drm/bridge: it6505: Move a variable assignment behind a null pointer check in receive_timing_debugfs_show()

Quoted 1 line

This patch seems to be a part of a series without being marked as such,

The mentioned patch affects only a single function implementation.

Quoted 3 lines

this causes issues when importing this patch with maintainer tools like b4 which automatically pull in the entire series and not just the specific patch.

It is a pity that there are special technical difficulties.

Quoted 1 line

Either label the patch as being part of a series ( [PATCH 1/XX] ),

Further software modules were similarly affected.

See also:
Reconsidering pointer dereferences before null pointer checks (with SmPL)
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2023-04/msg00021.html

Quoted 1 line

or submit it separately.

I thought that I did that (in principle).

Regards,
Markus

maintainer
#123 lore raw

Re: drm/bridge: it6505: Move a variable assignment behind a null pointer check in receive_timing_debugfs_show()

Quoted 23 lines · Markus Elfring
Quoted 2 lines

This patch seems to be a part of a series without being marked as such,

The mentioned patch affects only a single function implementation.

Quoted 3 lines

this causes issues when importing this patch with maintainer tools like b4 which automatically pull in the entire series and not just the specific patch.

It is a pity that there are special technical difficulties.

Quoted 1 line

Either label the patch as being part of a series ( [PATCH 1/XX] ),

Further software modules were similarly affected.

See also:
Reconsidering pointer dereferences before null pointer checks (with SmPL)
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2023-04/msg00021.html

Quoted 3 lines
Quoted 1 line

or submit it separately.

I thought that I did that (in principle).

You can have a look at LKML for the email message-id to see the whole thread of patches.
https://lore.kernel.org/all/[email protected]/#r

Or https://lore.kernel.org/all/$MSG_ID

Fix the email Sign-off email != Sender email issue, resubmit and I'll be able to apply this.

#124 lore raw

Re: [cocci] drm/bridge: it6505: Move a variable assignment behind a null pointer check in receive_timing_debugfs_show()

Quoted 2 lines

Fix the email Sign-off email != Sender email issue, resubmit and I'll be able to apply this.

You can pick the email from my tag “Signed-off-by” up also directly as an ordinary patch author email, can't you?

Regards,
Markus

maintainer
#125 lore raw

Re: drm/bridge: it6505: Move a variable assignment behind a null pointer check in receive_timing_debugfs_show()

Quoted 6 lines · Markus Elfring
Quoted 3 lines

Fix the email Sign-off email != Sender email issue, resubmit and I'll be able to apply this.

You can pick the email from my tag “Signed-off-by” up also directly as an ordinary patch author email, can't you?

Of course, I can change the email to anything, but drm maintainer scripts checks for this, presumably for a reason, so it should be correctly submitted.

Quoted 4 lines

Regards,
Markus

#126 lore raw

PATCH 1/1 drm/bridge: it6505: Move a variable assignment behind a null pointer check in receive_timing_debugfs_show()

From: Markus Elfring <[email protected]>
Date: Sun, 16 Apr 2023 17:30:46 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “receive_timing_debugfs_show”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “vid” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes b5c84a9edcd418cd055becad6a22439e7c5e3bf8 ("drm/bridge: add it6505 driver")
---
 drivers/gpu/drm/bridge/ite-it6505.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/bridge/ite-it6505.c +2 -1
index abaf6e23775e..45f579c365e7 100644
@@ -3207,7 +3207,7 @@ static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
 					   size_t len, loff_t *ppos)
 {
 	struct it6505 *it6505 = file->private_data;
-	struct drm_display_mode *vid = &it6505->video_info;
+	struct drm_display_mode *vid;
 	u8 read_buf[READ_BUFFER_SIZE];
 	u8 *str = read_buf, *end = read_buf + READ_BUFFER_SIZE;
 	ssize_t ret, count;
@@ -3216,6 +3216,7 @@ static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
 		return -ENODEV;
 
 	it6505_calc_video_info(it6505);
+	vid = &it6505->video_info;
 	str += scnprintf(str, end - str, "---video timing---\n");
 	str += scnprintf(str, end - str, "PCLK:%d.%03dMHz\n",
 			 vid->clock / 1000, vid->clock % 1000);

--
2.40.0

maintainer Applied
#127 lore raw

Re: [PATCH resent] drm/bridge: it6505: Move a variable assignment behind a null pointer check in receive_timing_debugfs_show()

Quoted 41 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Sun, 16 Apr 2023 17:30:46 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “receive_timing_debugfs_show”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “vid” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes b5c84a9edcd418cd055becad6a22439e7c5e3bf8 ("drm/bridge: add it6505 driver")
---
 drivers/gpu/drm/bridge/ite-it6505.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/bridge/ite-it6505.c +2 -2
index abaf6e23775e..45f579c365e7 100644
@@ -3207,7 +3207,7 @@ static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
                                           size_t len, loff_t *ppos)
 {
        struct it6505 *it6505 = file->private_data;
-       struct drm_display_mode *vid = &it6505->video_info;
+       struct drm_display_mode *vid;
        u8 read_buf[READ_BUFFER_SIZE];
        u8 *str = read_buf, *end = read_buf + READ_BUFFER_SIZE;
        ssize_t ret, count;
@@ -3216,6 +3216,7 @@ static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
                return -ENODEV;
 
        it6505_calc_video_info(it6505);
+       vid = &it6505->video_info;
        str += scnprintf(str, end - str, "---video timing---\n");
        str += scnprintf(str, end - str, "PCLK:%d.%03dMHz\n",
                         vid->clock / 1000, vid->clock % 1000);
--
Quoted 2 lines

2.40.0

Applied to drm-misc-next.

#128 lore raw

PATCH 1/1 drm/mm: Adjust input parameter validation in DECLARE_NEXT_HOLE_ADDR()

In reply to Markus Elfring

Date: Mon, 17 Apr 2023 11:26:34 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the macro “DECLARE_NEXT_HOLE_ADDR”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “node” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 5fad79fd66ff90b8c0a95319dad0b099008f8347 ("drm/mm: cleanup and improve next_hole_*_addr()")
---
 drivers/gpu/drm/drm_mm.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_mm.c +6 -2
index 8257f9d4f619..95c316aa36e5 100644
@@ -389,9 +389,13 @@ first_hole(struct drm_mm *mm,
 #define DECLARE_NEXT_HOLE_ADDR(name, first, last)			\
 static struct drm_mm_node *name(struct drm_mm_node *entry, u64 size)	\
 {									\
-	struct rb_node *parent, *node = &entry->rb_hole_addr;		\
+	struct rb_node *parent, *node;					\
 									\
-	if (!entry || RB_EMPTY_NODE(node))				\
+	if (!entry)							\
+		return NULL;						\
+									\
+	node = &entry->rb_hole_addr;					\
+	if (RB_EMPTY_NODE(node))					\
 		return NULL;						\
 									\
 	if (usable_hole_addr(node->first, size)) {			\

--
2.40.0

#129 lore raw

PATCH RESEND 1/1 drm/mm: Adjust input parameter validation in DECLARE_NEXT_HOLE_ADDR()

From: Markus Elfring <[email protected]>
Date: Mon, 17 Apr 2023 11:26:34 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the macro “DECLARE_NEXT_HOLE_ADDR”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “node” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 5fad79fd66ff ("drm/mm: cleanup and improve next_hole_*_addr()")
---
 drivers/gpu/drm/drm_mm.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_mm.c +6 -2
index 8257f9d4f619..95c316aa36e5 100644
@@ -389,9 +389,13 @@ first_hole(struct drm_mm *mm,
 #define DECLARE_NEXT_HOLE_ADDR(name, first, last)			\
 static struct drm_mm_node *name(struct drm_mm_node *entry, u64 size)	\
 {									\
-	struct rb_node *parent, *node = &entry->rb_hole_addr;		\
+	struct rb_node *parent, *node;					\
 									\
-	if (!entry || RB_EMPTY_NODE(node))				\
+	if (!entry)							\
+		return NULL;						\
+									\
+	node = &entry->rb_hole_addr;					\
+	if (RB_EMPTY_NODE(node))					\
 		return NULL;						\
 									\
 	if (usable_hole_addr(node->first, size)) {			\

--
2.40.0

#130 lore raw

PATCH 1/1 iwlegacy: Adjust input parameter validation in il_set_ht_add_station()

In reply to Markus Elfring

Date: Wed, 19 Apr 2023 18:35:55 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “il_set_ht_add_station”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “sta_ht_inf” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Delete also the jump target “done” by using return statements directly for two if branches.

Fixes 046d2e7c50e3087a32a85fd384c21f896dbccf83 ("mac80211: prepare sta handling for MLO support")
Fixes e7392364fcd1004a5e495f15cf21b1e0ef874215 ("iwlegacy: indentions and whitespaces")
---
 drivers/net/wireless/intel/iwlegacy/common.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/wireless/intel/iwlegacy/common.c +7 -5
index 96002121bb8b..8f6fd17b02a8 100644
@@ -1863,11 +1863,15 @@ EXPORT_SYMBOL(il_send_add_sta);
 static void
 il_set_ht_add_station(struct il_priv *il, u8 idx, struct ieee80211_sta *sta)
 {
-	struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->deflink.ht_cap;
+	struct ieee80211_sta_ht_cap *sta_ht_inf;
 	__le32 sta_flags;
 
-	if (!sta || !sta_ht_inf->ht_supported)
-		goto done;
+	if (!sta)
+		return;
+
+	sta_ht_inf = &sta->deflink.ht_cap;
+	if (!sta_ht_inf->ht_supported)
+		return;
 
 	D_ASSOC("spatial multiplexing power save mode: %s\n",
 		(sta->deflink.smps_mode == IEEE80211_SMPS_STATIC) ? "static" :
@@ -1906,8 +1910,6 @@ il_set_ht_add_station(struct il_priv *il, u8 idx, struct ieee80211_sta *sta)
 		sta_flags &= ~STA_FLG_HT40_EN_MSK;
 
 	il->stations[idx].sta.station_flags = sta_flags;
-done:
-	return;
 }
 
 /*

--
2.40.0

#131 lore raw

PATCH RESEND 1/1 iwlegacy: Adjust input parameter validation in il_set_ht_add_station()

From: Markus Elfring <[email protected]>
Date: Wed, 19 Apr 2023 18:35:55 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “il_set_ht_add_station”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “sta_ht_inf” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Delete also the jump target “done” by using return statements directly for two if branches.

Fixes 046d2e7c50e3 ("mac80211: prepare sta handling for MLO support")
Fixes e7392364fcd1 ("iwlegacy: indentions and whitespaces")
---
 drivers/net/wireless/intel/iwlegacy/common.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/wireless/intel/iwlegacy/common.c +7 -5
index 96002121bb8b..8f6fd17b02a8 100644
@@ -1863,11 +1863,15 @@ EXPORT_SYMBOL(il_send_add_sta);
 static void
 il_set_ht_add_station(struct il_priv *il, u8 idx, struct ieee80211_sta *sta)
 {
-	struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->deflink.ht_cap;
+	struct ieee80211_sta_ht_cap *sta_ht_inf;
 	__le32 sta_flags;
 
-	if (!sta || !sta_ht_inf->ht_supported)
-		goto done;
+	if (!sta)
+		return;
+
+	sta_ht_inf = &sta->deflink.ht_cap;
+	if (!sta_ht_inf->ht_supported)
+		return;
 
 	D_ASSOC("spatial multiplexing power save mode: %s\n",
 		(sta->deflink.smps_mode == IEEE80211_SMPS_STATIC) ? "static" :
@@ -1906,8 +1910,6 @@ il_set_ht_add_station(struct il_priv *il, u8 idx, struct ieee80211_sta *sta)
 		sta_flags &= ~STA_FLG_HT40_EN_MSK;
 
 	il->stations[idx].sta.station_flags = sta_flags;
-done:
-	return;
 }
 
 /*

--
2.40.0

#132 lore raw

PATCH 1/1 iwlwifi: Adjust input parameter validation in iwl_sta_calc_ht_flags()

In reply to Markus Elfring

Date: Wed, 19 Apr 2023 19:19:34 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “iwl_sta_calc_ht_flags”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “sta_ht_inf” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 046d2e7c50e3087a32a85fd384c21f896dbccf83 ("mac80211: prepare sta handling for MLO support")
---
 drivers/net/wireless/intel/iwlwifi/dvm/sta.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/wireless/intel/iwlwifi/dvm/sta.c +6 -2
index cef43cf80620..74814ce0155e 100644
@@ -147,7 +147,7 @@ static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
 				  struct iwl_rxon_context *ctx,
 				  __le32 *flags, __le32 *mask)
 {
-	struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->deflink.ht_cap;
+	struct ieee80211_sta_ht_cap *sta_ht_inf;
 
 	*mask = STA_FLG_RTS_MIMO_PROT_MSK |
 		STA_FLG_MIMO_DIS_MSK |
@@ -156,7 +156,11 @@ static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
 		STA_FLG_AGG_MPDU_DENSITY_MSK;
 	*flags = 0;
 
-	if (!sta || !sta_ht_inf->ht_supported)
+	if (!sta)
+		return;
+
+	sta_ht_inf = &sta->deflink.ht_cap;
+	if (!sta_ht_inf->ht_supported)
 		return;
 
 	IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",

--
2.40.0

#133 lore raw

PATCH RESEND 1/1 iwlwifi: Adjust input parameter validation in iwl_sta_calc_ht_flags()

From: Markus Elfring <[email protected]>
Date: Wed, 19 Apr 2023 19:19:34 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “iwl_sta_calc_ht_flags”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “sta_ht_inf” behind the null pointer check.

This issue was detected by using the Coccinelle software.

Fixes 046d2e7c50e3 ("mac80211: prepare sta handling for MLO support")
---
 drivers/net/wireless/intel/iwlwifi/dvm/sta.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/wireless/intel/iwlwifi/dvm/sta.c +6 -2
index cef43cf80620..74814ce0155e 100644
@@ -147,7 +147,7 @@ static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
 				  struct iwl_rxon_context *ctx,
 				  __le32 *flags, __le32 *mask)
 {
-	struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->deflink.ht_cap;
+	struct ieee80211_sta_ht_cap *sta_ht_inf;
 
 	*mask = STA_FLG_RTS_MIMO_PROT_MSK |
 		STA_FLG_MIMO_DIS_MSK |
@@ -156,7 +156,11 @@ static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
 		STA_FLG_AGG_MPDU_DENSITY_MSK;
 	*flags = 0;
 
-	if (!sta || !sta_ht_inf->ht_supported)
+	if (!sta)
+		return;
+
+	sta_ht_inf = &sta->deflink.ht_cap;
+	if (!sta_ht_inf->ht_supported)
 		return;
 
 	IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",

--
2.40.0

#134 lore raw

Re: [PATCH RESEND] iwlwifi: Adjust input parameter validation in iwl_sta_calc_ht_flags()

Quoted 9 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 19 Apr 2023 19:19:34 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “iwl_sta_calc_ht_flags”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “sta_ht_inf” behind the null pointer check.

I am a bit confused, I don't see any risk of undefined behaviour here.

The change is obviously fine, and I guess one can argue that it is less confusing as the compiler will generate a warning if one uses the variable before assignment.

However, the code is both well defined and correct. If sta is NULL then sta_ht_inf is never used, so the fact that it is effectively a NULL pointer [offsetof(struct ieee80211_sta, deflink.ht_cap)] does not matter.

Benjamin

Quoted 38 lines

This issue was detected by using the Coccinelle software.

Fixes 046d2e7c50e3 ("mac80211: prepare sta handling for MLO

support")

---
 drivers/net/wireless/intel/iwlwifi/dvm/sta.c | 8 ++++++--

 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/wireless/intel/iwlwifi/dvm/sta.c +1 -1
index cef43cf80620..74814ce0155e 100644
@@ -147,7 +147,7 @@ static void iwl_sta_calc_ht_flags(struct iwl_priv
 *priv,
  				  struct iwl_rxon_context *ctx,
  				  __le32 *flags, __le32 *mask)
  {
-	struct ieee80211_sta_ht_cap *sta_ht_inf = &sta-
 >deflink.ht_cap;
+	struct ieee80211_sta_ht_cap *sta_ht_inf;
 

  *mask = STA_FLG_RTS_MIMO_PROT_MSK |
  STA_FLG_MIMO_DIS_MSK |

drivers/net/wireless/intel/iwlwifi/dvm/sta.c +5 -1
@@ -156,7 +156,11 @@ static void iwl_sta_calc_ht_flags(struct
 iwl_priv *priv,
  		STA_FLG_AGG_MPDU_DENSITY_MSK;
  	*flags = 0;
 
-	if (!sta || !sta_ht_inf->ht_supported)
+	if (!sta)
+		return;
+
+	sta_ht_inf = &sta->deflink.ht_cap;
+	if (!sta_ht_inf->ht_supported)
  		return;
 
Quoted 4 lines

  IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n", --
2.40.0

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Sean Fennelly, Jeffrey Schneiderman, Tiffany Doon Silva Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

#135 lore raw

PATCH 1/1 usb: dwc2: gadget: Move a variable assignment behind condition checks in dwc2_hsotg_handle_outdone()

In reply to Markus Elfring

Date: Wed, 19 Apr 2023 20:06:25 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “dwc2_hsotg_handle_outdone”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “req” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 5b7d70c6dbf2db786395cbd21750a1a4ce222f84 ("USB: Gadget driver for Samsung HS/OtG block")
---
 drivers/usb/dwc2/gadget.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/dwc2/gadget.c +3 -1
index 8b15742d9e8a..cab04816dd6c 100644
@@ -2389,7 +2389,7 @@ static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
 	u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
 	struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
 	struct dwc2_hsotg_req *hs_req = hs_ep->req;
-	struct usb_request *req = &hs_req->req;
+	struct usb_request *req;
 	unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
 	int result = 0;
 
@@ -2408,6 +2408,8 @@ static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
 	if (using_desc_dma(hsotg))
 		size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
 
+	req = &hs_req->req;
+
 	if (using_dma(hsotg)) {
 		unsigned int size_done;
 

--
2.40.0

Acked-by on v1 1/1
#136 lore raw

Re: [PATCH] usb: dwc2: gadget: Move a variable assignment behind condition checks in dwc2_hsotg_handle_outdone()

Quoted 6 lines · Markus Elfring

Date: Wed, 19 Apr 2023 20:06:25 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “dwc2_hsotg_handle_outdone”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “req” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 5b7d70c6dbf2db786395cbd21750a1a4ce222f84 ("USB: Gadget driver for Samsung HS/OtG block")
Quoted 29 lines
---
  drivers/usb/dwc2/gadget.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/dwc2/gadget.c +3 -2
index 8b15742d9e8a..cab04816dd6c 100644
@@ -2389,7 +2389,7 @@ static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
  	u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
  	struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
  	struct dwc2_hsotg_req *hs_req = hs_ep->req;
-	struct usb_request *req = &hs_req->req;
+	struct usb_request *req;
  	unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  	int result = 0;
 
@@ -2408,6 +2408,8 @@ static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
  	if (using_desc_dma(hsotg))
  		size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
 
+	req = &hs_req->req;
+
  	if (using_dma(hsotg)) {
  		unsigned int size_done;
 
--

2.40.0

#137 lore raw

PATCH RESEND 1/1 usb: dwc2: gadget: Move a variable assignment behind condition checks in dwc2_hsotg_handle_outdone()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 19 Apr 2023 20:06:25 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “dwc2_hsotg_handle_outdone”.

Thus avoid the risk for undefined behaviour by moving the assignment for the variable “req” behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 5b7d70c6dbf2 ("USB: Gadget driver for Samsung HS/OtG block")
---
 drivers/usb/dwc2/gadget.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/dwc2/gadget.c +3 -1
index 8b15742d9e8a..cab04816dd6c 100644
@@ -2389,7 +2389,7 @@ static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
 	u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
 	struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
 	struct dwc2_hsotg_req *hs_req = hs_ep->req;
-	struct usb_request *req = &hs_req->req;
+	struct usb_request *req;
 	unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
 	int result = 0;
 
@@ -2408,6 +2408,8 @@ static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
 	if (using_desc_dma(hsotg))
 		size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
 
+	req = &hs_req->req;
+
 	if (using_dma(hsotg)) {
 		unsigned int size_done;
 

--
2.40.0

#138 lore raw

PATCH 1/1 ASoC: SOF: Intel: hda-stream: Move three variable assignments behind condition checks in hda_dsp_iccmax_stream_hw_params()

In reply to Markus Elfring

Date: Wed, 19 Apr 2023 20:42:19 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “hda_dsp_iccmax_stream_hw_params”.

Thus avoid the risk for undefined behaviour by moving the assignment for three local variables behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 7d88b9608142f95ccdd3dfb190da4a5faddb1cc7 ("ASoC: SOF: Intel: hdac_ext_stream: consistent prefixes for variables/members")
---
 sound/soc/sof/intel/hda-stream.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
sound/soc/sof/intel/hda-stream.c +8 -3
index 79d818e6a0fa..9c44127014fc 100644
@@ -407,10 +407,10 @@ int hda_dsp_iccmax_stream_hw_params(struct snd_sof_dev *sdev, struct hdac_ext_st
 				    struct snd_dma_buffer *dmab,
 				    struct snd_pcm_hw_params *params)
 {
-	struct hdac_stream *hstream = &hext_stream->hstream;
-	int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
+	struct hdac_stream *hstream;
+	int sd_offset;
 	int ret;
-	u32 mask = 0x1 << hstream->index;
+	u32 mask;
 
 	if (!hext_stream) {
 		dev_err(sdev->dev, "error: no stream available\n");
@@ -422,9 +422,12 @@ int hda_dsp_iccmax_stream_hw_params(struct snd_sof_dev *sdev, struct hdac_ext_st
 		return -ENODEV;
 	}
 
+	hstream = &hext_stream->hstream;
 	if (hstream->posbuf)
 		*hstream->posbuf = 0;
 
+	sd_offset = SOF_STREAM_SD_OFFSET(hstream);
+
 	/* reset BDL address */
 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
 			  sd_offset + SOF_HDA_ADSP_REG_SD_BDLPL,
@@ -459,6 +462,8 @@ int hda_dsp_iccmax_stream_hw_params(struct snd_sof_dev *sdev, struct hdac_ext_st
 				sd_offset + SOF_HDA_ADSP_REG_SD_LVI,
 				0xffff, (hstream->frags - 1));
 
+	mask = 0x1 << hstream->index;
+
 	/* decouple host and link DMA, enable DSP features */
 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
 				mask, mask);

--
2.40.0

#139 lore raw

Re: [PATCH] ASoC: SOF: Intel: hda-stream: Move three variable assignments behind condition checks in hda_dsp_iccmax_stream_hw_params()

Quoted 6 lines · Markus Elfring

Date: Wed, 19 Apr 2023 20:42:19 +0200

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “hda_dsp_iccmax_stream_hw_params”.

Quoted 7 lines

Thus avoid the risk for undefined behaviour by moving the assignment for three local variables behind some condition checks.

This issue was detected by using the Coccinelle software.

Fixes 7d88b9608142f95ccdd3dfb190da4a5faddb1cc7 ("ASoC: SOF: Intel: hdac_ext_stream: consistent prefixes for variables/members")

Yes indeed, for some reason this was fixed in
hda_dsp_stream_hw_params() but not in the
hda_dsp_iccmax_stream_hw_params() variant.

Could we however use the same code as in hda_dsp_stream_hw_params() for consistency?

	if (!hext_stream) {
		dev_err(sdev->dev, "error: no stream available\n");
		return -ENODEV;
	}

	if (!dmab) {
		dev_err(sdev->dev, "error: no dma buffer allocated!\n");
		return -ENODEV;
	}

	hstream = &hext_stream->hstream;
	sd_offset = SOF_STREAM_SD_OFFSET(hstream);
	mask = BIT(hstream->index);

Thanks!

Quoted 47 lines
---
 sound/soc/sof/intel/hda-stream.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
sound/soc/sof/intel/hda-stream.c +8 -4
index 79d818e6a0fa..9c44127014fc 100644
@@ -407,10 +407,10 @@ int hda_dsp_iccmax_stream_hw_params(struct snd_sof_dev *sdev, struct hdac_ext_st
 				    struct snd_dma_buffer *dmab,
 				    struct snd_pcm_hw_params *params)
 {
-	struct hdac_stream *hstream = &hext_stream->hstream;
-	int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
+	struct hdac_stream *hstream;
+	int sd_offset;
 	int ret;
-	u32 mask = 0x1 << hstream->index;
+	u32 mask;
 
 	if (!hext_stream) {
 		dev_err(sdev->dev, "error: no stream available\n");
@@ -422,9 +422,12 @@ int hda_dsp_iccmax_stream_hw_params(struct snd_sof_dev *sdev, struct hdac_ext_st
 		return -ENODEV;
 	}
 
+	hstream = &hext_stream->hstream;
 	if (hstream->posbuf)
 		*hstream->posbuf = 0;
 
+	sd_offset = SOF_STREAM_SD_OFFSET(hstream);
+
 	/* reset BDL address */
 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
 			  sd_offset + SOF_HDA_ADSP_REG_SD_BDLPL,
@@ -459,6 +462,8 @@ int hda_dsp_iccmax_stream_hw_params(struct snd_sof_dev *sdev, struct hdac_ext_st
 				sd_offset + SOF_HDA_ADSP_REG_SD_LVI,
 				0xffff, (hstream->frags - 1));
 
+	mask = 0x1 << hstream->index;
+
 	/* decouple host and link DMA, enable DSP features */
 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
 				mask, mask);
--

2.40.0

#140 lore raw

Re: [PATCH] ASoC: SOF: Intel: hda-stream: Move three variable assignments behind condition checks in hda_dsp_iccmax_stream_hw_params()

Quoted 7 lines
Quoted 7 lines

The address of a data structure member was determined before a corresponding null pointer check in the implementation of the function “hda_dsp_iccmax_stream_hw_params”.

Thus avoid the risk for undefined behaviour by moving the assignment for three local variables behind some condition checks.

Quoted 8 lines
Quoted 4 lines

This issue was detected by using the Coccinelle software.

Fixes 7d88b9608142f95ccdd3dfb190da4a5faddb1cc7 ("ASoC: SOF: Intel: hdac_ext_stream: consistent prefixes for variables/members")

Yes indeed, for some reason this was fixed in
hda_dsp_stream_hw_params() but not in the
hda_dsp_iccmax_stream_hw_params() variant.

Would Peter Ujfalusi like to support similar source code adjustments also according to his commit 09255c7ed8ca1f1ed99357b845d2f63fe2ef3e1e ("ASoC: SOF: Intel: hda-stream: Do not dereference hstream until it is safe") from 2023-04-04?

Quoted 1 line

Could we however use the same code as in hda_dsp_stream_hw_params() for consistency?

…

Quoted 3 lines
	hstream = &hext_stream->hstream;
	sd_offset = SOF_STREAM_SD_OFFSET(hstream);
	mask = BIT(hstream->index);

Can it matter to move such assignment statements a bit closer to subsequent statements?

Regards,
Markus

#141 lore raw

cover staging: rtl8712: Adjustments for process_link_qual()

In reply to Markus Elfring

Date: Thu, 20 Apr 2023 12:05:12 +0200

A few update suggestions were taken into account
from static source code analysis.

Markus Elfring (4):
Delete null pointer checks
Delete two variables
Reduce scope for the variable “sqd”
Simplify the usage of an expression

 drivers/staging/rtl8712/rtl8712_recv.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

--
2.40.0

#142 lore raw

PATCH 1/4 staging: rtl8712: Delete null pointer checks in process_link_qual()

Date: Thu, 20 Apr 2023 09:54:06 +0200

Omit extra null pointer checks for the input parameter validation in the implementation of the function “process_link_qual” because it should be called only with valid pointers.

---
 drivers/staging/rtl8712/rtl8712_recv.c | 2 --
 1 file changed, 2 deletions(-)
drivers/staging/rtl8712/rtl8712_recv.c +0 -2
index 7da014ab0723..f0789b5ef59b 100644
@@ -866,8 +866,6 @@ static void process_link_qual(struct _adapter *padapter,
 	struct rx_pkt_attrib *pattrib;
 	struct smooth_rssi_data *sqd = &padapter->recvpriv.signal_qual_data;
 
-	if (!prframe || !padapter)
-		return;
 	pattrib = &prframe->u.hdr.attrib;
 	if (pattrib->signal_qual != 0) {
 		/*

--
2.40.0

#143 lore raw

PATCH 2/4 staging: rtl8712: Delete two variables in process_link_qual()

In reply to Markus Elfring

Date: Thu, 20 Apr 2023 10:42:40 +0200

* Use two values for computations (in one if branch) without storing them in intermediate variables.

* Remove the local variables “last_evm” and “tmpVal” which became unnecessary with this refactoring.

---
 drivers/staging/rtl8712/rtl8712_recv.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/staging/rtl8712/rtl8712_recv.c +3 -5
index f0789b5ef59b..3a72d0601dc0 100644
@@ -862,7 +862,6 @@ static void query_rx_phy_status(struct _adapter *padapter,
 static void process_link_qual(struct _adapter *padapter,
 			      union recv_frame *prframe)
 {
-	u32	last_evm = 0, tmpVal;
 	struct rx_pkt_attrib *pattrib;
 	struct smooth_rssi_data *sqd = &padapter->recvpriv.signal_qual_data;
 
@@ -873,8 +872,7 @@ static void process_link_qual(struct _adapter *padapter,
 		 */
 		if (sqd->total_num++ >= PHY_LINKQUALITY_SLID_WIN_MAX) {
 			sqd->total_num = PHY_LINKQUALITY_SLID_WIN_MAX;
-			last_evm = sqd->elements[sqd->index];
-			sqd->total_val -= last_evm;
+			sqd->total_val -= sqd->elements[sqd->index];
 		}
 		sqd->total_val += pattrib->signal_qual;
 		sqd->elements[sqd->index++] = pattrib->signal_qual;
@@ -882,8 +880,8 @@ static void process_link_qual(struct _adapter *padapter,
 			sqd->index = 0;
 
 		/* <1> Showed on UI for user, in percentage. */
-		tmpVal = sqd->total_val / sqd->total_num;
-		padapter->recvpriv.signal = (u8)tmpVal;
+		padapter->recvpriv.signal = (u8)(sqd->total_val
+						/ sqd->total_num);
 	}
 }
 

--
2.40.0

#144 lore raw

PATCH 3/4 staging: rtl8712: Reduce scope for the variable “sqd” in process_link_qual()

In reply to Markus Elfring

Date: Thu, 20 Apr 2023 11:00:11 +0200

A local variable was used only within an if branch. Thus move the definition for the variable “sqd” into the corresponding code block.

---
 drivers/staging/rtl8712/rtl8712_recv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/rtl8712/rtl8712_recv.c +2 -1
index 3a72d0601dc0..9135c92906ac 100644
@@ -863,10 +863,11 @@ static void process_link_qual(struct _adapter *padapter,
 			      union recv_frame *prframe)
 {
 	struct rx_pkt_attrib *pattrib;
-	struct smooth_rssi_data *sqd = &padapter->recvpriv.signal_qual_data;
 
 	pattrib = &prframe->u.hdr.attrib;
 	if (pattrib->signal_qual != 0) {
+		struct smooth_rssi_data *sqd = &padapter->recvpriv.signal_qual_data;
+
 		/*
 		 * 1. Record the general EVM to the sliding window.
 		 */

--
2.40.0

#145 lore raw

PATCH 4/4 staging: rtl8712: Simplify the usage of an expression in process_link_qual()

In reply to Markus Elfring

Date: Thu, 20 Apr 2023 11:44:01 +0200

An expression was used at three source code places. Thus use its value by a corresponding variable.

---
 drivers/staging/rtl8712/rtl8712_recv.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/staging/rtl8712/rtl8712_recv.c +6 -5
index 9135c92906ac..3d0c48918510 100644
@@ -862,10 +862,9 @@ static void query_rx_phy_status(struct _adapter *padapter,
 static void process_link_qual(struct _adapter *padapter,
 			      union recv_frame *prframe)
 {
-	struct rx_pkt_attrib *pattrib;
+	u8 signal_qual = prframe->u.hdr.attrib.signal_qual;
 
-	pattrib = &prframe->u.hdr.attrib;
-	if (pattrib->signal_qual != 0) {
+	if (signal_qual) {
 		struct smooth_rssi_data *sqd = &padapter->recvpriv.signal_qual_data;
 
 		/*
@@ -875,8 +874,10 @@ static void process_link_qual(struct _adapter *padapter,
 			sqd->total_num = PHY_LINKQUALITY_SLID_WIN_MAX;
 			sqd->total_val -= sqd->elements[sqd->index];
 		}
-		sqd->total_val += pattrib->signal_qual;
-		sqd->elements[sqd->index++] = pattrib->signal_qual;
+
+		sqd->total_val += signal_qual;
+		sqd->elements[sqd->index++] = signal_qual;
+
 		if (sqd->index >= PHY_LINKQUALITY_SLID_WIN_MAX)
 			sqd->index = 0;
 

--
2.40.0

Tested-by on the v1 series
#146 lore raw

Re: [PATCH 0/4] staging: rtl8712: Adjustments for process_link_qual()

In reply to Markus Elfring
Quoted 11 lines · Markus Elfring

Date: Thu, 20 Apr 2023 12:05:12 +0200

A few update suggestions were taken into account
from static source code analysis.

Markus Elfring (4):
Delete null pointer checks
Delete two variables
Reduce scope for the variable “sqd”
Simplify the usage of an expression

Quoted 7 lines
  drivers/staging/rtl8712/rtl8712_recv.c | 24 +++++++++++-------------
  1 file changed, 11 insertions(+), 13 deletions(-)

--
2.40.0

Hi, you get the following checkpatch warning. Is this wanted?

WARNING: From:/Signed-off-by: email address mismatch: 'From: Markus Elfring <[email protected]>' != 'Signed-off-by: Markus Elfring <[email protected]>'

#147 lore raw

cover staging: rtl8723bs: Adjustments for rtw_set_802_11_bssid_list_scan()

In reply to Markus Elfring

Date: Thu, 20 Apr 2023 16:16:32 +0200

A few update suggestions were taken into account
from static source code analysis.

Markus Elfring (4):
Delete a null pointer check
Return directly after a failed initialisation check Delete an unnecessary variable initialisation Move a variable assignment behind a condition check

 drivers/staging/rtl8723bs/core/rtw_ioctl_set.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)

--
2.40.0

#148 lore raw

PATCH 1/4 staging: rtl8723bs: Delete a null pointer check in rtw_set_802_11_bssid_list_scan()

Date: Thu, 20 Apr 2023 15:30:23 +0200

Omit an extra null pointer check for the input parameter “padapter” in the implementation of the function “rtw_set_802_11_bssid_list_scan” because it should be called only with a valid pointer.

---
 drivers/staging/rtl8723bs/core/rtw_ioctl_set.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/staging/rtl8723bs/core/rtw_ioctl_set.c +0 -4
index 3b44f0dd5b0a..7c902f50d38b 100644
@@ -371,10 +371,6 @@ u8 rtw_set_802_11_bssid_list_scan(struct adapter *padapter, struct ndis_802_11_s
 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 	u8 res = true;
 
-	if (!padapter) {
-		res = false;
-		goto exit;
-	}
 	if (padapter->hw_init_completed == false) {
 		res = false;
 		goto exit;

--
2.40.0

#149 lore raw

PATCH 2/4 staging: rtl8723bs: Return directly after a failed initialisation check in rtw_set_802_11_bssid_list_scan()

In reply to Markus Elfring

Date: Thu, 20 Apr 2023 15:46:37 +0200

1. Return directly if the hardware initialisation was not completed so far.

2. Delete the label “exit” which became unnecessary with this refactoring.

---
 drivers/staging/rtl8723bs/core/rtw_ioctl_set.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/staging/rtl8723bs/core/rtw_ioctl_set.c +2 -6
index 7c902f50d38b..0c2df854afe7 100644
@@ -371,10 +371,8 @@ u8 rtw_set_802_11_bssid_list_scan(struct adapter *padapter, struct ndis_802_11_s
 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 	u8 res = true;
 
-	if (padapter->hw_init_completed == false) {
-		res = false;
-		goto exit;
-	}
+	if (!padapter->hw_init_completed)
+		return false;
 
 	if ((check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING) == true) ||
 		(pmlmepriv->LinkDetectInfo.bBusyTraffic == true)) {
@@ -391,8 +389,6 @@ u8 rtw_set_802_11_bssid_list_scan(struct adapter *padapter, struct ndis_802_11_s
 
 		spin_unlock_bh(&pmlmepriv->lock);
 	}
-exit:
-
 	return res;
 }
 

--
2.40.0

#150 lore raw

PATCH 3/4 staging: rtl8723bs: Delete an unnecessary variable initialisation in rtw_set_802_11_bssid_list_scan()

In reply to Markus Elfring

Date: Thu, 20 Apr 2023 15:55:48 +0200

The variable “res” will eventually be set to an appropriate value a bit later.
Thus omit the explicit initialisation at the beginning.

---
 drivers/staging/rtl8723bs/core/rtw_ioctl_set.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/rtl8723bs/core/rtw_ioctl_set.c +1 -1
index 0c2df854afe7..0716b704106d 100644
@@ -369,7 +369,7 @@ u8 rtw_set_802_11_disassociate(struct adapter *padapter)
 u8 rtw_set_802_11_bssid_list_scan(struct adapter *padapter, struct ndis_802_11_ssid *pssid, int ssid_max_num)
 {
 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
-	u8 res = true;
+	u8 res;
 
 	if (!padapter->hw_init_completed)
 		return false;

--
2.40.0

#151 lore raw

PATCH 4/4 staging: rtl8723bs: Move a variable assignment behind a condition check in rtw_set_802_11_bssid_list_scan()

In reply to Markus Elfring

Date: Thu, 20 Apr 2023 16:06:08 +0200

Convert the explicit initialisation of the local variable “pmlmepriv” at the beginning to an assignment statement directly before the source code place where this pointer is used.

---
 drivers/staging/rtl8723bs/core/rtw_ioctl_set.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/rtl8723bs/core/rtw_ioctl_set.c +2 -1
index 0716b704106d..fc6875b4697e 100644
@@ -368,12 +368,13 @@ u8 rtw_set_802_11_disassociate(struct adapter *padapter)
 
 u8 rtw_set_802_11_bssid_list_scan(struct adapter *padapter, struct ndis_802_11_ssid *pssid, int ssid_max_num)
 {
-	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
+	struct mlme_priv *pmlmepriv;
 	u8 res;
 
 	if (!padapter->hw_init_completed)
 		return false;
 
+	pmlmepriv = &padapter->mlmepriv;
 	if ((check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING) == true) ||
 		(pmlmepriv->LinkDetectInfo.bBusyTraffic == true)) {
 		/*  Scan or linking is in progress, do nothing. */

--
2.40.0