Fix cache path initialization and set.

Add null terminator to strings
Return nullptr if string is not set

bug 20894664

Change-Id: I33f5d84d56f6299558f5d8713694e40afc82dd20
diff --git a/rsContext.cpp b/rsContext.cpp
index 29b4036..fba06cd 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -459,8 +459,26 @@
     mContextType = RS_CONTEXT_TYPE_NORMAL;
     mSynchronous = false;
     mFatalErrorOccured = false;
+
+    memset(mCacheDir, 0, sizeof(mCacheDir));
+#ifdef RS_COMPATIBILITY_LIB
+    memset(nativeLibDir, 0, sizeof(nativeLibDir));
+#endif
 }
 
+void Context::setCacheDir(const char * cacheDir_arg, uint32_t length) {
+    if (!hasSetCacheDir) {
+        if (length <= PATH_MAX) {
+            memcpy(mCacheDir, cacheDir_arg, length);
+            mCacheDir[length] = 0;
+            hasSetCacheDir = true;
+        } else {
+            setError(RS_ERROR_BAD_VALUE, "Invalid path");
+        }
+    }
+}
+
+
 Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
                                  RsContextType ct, uint32_t flags) {
     Context * rsc = new Context();