diff --git a/example/jsonparse.cpp b/example/jsonparse.cpp index ba26b8a..e28b6d0 100644 --- a/example/jsonparse.cpp +++ b/example/jsonparse.cpp @@ -95,6 +95,7 @@ void testJsonSuite() if (dir == NULL) { printf("Error: failed to open jsontest directory\n"); + closedir(dir); return; } @@ -122,6 +123,8 @@ void testJsonSuite() } } } + + closedir(dir); } #endif diff --git a/include/str.h b/include/str.h index c4b4f09..dd3356a 100644 --- a/include/str.h +++ b/include/str.h @@ -534,8 +534,13 @@ class StrBld const char* c_str() { char* buf = (char*)mBuf.ptr(); - // Lazy null termination - buf[mLen] = '\0'; + if(buf) + { + // Lazy null termination + buf[mLen] = '\0'; + } + // May return buffer without zero termination, + // but better than writing to null pointer. return buf; } std::string toString() @@ -554,7 +559,17 @@ class StrBld inline int compare(const char* s) { - return strcmp(c_str(), s); + const char* cp = c_str(); + + if (cp) + { + return strcmp(cp, s); + } + else + { + // cp is null, fallback to direct comparison + return cp != s; + } } inline bool equals(const char* s) {