From: nuno-lopes Date: Wed, 9 Apr 2008 22:57:45 +0000 (+0000) Subject: fix a memory error detected with valgrind in raw_token() (the string returned by... X-Git-Tag: 0.13.6~45 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7d8b204162ba10937fdf98b787dce3092b816d9d;p=re2c fix a memory error detected with valgrind in raw_token() (the string returned by it was freeded when the function returned --- diff --git a/re2c/code.cc b/re2c/code.cc index 761b6cda..cb304428 100644 --- a/re2c/code.cc +++ b/re2c/code.cc @@ -2450,9 +2450,9 @@ void Scanner::check_token_length(char *pos, uint len) const } } -SubStr Scanner::raw_token(std::string enclosure) const +Str Scanner::raw_token(std::string enclosure) const { - return SubStr(std::string(enclosure + token().to_string() + enclosure).c_str()); + return Str(std::string(enclosure + token().to_string() + enclosure).c_str()); } void Scanner::reuse() diff --git a/re2c/scanner.h b/re2c/scanner.h index db6528f8..3e59d3a0 100644 --- a/re2c/scanner.h +++ b/re2c/scanner.h @@ -67,7 +67,7 @@ public: void check_token_length(char *pos, uint len) const; SubStr token() const; SubStr token(uint start, uint len) const; - SubStr raw_token(std::string enclosure) const; + Str raw_token(std::string enclosure) const; virtual uint get_line() const; uint xlat(uint c) const; diff --git a/re2c/substr.cc b/re2c/substr.cc index 771a925f..1b6f5d7b 100644 --- a/re2c/substr.cc +++ b/re2c/substr.cc @@ -43,6 +43,12 @@ Str::Str(Str& s) s.len = 0; } +Str::Str(const char *s) + : SubStr(strdup(s), strlen(s)) +{ + ; +} + Str::Str() : SubStr((char*) NULL, 0) { @@ -55,8 +61,8 @@ Str::~Str() if (str) { free((void*)str); } - str = (char*) - 1; - len = (uint) - 1; + str = NULL; + len = 0; } } // end namespace re2c diff --git a/re2c/substr.h b/re2c/substr.h index 26fcb3be..e6523180 100644 --- a/re2c/substr.h +++ b/re2c/substr.h @@ -38,6 +38,7 @@ class Str: public SubStr public: Str(const SubStr&); Str(Str&); + Str(const char*); Str(); ~Str(); };