Merge lp:~vitty/armagetronad/trunk-armagetronad-verify_color_code into lp:~armagetronad-dev/armagetronad/trunk-armagetronad-work

Proposed by Vitty
Status: Merged
Approved by: Manuel Moos
Approved revision: 1019
Merged at revision: 1019
Proposed branch: lp:~vitty/armagetronad/trunk-armagetronad-verify_color_code
Merge into: lp:~armagetronad-dev/armagetronad/trunk-armagetronad-work
Diff against target: 148 lines (+72/-25)
3 files modified
src/render/rFont.cpp (+13/-11)
src/tools/tColor.cpp (+56/-14)
src/tools/tColor.h (+3/-0)
To merge this branch: bzr merge lp:~vitty/armagetronad/trunk-armagetronad-verify_color_code
Reviewer Review Type Date Requested Status
Manuel Moos Approve
Review via email: mp+67480@code.launchpad.net

Description of the change

Verify the color code.
 - This prevents a segfault when a color code can be too
   short and it tries to use a UTF character to complete it.

To post a comment you must log in.
Revision history for this message
Manuel Moos (z-man) wrote :

Good work.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/render/rFont.cpp'
2--- src/render/rFont.cpp 2009-01-31 20:20:16 +0000
3+++ src/render/rFont.cpp 2011-07-11 02:50:59 +0000
4@@ -617,21 +617,23 @@
5 //}
6
7 // detect presence of color code
8- if (*c=='0' && my_strnlen(c, 8)>=8 && c[1]=='x' && colorMode != COLOR_IGNORE )
9+
10+ FTGL_CHAR const resett[] = {
11+ static_cast<FTGL_CHAR>('0'),
12+ static_cast<FTGL_CHAR>('x'),
13+ static_cast<FTGL_CHAR>('R'),
14+ static_cast<FTGL_CHAR>('E'),
15+ static_cast<FTGL_CHAR>('S'),
16+ static_cast<FTGL_CHAR>('E'),
17+ static_cast<FTGL_CHAR>('T'),
18+ static_cast<FTGL_CHAR>('T'),
19+ 0};
20+
21+ if (*c=='0' && my_strnlen(c, 8)>=8 && c[1]=='x' && colorMode != COLOR_IGNORE && (tColor::VerifyColorCode(c) || 0 == my_strncmp(c,resett,8)))
22 {
23 tColor color;
24 bool use = false;
25
26- FTGL_CHAR const resett[] = {
27- static_cast<FTGL_CHAR>('0'),
28- static_cast<FTGL_CHAR>('x'),
29- static_cast<FTGL_CHAR>('R'),
30- static_cast<FTGL_CHAR>('E'),
31- static_cast<FTGL_CHAR>('S'),
32- static_cast<FTGL_CHAR>('E'),
33- static_cast<FTGL_CHAR>('T'),
34- static_cast<FTGL_CHAR>('T'),
35- 0};
36 if ( 0 == my_strncmp(c,resett,8) )
37 {
38 // color reset to default requested
39
40=== modified file 'src/tools/tColor.cpp'
41--- src/tools/tColor.cpp 2009-02-02 16:15:01 +0000
42+++ src/tools/tColor.cpp 2011-07-11 02:50:59 +0000
43@@ -147,14 +147,11 @@
44
45 void tColor::FillFrom( const char * c )
46 {
47- // check whether the passed string is too short
48- for( int i = 0; i < 8; ++i )
49+ // Verify it's a valid color code
50+ if( !VerifyColorCode( c ) )
51 {
52- if( !c[i] )
53- {
54- r_ = g_ = b_ = 0;
55- return;
56- }
57+ r_ = g_ = b_ = 0;
58+ return;
59 }
60
61 r_ = CTR( hex_to_int( c[2] ) *16 + hex_to_int( c[3] ) );
62@@ -174,14 +171,11 @@
63
64 void tColor::FillFrom( const wchar_t * c )
65 {
66- // check whether the passed string is too short
67- for( int i = 0; i < 8; ++i )
68+ // Verify it's a valid color code
69+ if( !VerifyColorCode( c ) )
70 {
71- if( !c[i] )
72- {
73- r_ = g_ = b_ = 0;
74- return;
75- }
76+ r_ = g_ = b_ = 0;
77+ return;
78 }
79
80 r_ = CTR( hex_to_int( c[2] ) *16 + hex_to_int( c[3] ) );
81@@ -225,6 +219,54 @@
82
83 // *******************************************************************************************
84 // *
85+// * VerifyColorCode
86+// *
87+// *******************************************************************************************
88+//!
89+//! @param c Color code string to read the color from
90+//!
91+// *******************************************************************************************
92+
93+bool tColor::VerifyColorCode( const char * c )
94+{
95+ for( int i = 2; i < 8; ++i )
96+ {
97+ if ( !c[i] ||
98+ (!(c[i] >= '0' && c[i] <= '9') &&
99+ !(c[i] >= 'A' && c[i] <= 'F') &&
100+ !(c[i] >= 'a' && c[i] <= 'f')
101+ )
102+ ) return false;
103+ }
104+ return true;
105+}
106+
107+// *******************************************************************************************
108+// *
109+// * VerifyColorCode
110+// *
111+// *******************************************************************************************
112+//!
113+//! @param c Color code string to read the color from
114+//!
115+// *******************************************************************************************
116+
117+bool tColor::VerifyColorCode( const wchar_t * c )
118+{
119+ for( int i = 2; i < 8; ++i )
120+ {
121+ if ( !c[i] ||
122+ (!(c[i] >= '0' && c[i] <= '9') &&
123+ !(c[i] >= 'A' && c[i] <= 'F') &&
124+ !(c[i] >= 'a' && c[i] <= 'f')
125+ )
126+ ) return false;
127+ }
128+ return true;
129+}
130+
131+// *******************************************************************************************
132+// *
133 // * tColor
134 // *
135 // *******************************************************************************************
136
137=== modified file 'src/tools/tColor.h'
138--- src/tools/tColor.h 2010-08-06 07:05:46 +0000
139+++ src/tools/tColor.h 2011-07-11 02:50:59 +0000
140@@ -50,6 +50,9 @@
141 void WriteSync( Tools::Color & target ) const; //!< writes sync data
142 void ReadSync( Tools::Color const & source ); //!< reads sync data
143
144+ static bool VerifyColorCode( const char * c ); // Verifys it's a valid color code
145+ bool VerifyColorCode( const wchar_t * c); // Verifys it's a valid color code
146+
147 // the colors are public because they are independent of each other
148 REAL r_, g_, b_, a_; //!< Color values
149

Subscribers

People subscribed via source and target branches

to status/vote changes: