Skip to contentSkip to conversation

drm/sun4i: backend: Fix error pointers in sun4i_backend_atomic_check

PATCH In review Chenyuan Yang 3 participants 4 messages Mar 14, 2025 – Aug 7, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/sun4i: backend: Fix error pointers in sun4i_backend_atomic_check

The function sun4i_backend_atomic_check was dereferencing pointers returned by drm_atomic_get_plane_state without checking for errors. This could lead to undefined behavior if the function returns an error pointer.

This commit adds checks using IS_ERR to ensure that plane_state is valid before dereferencing them.

Similar to commit da29abe71e16
("drm/amd/display: Fix error pointers in amdgpu_dm_crtc_mem_type_changed").

Fixes 96180dde23b7 ("drm/sun4i: backend: Add a custom atomic_check for the frontend")
---
 drivers/gpu/drm/sun4i/sun4i_backend.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/sun4i/sun4i_backend.c +4 -0
index 2dded3b828df..a8e0e2123764 100644
@@ -490,6 +490,10 @@ static int sun4i_backend_atomic_check(struct sunxi_engine *engine,
 	drm_for_each_plane_mask(plane, drm, crtc_state->plane_mask) {
 		struct drm_plane_state *plane_state =
 			drm_atomic_get_plane_state(state, plane);
+
+		if (IS_ERR(plane_state))
+			return PTR_ERR(plane_state);
+
 		struct sun4i_layer_state *layer_state =
 			state_to_sun4i_layer_state(plane_state);
 		struct drm_framebuffer *fb = plane_state->fb;
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Hi,

Quoted 21 lines · Chenyuan Yang

The function sun4i_backend_atomic_check was dereferencing pointers returned by drm_atomic_get_plane_state without checking for errors. This could lead to undefined behavior if the function returns an error pointer.

This commit adds checks using IS_ERR to ensure that plane_state is valid before dereferencing them.

Similar to commit da29abe71e16
("drm/amd/display: Fix error pointers in amdgpu_dm_crtc_mem_type_changed").

Fixes 96180dde23b7 ("drm/sun4i: backend: Add a custom atomic_check for the frontend")
---
 drivers/gpu/drm/sun4i/sun4i_backend.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/sun4i/sun4i_backend.c +0 -0
index 2dded3b828df..a8e0e2123764 100644

drivers/gpu/drm/sun4i/sun4i_backend.c static int sun4i_backend_atomic_check(struct sunxi_engine *engine, · line 496

Quoted 7 lines
drivers/gpu/drm/sun4i/sun4i_backend.c +4 -0
@@ -490,6 +490,10 @@ static int sun4i_backend_atomic_check(struct sunxi_engine *engine,
 	drm_for_each_plane_mask(plane, drm, crtc_state->plane_mask) {
 		struct drm_plane_state *plane_state =
 			drm_atomic_get_plane_state(state, plane);
+
+		if (IS_ERR(plane_state))
+			return PTR_ERR(plane_state);
+

This introduces a build warning. You shouldn't mix declarations and code.

Maxime

#2 lore raw

HI Maxime.

Thanks so much for pointing that out!

How about such a patch?

(quoted diff) +0 -0

b/drivers/gpu/drm/sun4i/sun4i_backend.c
index 2dded3b828df..5ad0e90d3e6b 100644

drivers/gpu/drm/sun4i/sun4i_backend.c +0 -0
@@ -490,9 +490,14 @@ static int sun4i_backend_atomic_check(struct

sunxi_engine *engine,
drm_for_each_plane_mask(plane, drm, crtc_state->plane_mask) { struct drm_plane_state *plane_state = drm_atomic_get_plane_state(state, plane);
- struct sun4i_layer_state *layer_state =
- state_to_sun4i_layer_state(plane_state);
- struct drm_framebuffer *fb = plane_state->fb;
+ struct sun4i_layer_state *layer_state = NULL;
+ struct drm_framebuffer *fb = NULL;
+
+ if (IS_ERR(plane_state))
+ return PTR_ERR(plane_state);
+
+ layer_state = state_to_sun4i_layer_state(plane_state);
+ fb = plane_state->fb;

if (!sun4i_backend_plane_is_supported(plane_state,

        &layer_state->uses_frontend))
Signature · 1 line
-Chenyuan
Quoted 36 lines · Maxime Ripard

Hi,

Quoted 21 lines · Chenyuan Yang

The function sun4i_backend_atomic_check was dereferencing pointers returned by drm_atomic_get_plane_state without checking for errors. This could lead to undefined behavior if the function returns an error pointer.

This commit adds checks using IS_ERR to ensure that plane_state is valid before dereferencing them.

Similar to commit da29abe71e16
("drm/amd/display: Fix error pointers in amdgpu_dm_crtc_mem_type_changed").

Fixes 96180dde23b7 ("drm/sun4i: backend: Add a custom atomic_check for the frontend")
---
 drivers/gpu/drm/sun4i/sun4i_backend.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/sun4i/sun4i_backend.c +0 -0
index 2dded3b828df..a8e0e2123764 100644

drivers/gpu/drm/sun4i/sun4i_backend.c static int sun4i_backend_atomic_check(struct sunxi_engine *engine, · line 496

Quoted 7 lines
drivers/gpu/drm/sun4i/sun4i_backend.c +4 -0
@@ -490,6 +490,10 @@ static int sun4i_backend_atomic_check(struct sunxi_engine *engine,
      drm_for_each_plane_mask(plane, drm, crtc_state->plane_mask) {
              struct drm_plane_state *plane_state =
                      drm_atomic_get_plane_state(state, plane);
+
+             if (IS_ERR(plane_state))
+                     return PTR_ERR(plane_state);
+

This introduces a build warning. You shouldn't mix declarations and code.

Maxime

#3 lore raw
Quoted 6 lines · Chenyuan Yang

HI Maxime.

Thanks so much for pointing that out!

How about such a patch?

This patch is corrupted so we can't review it.

Quoted 6 lines
drivers/gpu/drm/sun4i/sun4i_backend.c +0 -0
index 2dded3b828df..5ad0e90d3e6b 100644

drivers/gpu/drm/sun4i/sun4i_backend.c static int sun4i_backend_atomic_check(struct · line 495

Quoted 9 lines
drivers/gpu/drm/sun4i/sun4i_backend.c +2 -3
@@ -490,9 +490,14 @@ static int sun4i_backend_atomic_check(struct
 sunxi_engine *engine,
  drm_for_each_plane_mask(plane, drm, crtc_state->plane_mask) {
  struct drm_plane_state *plane_state =
  drm_atomic_get_plane_state(state, plane);
- struct sun4i_layer_state *layer_state =
- state_to_sun4i_layer_state(plane_state);
- struct drm_framebuffer *fb = plane_state->fb;
+ struct sun4i_layer_state *layer_state = NULL;
+ struct drm_framebuffer *fb = NULL;

No need to initialize things like this. NULL isn't a valid pointer. This just disables static checker tools from finding uninitialized variable warnings so all the expense of writing the checker tools is now wasted.

regards,
dan carpenter

Quoted 50 lines
drivers/gpu/drm/sun4i/sun4i_backend.c +6 -0
+
+ if (IS_ERR(plane_state))
+ return PTR_ERR(plane_state);
+
+ layer_state = state_to_sun4i_layer_state(plane_state);
+ fb = plane_state->fb;
 
  if (!sun4i_backend_plane_is_supported(plane_state,
        &layer_state->uses_frontend))
Signature · 1 line
-Chenyuan
Quoted 36 lines · Maxime Ripard

Hi,

Quoted 21 lines · Chenyuan Yang

The function sun4i_backend_atomic_check was dereferencing pointers returned by drm_atomic_get_plane_state without checking for errors. This could lead to undefined behavior if the function returns an error pointer.

This commit adds checks using IS_ERR to ensure that plane_state is valid before dereferencing them.

Similar to commit da29abe71e16
("drm/amd/display: Fix error pointers in amdgpu_dm_crtc_mem_type_changed").

Fixes 96180dde23b7 ("drm/sun4i: backend: Add a custom atomic_check for the frontend")
---
 drivers/gpu/drm/sun4i/sun4i_backend.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/sun4i/sun4i_backend.c +0 -0
index 2dded3b828df..a8e0e2123764 100644

drivers/gpu/drm/sun4i/sun4i_backend.c static int sun4i_backend_atomic_check(struct sunxi_engine *engine, · line 496

Quoted 7 lines
drivers/gpu/drm/sun4i/sun4i_backend.c +4 -0
@@ -490,6 +490,10 @@ static int sun4i_backend_atomic_check(struct sunxi_engine *engine,
      drm_for_each_plane_mask(plane, drm, crtc_state->plane_mask) {
              struct drm_plane_state *plane_state =
                      drm_atomic_get_plane_state(state, plane);
+
+             if (IS_ERR(plane_state))
+                     return PTR_ERR(plane_state);
+

This introduces a build warning. You shouldn't mix declarations and code.

Maxime