Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: src/utils/SkRTConf.cpp

Issue 23174002: Fix crash when querying a runtime config that is defined in environment (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/utils/SkRTConf.h ('k') | tests/UnitTestTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkRTConf.h" 8 #include "SkRTConf.h"
9 #include "SkOSFile.h" 9 #include "SkOSFile.h"
10 10
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 static inline void str_replace(char *s, char search, char replace) { 207 static inline void str_replace(char *s, char search, char replace) {
208 for (char *ptr = s ; *ptr ; ptr++) { 208 for (char *ptr = s ; *ptr ; ptr++) {
209 if (*ptr == search) { 209 if (*ptr == search) {
210 *ptr = replace; 210 *ptr = replace;
211 } 211 }
212 } 212 }
213 } 213 }
214 214
215 template<typename T> bool SkRTConfRegistry::parse(const char *name, T* value) { 215 template<typename T> bool SkRTConfRegistry::parse(const char *name, T* value) {
216 SkString *str = NULL; 216 SkString *str = NULL;
217 SkString tmp;
217 218
218 for (int i = fConfigFileKeys.count() - 1 ; i >= 0; i--) { 219 for (int i = fConfigFileKeys.count() - 1 ; i >= 0; i--) {
219 if (fConfigFileKeys[i]->equals(name)) { 220 if (fConfigFileKeys[i]->equals(name)) {
220 str = fConfigFileValues[i]; 221 str = fConfigFileValues[i];
221 break; 222 break;
222 } 223 }
223 } 224 }
224 225
225 SkString environment_variable("skia."); 226 SkString environment_variable("skia.");
226 environment_variable.append(name); 227 environment_variable.append(name);
227 228
228 const char *environment_value = getenv(environment_variable.c_str()); 229 const char *environment_value = getenv(environment_variable.c_str());
229 if (environment_value) { 230 if (environment_value) {
231 if (NULL == str) {
232 str = &tmp;
233 }
230 str->set(environment_value); 234 str->set(environment_value);
231 } else { 235 } else {
232 // apparently my shell doesn't let me have environment variables that 236 // apparently my shell doesn't let me have environment variables that
233 // have periods in them, so also let the user substitute underscores. 237 // have periods in them, so also let the user substitute underscores.
234 SkString underscore_environment_variable("skia_"); 238 SkString underscore_environment_variable("skia_");
235 char *underscore_name = SkStrDup(name); 239 char *underscore_name = SkStrDup(name);
236 str_replace(underscore_name,'.','_'); 240 str_replace(underscore_name,'.','_');
237 underscore_environment_variable.append(underscore_name); 241 underscore_environment_variable.append(underscore_name);
238 sk_free(underscore_name); 242 sk_free(underscore_name);
239 environment_value = getenv(underscore_environment_variable.c_str()); 243 environment_value = getenv(underscore_environment_variable.c_str());
240 if (environment_value) { 244 if (environment_value) {
245 if (NULL == str) {
246 str = &tmp;
247 }
241 str->set(environment_value); 248 str->set(environment_value);
242 } 249 }
243 } 250 }
244 251
245 if (!str) { 252 if (!str) {
246 return false; 253 return false;
247 } 254 }
248 255
249 bool success; 256 bool success;
250 T new_value = doParse<T>(str->c_str(),&success); 257 T new_value = doParse<T>(str->c_str(),&success);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 template void SkRTConfRegistry::set(const char *name, int value); 294 template void SkRTConfRegistry::set(const char *name, int value);
288 template void SkRTConfRegistry::set(const char *name, unsigned int value); 295 template void SkRTConfRegistry::set(const char *name, unsigned int value);
289 template void SkRTConfRegistry::set(const char *name, float value); 296 template void SkRTConfRegistry::set(const char *name, float value);
290 template void SkRTConfRegistry::set(const char *name, double value); 297 template void SkRTConfRegistry::set(const char *name, double value);
291 template void SkRTConfRegistry::set(const char *name, char * value); 298 template void SkRTConfRegistry::set(const char *name, char * value);
292 299
293 SkRTConfRegistry &skRTConfRegistry() { 300 SkRTConfRegistry &skRTConfRegistry() {
294 static SkRTConfRegistry r; 301 static SkRTConfRegistry r;
295 return r; 302 return r;
296 } 303 }
304
305
306 #ifdef SK_SUPPORT_UNITTEST
307
308 #ifdef SK_BUILD_FOR_WIN32
309 static void sk_setenv(const char* key, const char* value) {
310 _putenv_s(key, value);
311 }
312 #else
313 static void sk_setenv(const char* key, const char* value) {
314 setenv(key, value, 1);
315 }
316 #endif
317
318 void SkRTConfRegistry::UnitTest() {
319 SkRTConfRegistry registryWithoutContents(true);
320
321 sk_setenv("skia_nonexistent_item", "132");
322 int result = 0;
323 registryWithoutContents.parse("nonexistent.item", &result);
324 SkASSERT(result == 132);
325 }
326
327 SkRTConfRegistry::SkRTConfRegistry(bool)
328 : fConfs(100) {
329 }
330 #endif
OLDNEW
« no previous file with comments | « include/utils/SkRTConf.h ('k') | tests/UnitTestTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698