Skip to contentSkip to conversation

drm: Reduce scope of 'state' variable

PATCH Superseded Dawid Kurek 2 participants 5 messages Jun 15, 2017

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm: Reduce scope of 'state' variable

Smaller scope reduces visibility of variable and makes usage of uninitialized variable less possible.

---
 drivers/gpu/drm/drm_atomic.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_atomic.c +3 -2
index f32506a..ea5a9a7 100644
@@ -108,10 +108,11 @@ struct drm_atomic_state *
 drm_atomic_state_alloc(struct drm_device *dev)
 {
 	struct drm_mode_config *config = &dev->mode_config;
-	struct drm_atomic_state *state;
 
 	if (!config->funcs->atomic_state_alloc) {
-		state = kzalloc(sizeof(*state), GFP_KERNEL);
+		struct drm_atomic_state *state
+			= kzalloc(sizeof(*state), GFP_KERNEL);
+
 		if (!state)
 			return NULL;
 		if (drm_atomic_state_init(dev, state) < 0) {
Signature · 11 lines
2.10.0


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw
Quoted 11 lines · Dawid Kurek

Smaller scope reduces visibility of variable and makes usage of uninitialized variable less possible.

---
 drivers/gpu/drm/drm_atomic.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_atomic.c +0 -0
index f32506a..ea5a9a7 100644

drivers/gpu/drm/drm_atomic.c struct drm_atomic_state * · line 114

Quoted 9 lines
drivers/gpu/drm/drm_atomic.c +2 -2
@@ -108,10 +108,11 @@ struct drm_atomic_state *
 drm_atomic_state_alloc(struct drm_device *dev)
 {
 	struct drm_mode_config *config = &dev->mode_config;
-	struct drm_atomic_state *state;
 
 	if (!config->funcs->atomic_state_alloc) {
-		state = kzalloc(sizeof(*state), GFP_KERNEL);
+		struct drm_atomic_state *state
+			= kzalloc(sizeof(*state), GFP_KERNEL);

Separate declaration and initialization would lead to a cleaner patch and result.

BR,
Jani.

Quoted 4 lines
drivers/gpu/drm/drm_atomic.c +1 -0
+
 		if (!state)
 			return NULL;
 		if (drm_atomic_state_init(dev, state) < 0) {
Signature · 5 lines
Jani Nikula, Intel Open Source Technology Center
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw
Quoted 17 lines · Jani Nikula
Quoted 16 lines · Dawid Kurek

Smaller scope reduces visibility of variable and makes usage of uninitialized variable less possible.

---
 drivers/gpu/drm/drm_atomic.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_atomic.c +0 -1
index f32506a..ea5a9a7 100644
@@ -108,10 +108,11 @@ struct drm_atomic_state *
 drm_atomic_state_alloc(struct drm_device *dev)
 {
 	struct drm_mode_config *config = &dev->mode_config;
-	struct drm_atomic_state *state;
 
Quoted 7 lines

drivers/gpu/drm/drm_atomic.c struct drm_atomic_state * · line 114

Quoted 4 lines
drivers/gpu/drm/drm_atomic.c +2 -1
 	if (!config->funcs->atomic_state_alloc) {
-		state = kzalloc(sizeof(*state), GFP_KERNEL);
+		struct drm_atomic_state *state
+			= kzalloc(sizeof(*state), GFP_KERNEL);

Separate declaration and initialization would lead to a cleaner patch and result.

I saw combining declaration and initialization is quite common, i.e. in drm_atomic file. Personally, I also prefer those in one statement. But yes, it looks cleaner here, in two lines.

v2 sent :)

Thanks,
Dawid

Quoted 11 lines

BR,
Jani.

Quoted 4 lines
drivers/gpu/drm/drm_atomic.c +1 -0
+
 		if (!state)
 			return NULL;
 		if (drm_atomic_state_init(dev, state) < 0) {
Signature · 1 line
Jani Nikula, Intel Open Source Technology Center

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw
Quoted 7 lines · Dawid Kurek
Quoted 2 lines · Jani Nikula

Separate declaration and initialization would lead to a cleaner patch and result.

I saw combining declaration and initialization is quite common, i.e. in drm_atomic file. Personally, I also prefer those in one statement. But yes, it looks cleaner here, in two lines.

I'd say the rule of thumb is that combined declaration and initialization is fine if the initialization is trivial, in particular can never fail. If you need to check the return value, like in this case, I'd prefer separate initialization.

BR,
Jani.

Signature · 5 lines
Jani Nikula, Intel Open Source Technology Center
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#4 lore raw
Quoted 5 lines · Jani Nikula
Quoted 4 lines · Dawid Kurek
Quoted 2 lines · Jani Nikula

Separate declaration and initialization would lead to a cleaner patch and result.

Quoted 9 lines
Quoted 3 lines

I saw combining declaration and initialization is quite common, i.e. in drm_atomic file. Personally, I also prefer those in one statement. But yes, it looks cleaner here, in two lines.

I'd say the rule of thumb is that combined declaration and initialization is fine if the initialization is trivial, in particular can never fail. If you need to check the return value, like in this case, I'd prefer separate initialization.

Yeah, makes sense. If you need to check then it exceeds simple initialization, and then it is not declare&initialize. Yes, now I see it.

Thanks a lot,
Dawid

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel