Fix crash when querying a runtime config that is defined in environment
Fix crash when querying a runtime config in case when there's no
skia.conf file and the config variable is still defined in the
environment.
Runs the added SkRTConf::UnitTest test as part of new "UnitTest" test.
Previous version of the patch failed Windows build due to setenv usage.
On Windows, use _putenv_s instead.
BUG=skia:1494
R=bsalomon@google.com, humper@google.com
Author: kkinnunen@nvidia.com
Review URL: https://chromiumcodereview.appspot.com/23174002
git-svn-id: http://skia.googlecode.com/svn/trunk@10715 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/utils/SkRTConf.cpp b/src/utils/SkRTConf.cpp
index e0977fe..6fcc318 100644
--- a/src/utils/SkRTConf.cpp
+++ b/src/utils/SkRTConf.cpp
@@ -214,6 +214,7 @@
template<typename T> bool SkRTConfRegistry::parse(const char *name, T* value) {
SkString *str = NULL;
+ SkString tmp;
for (int i = fConfigFileKeys.count() - 1 ; i >= 0; i--) {
if (fConfigFileKeys[i]->equals(name)) {
@@ -227,6 +228,9 @@
const char *environment_value = getenv(environment_variable.c_str());
if (environment_value) {
+ if (NULL == str) {
+ str = &tmp;
+ }
str->set(environment_value);
} else {
// apparently my shell doesn't let me have environment variables that
@@ -238,6 +242,9 @@
sk_free(underscore_name);
environment_value = getenv(underscore_environment_variable.c_str());
if (environment_value) {
+ if (NULL == str) {
+ str = &tmp;
+ }
str->set(environment_value);
}
}
@@ -294,3 +301,30 @@
static SkRTConfRegistry r;
return r;
}
+
+
+#ifdef SK_SUPPORT_UNITTEST
+
+#ifdef SK_BUILD_FOR_WIN32
+static void sk_setenv(const char* key, const char* value) {
+ _putenv_s(key, value);
+}
+#else
+static void sk_setenv(const char* key, const char* value) {
+ setenv(key, value, 1);
+}
+#endif
+
+void SkRTConfRegistry::UnitTest() {
+ SkRTConfRegistry registryWithoutContents(true);
+
+ sk_setenv("skia_nonexistent_item", "132");
+ int result = 0;
+ registryWithoutContents.parse("nonexistent.item", &result);
+ SkASSERT(result == 132);
+}
+
+SkRTConfRegistry::SkRTConfRegistry(bool)
+ : fConfs(100) {
+}
+#endif