Closed leecbaker closed 4 years ago
This shouldn't cause any issues. But may be worth fixing. It is a trim on a persistant string called at startup, taken from https://www.linuxquestions.org/questions/programming-9/read-parameters-from-config-file-file-parser-362188/
the string loaded into memory might be " hello " this trim tweeks the pointer such that referencing string only returns "hello" with probably "\0o " hanging off the end never used.
This, at least, is why it overlaps.
I'll have a look if there is a better trim implementation for C, I like how the mac tools check for things like this. Its Java equivalent is String.trim()
I'd quite like to replace the C++ plugin side strings with a utf8 implementation, using ANSI is terrible for i8n.
You can fix it really easily by replacing the strcpy()
with memmove (s, s1, s2 - s1 + 1);
. Should be the same thing, but memmove()
permits overlapping regions. I think strcpy()
(and memcpy()
) don't allow overlapping inputs is because it removes a lot of complexity, and opens up some additional optimization possibilities.
I was using AddressSanitizer- this is an amazing tool for detecting all sorts of problems. It's available in GCC and Clang, and probably Windows as well. You just have to set up the right compilation flags.
If you want to use C++ strings here, it's easy to use boost::trim()
, or you can make your own fairly easily. PlaneCommand uses this implementation, which is really not optimal but works correctly:
inline void string_trim(std::string & string) {
while(false == string.empty() && std::isspace(static_cast<int>(string.front()))) {
string.erase(string.begin());
}
while(false == string.empty() && std::isspace(static_cast<int>(string.back()))) {
string.pop_back();
}
}
It doesn't need to move any memory around because it is just changing the contents of single a fixed length array and the result is always at most the same length.
probably easier to replace it with
while (*s != '\0') { *s1 = *s; s1++; s++; } *s1 = '\0';
just in case the implementation is different.
added to next commit
While debugging #56, my tools picked up the following bug.
strcpy()
's two parameters' ranges should not overlap; when they overlap, this is undefined behavior (reference), perhaps resulting in memory corruption.One way to fix this is to use
memmove()
.This happens here: https://github.com/mSparks43/XPlane-11-AutoATC-plugin/blob/27e9f5ebb6d0621a76fe2fb87d2f960db1c3da29/src/jvm.cpp#L380
Here's the tool output. Once we know what's happening here, probably don't need to read through this.