Permalink
Browse files

Fix issue #31

  • Loading branch information...
1 parent 617c790 commit e6d37ff64d3dd13a3c062e36b038c2254ae0b170 @PerGraa PerGraa committed Nov 3, 2016
Showing with 18 additions and 3 deletions.
  1. +18 −3 include/str.h
View
@@ -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)
{

0 comments on commit e6d37ff

Please sign in to comment.