Merge lp:~gordallott/unity/fix-937421 into lp:unity

Proposed by Gord Allott
Status: Merged
Approved by: Tim Penhey
Approved revision: no longer in the source branch.
Merged at revision: 2050
Proposed branch: lp:~gordallott/unity/fix-937421
Merge into: lp:unity
Diff against target: 114 lines (+51/-38)
1 file modified
plugins/unityshell/src/HudIconTextureSource.cpp (+51/-38)
To merge this branch: bzr merge lp:~gordallott/unity/fix-937421
Reviewer Review Type Date Requested Status
Tim Penhey (community) Approve
Review via email: mp+94970@code.launchpad.net

Commit message

Check pixbuf before dereferencing.

Description of the change

safety wrapper around the gdkpixbuf sent to the object

no test as I can't reproduce the issue, without the pixbuf that was causing the issue, we can't write a succesful test

UNBLOCK

To post a comment you must log in.
Revision history for this message
Tim Penhey (thumper) wrote :

Is there any way we can fake a bad pixbuf for this function?

If not, how about extract that content into a stand alone function, and test that in a unit test.

If the function content is the same as the LaunchadIcon.cpp, they should both use the same function.

review: Needs Information
Revision history for this message
Gord Allott (gordallott) wrote :

Without knowing precicely what the bad pixbuf is, a test is meaningless, it may or may not be testing what you thought it was and it certainly does not verrify that the bug itself is fixed

What we need is an application guarunteed to cause the bug, so we can look at the real reason as to why a bad pixbuf is generated in the first place and potentially build ap tests using that application.

I really can't think of a way of testing this change that doesn't basically just boil down to writing EXPECT_EQ(GDK_IS_PIXBUF(NULL), FALSE);

Revision history for this message
Tim Penhey (thumper) wrote :

OK. Fair enough. I can't think of a reasonable test for this either. We do say that sometimes we need to use our discretion, and to be reasonable. I think this is one of those cases.

I approve this landing without an explicit test.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugins/unityshell/src/HudIconTextureSource.cpp'
2--- plugins/unityshell/src/HudIconTextureSource.cpp 2012-02-02 18:01:23 +0000
3+++ plugins/unityshell/src/HudIconTextureSource.cpp 2012-02-28 14:18:23 +0000
4@@ -26,6 +26,10 @@
5 #include <Nux/Nux.h>
6 #include <NuxCore/Logger.h>
7
8+namespace
9+{
10+ nux::logging::Logger logger("unity.hud.HudIconTextureSource");
11+}
12
13 namespace unity
14 {
15@@ -44,46 +48,54 @@
16
17 void HudIconTextureSource::ColorForIcon(GdkPixbuf* pixbuf)
18 {
19- unsigned int width = gdk_pixbuf_get_width(pixbuf);
20- unsigned int height = gdk_pixbuf_get_height(pixbuf);
21- unsigned int row_bytes = gdk_pixbuf_get_rowstride(pixbuf);
22-
23- long int rtotal = 0, gtotal = 0, btotal = 0;
24- float total = 0.0f;
25-
26- guchar* img = gdk_pixbuf_get_pixels(pixbuf);
27-
28- for (unsigned int i = 0; i < width; i++)
29+ if (GDK_IS_PIXBUF(pixbuf))
30 {
31- for (unsigned int j = 0; j < height; j++)
32+ unsigned int width = gdk_pixbuf_get_width(pixbuf);
33+ unsigned int height = gdk_pixbuf_get_height(pixbuf);
34+ unsigned int row_bytes = gdk_pixbuf_get_rowstride(pixbuf);
35+
36+ long int rtotal = 0, gtotal = 0, btotal = 0;
37+ float total = 0.0f;
38+
39+ guchar* img = gdk_pixbuf_get_pixels(pixbuf);
40+
41+ for (unsigned int i = 0; i < width; i++)
42 {
43- guchar* pixels = img + (j * row_bytes + i * 4);
44- guchar r = *(pixels + 0);
45- guchar g = *(pixels + 1);
46- guchar b = *(pixels + 2);
47- guchar a = *(pixels + 3);
48-
49- float saturation = (MAX(r, MAX(g, b)) - MIN(r, MIN(g, b))) / 255.0f;
50- float relevance = .1 + .9 * (a / 255.0f) * saturation;
51-
52- rtotal += (guchar)(r * relevance);
53- gtotal += (guchar)(g * relevance);
54- btotal += (guchar)(b * relevance);
55-
56- total += relevance * 255;
57+ for (unsigned int j = 0; j < height; j++)
58+ {
59+ guchar* pixels = img + (j * row_bytes + i * 4);
60+ guchar r = *(pixels + 0);
61+ guchar g = *(pixels + 1);
62+ guchar b = *(pixels + 2);
63+ guchar a = *(pixels + 3);
64+
65+ float saturation = (MAX(r, MAX(g, b)) - MIN(r, MIN(g, b))) / 255.0f;
66+ float relevance = .1 + .9 * (a / 255.0f) * saturation;
67+
68+ rtotal += (guchar)(r * relevance);
69+ gtotal += (guchar)(g * relevance);
70+ btotal += (guchar)(b * relevance);
71+
72+ total += relevance * 255;
73+ }
74 }
75- }
76-
77- nux::color::RedGreenBlue rgb(rtotal / total,
78- gtotal / total,
79- btotal / total);
80- nux::color::HueSaturationValue hsv(rgb);
81-
82- if (hsv.saturation > 0.15f)
83- hsv.saturation = 0.65f;
84-
85- hsv.value = 0.90f;
86- bg_color = nux::Color(nux::color::RedGreenBlue(hsv));
87+
88+ nux::color::RedGreenBlue rgb(rtotal / total,
89+ gtotal / total,
90+ btotal / total);
91+ nux::color::HueSaturationValue hsv(rgb);
92+
93+ if (hsv.saturation > 0.15f)
94+ hsv.saturation = 0.65f;
95+
96+ hsv.value = 0.90f;
97+ bg_color = nux::Color(nux::color::RedGreenBlue(hsv));
98+ }
99+ else
100+ {
101+ LOG_ERROR(logger) << "Pixbuf (" << pixbuf << ") passed is non valid";
102+ bg_color = nux::Color(255,255,255,255);
103+ }
104 }
105
106 nux::Color HudIconTextureSource::BackgroundColor()
107@@ -107,4 +119,5 @@
108 }
109
110 }
111-}
112\ No newline at end of file
113+}
114+