Skip to content

libSyntax: ensure copy constructor of OwnedString to release previously allocated data. rdar://35116413 #12610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions include/swift/Basic/OwnedString.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ enum class StringOwnership {
class OwnedString {
const char *Data;
size_t Length;
StringOwnership Ownership;
StringOwnership Ownership = StringOwnership::Unowned;

OwnedString(const char* Data, size_t Length, StringOwnership Ownership)
: Length(Length), Ownership(Ownership) {
assert(Length >= 0 && "expected length to be non-negative");
void release() {
if (Ownership == StringOwnership::Copied)
free(const_cast<char *>(Data));
}

void initialize(const char* Data, size_t Length, StringOwnership Ownership) {
this->Length = Length;
this->Ownership = Ownership;
assert(Length >= 0 && "expected length to be non-negative");
if (Ownership == StringOwnership::Copied && Data) {
char *substring = static_cast<char *>(malloc(Length + 1));
assert(substring && "expected successful malloc of copy");
Expand All @@ -60,20 +65,48 @@ class OwnedString {
else
this->Data = Data;
}

OwnedString(const char* Data, size_t Length, StringOwnership Ownership) {
initialize(Data, Length, Ownership);
}
public:
OwnedString() : OwnedString(nullptr, 0, StringOwnership::Unowned) {}
OwnedString(): OwnedString(nullptr, 0, StringOwnership::Unowned) {}

OwnedString(const char *Data, size_t Length)
: OwnedString(Data, Length, StringOwnership::Unowned) {}
OwnedString(const char *Data, size_t Length):
OwnedString(Data, Length, StringOwnership::Unowned) {}

OwnedString(StringRef Str) : OwnedString(Str.data(), Str.size()) {}

OwnedString(const char *Data) : OwnedString(StringRef(Data)) {}

OwnedString(const OwnedString &Other)
: OwnedString(Other.Data, Other.Length, Other.Ownership) {}

OwnedString(const OwnedString &Other):
OwnedString(Other.Data, Other.Length, Other.Ownership) {}

OwnedString(OwnedString &&Other): Data(Other.Data), Length(Other.Length),
Ownership(Other.Ownership) {
Other.Data = nullptr;
Other.Ownership = StringOwnership::Unowned;
}

OwnedString& operator=(const OwnedString &Other) {
if (&Other != this) {
release();
initialize(Other.Data, Other.Length, Other.Ownership);
}
return *this;
}

OwnedString& operator=(OwnedString &&Other) {
if (&Other != this) {
release();
this->Data = Other.Data;
this->Length = Other.Length;
this->Ownership = Other.Ownership;
Other.Ownership = StringOwnership::Unowned;
Other.Data = nullptr;
}
return *this;
}

OwnedString copy() {
return OwnedString(Data, Length, StringOwnership::Copied);
}
Expand All @@ -99,8 +132,7 @@ class OwnedString {
}

~OwnedString() {
if (Ownership == StringOwnership::Copied)
free(const_cast<char *>(Data));
release();
}
};

Expand Down