瀏覽代碼

Never return NULL from String c_str() on Teensy 3.x

teensy4-core
PaulStoffregen 4 年之前
父節點
當前提交
2cd86801ae
共有 2 個文件被更改,包括 7 次插入1 次删除
  1. +2
    -0
      teensy3/WString.cpp
  2. +5
    -1
      teensy3/WString.h

+ 2
- 0
teensy3/WString.cpp 查看文件

@@ -484,6 +484,8 @@ unsigned char String::endsWith( const String &s2 ) const
/* Character Access */
/*********************************************/

const char String::zerotermination = 0;

char String::charAt(unsigned int loc) const
{
return operator[](loc);

+ 5
- 1
teensy3/WString.h 查看文件

@@ -164,7 +164,10 @@ public:
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
{getBytes((unsigned char *)buf, bufsize, index);}
const char * c_str() const { return buffer; }
const char * c_str() const {
if (!buffer) return &zerotermination; // https://forum.pjrc.com/threads/63842
return buffer;
}

// search
int indexOf( char ch ) const;
@@ -205,6 +208,7 @@ private:
// for more information http://www.artima.com/cppsource/safebool.html
typedef void (String::*StringIfHelperType)() const;
void StringIfHelper() const {}
static const char zerotermination;
public:
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
};

Loading…
取消
儲存