[Scummvm-git-logs] scummvm master -> dc7b0a79094ca99691960582a32b24afa0710f78

criezy criezy at scummvm.org
Sun Jul 19 22:07:39 UTC 2020


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
dc7b0a7909 COMMON: Dedicated function for in buffer check


Commit: dc7b0a79094ca99691960582a32b24afa0710f78
    https://github.com/scummvm/scummvm/commit/dc7b0a79094ca99691960582a32b24afa0710f78
Author: Henrik "Henke37" Andersson (henke at henke37.cjb.net)
Date: 2020-07-19T23:07:35+01:00

Commit Message:
COMMON: Dedicated function for in buffer check

It's UB to compare pointers that aren't from the same array. Cast to uintptr to reduce the issue to IB.

Changed paths:
    common/str.cpp
    common/str.h


diff --git a/common/str.cpp b/common/str.cpp
index 77a50bd768..535cf43876 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -284,7 +284,7 @@ String &String::operator=(char c) {
 }
 
 String &String::operator+=(const char *str) {
-	if (_str <= str && str <= _str + _size)
+	if (pointerInOwnBuffer(str))
 		return operator+=(String(str));
 
 	int len = strlen(str);
@@ -297,6 +297,16 @@ String &String::operator+=(const char *str) {
 	return *this;
 }
 
+bool String::pointerInOwnBuffer(const char *str) const {
+	//compared pointers must be in the same array or UB
+	//cast to intptr however is IB
+	//which includes comparision of the values
+	uintptr ownBuffStart = (uintptr)_str;
+	uintptr ownBuffEnd = (uintptr)(_str + _size);
+	uintptr candidateAddr = (uintptr)str;
+	return ownBuffStart <= candidateAddr && candidateAddr <= ownBuffEnd;
+}
+
 String &String::operator+=(const String &str) {
 	if (&str == this)
 		return operator+=(String(str));
diff --git a/common/str.h b/common/str.h
index bce55fd210..0b2a9f6b89 100644
--- a/common/str.h
+++ b/common/str.h
@@ -398,6 +398,8 @@ protected:
 	void decRefCount(int *oldRefCount);
 	void initWithCStr(const char *str, uint32 len);
 
+	bool pointerInOwnBuffer(const char *str) const;
+
 	void decodeUTF8(U32String &dst) const;
 	void decodeOneByte(U32String &dst, CodePage page) const;
 };




More information about the Scummvm-git-logs mailing list