-
Notifications
You must be signed in to change notification settings - Fork 118
fix: Replace strcpy with strlcpy for robustness #1712
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
fix: Replace strcpy with strlcpy for robustness #1712
Conversation
498a8e9 to
db9bc33
Compare
db9bc33 to
a11e0e0
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
xezon
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Munkees really loved their string buffers.
Before continuing this change, I think we need another change that gets rid of all the redundant and obsolete buffer copies that I have raised in this review. I don't know if I caught them all, so more reviewing is required.
| return buffer; | ||
| } | ||
| strcpy(buffer, w3d_name); | ||
| strlcpy(buffer, w3d_name, ARRAY_SIZE(buffer)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same Assert as in sphereobj.cpp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert was not changed
cd6da64 to
994e0df
Compare
38188de to
40dee38
Compare
|
Changes addressing review comments added in a separate commit |
40dee38 to
fd90bbe
Compare
GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWaterTracks.cpp
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
Outdated
Show resolved
Hide resolved
xezon
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change now contains a bunch of edits that do not relate to strcpy replacements, but simplifications to get rid of unnecessary strcpy and related junk. How about we move them to a separate Pull Request?
| WWASSERT(name != NULL); | ||
| WWASSERT(strlen(name) < 2*W3D_NAME_LEN); | ||
| strcpy(Name,name); | ||
| strlcpy(Name, name, ARRAY_SIZE(Name)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use assert like in SphereRenderObjClass::Set_Name
|
|
||
| // Copy all characters from start to end (excluding 'end') | ||
| // into the w3d_name buffer. Then capitalize the string. | ||
| int num_chars = end - start; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_t to get rid of compiler warning
| return buffer; | ||
| } | ||
| strcpy(buffer, w3d_name); | ||
| strlcpy(buffer, w3d_name, ARRAY_SIZE(buffer)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert was not changed
| strcpy(s_buf, fname); | ||
| strlcpy(s_buf, fname, ARRAY_SIZE(s_buf)); | ||
|
|
||
| char tmp[256]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely _MAX_PATH
|
|
||
| // copy over the full path and filename | ||
| strcpy( m_savePathAndFilename, fullPathAndFilename ); | ||
| strlcpy(m_savePathAndFilename, fullPathAndFilename, ARRAY_SIZE(m_saveFilename)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong field for size.
| strlcat(curbuf, ".ini", ARRAY_SIZE(curbuf)); | ||
| strlcat(buffer, name.str(), ARRAY_SIZE(buffer)); | ||
| strlcat(buffer, "_BuildList", ARRAY_SIZE(buffer)); | ||
| strlcat(buffer, ".ini", ARRAY_SIZE(buffer)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can merge this cat with the one above.
| char buffer[ _MAX_PATH ]; | ||
| ::GetModuleFileName( NULL, buffer, sizeof( buffer ) ); | ||
| char *pEnd = buffer + strlen( buffer ); | ||
| while( pEnd != buffer ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This thing here looks like a poor mans strrchr and can be simplied. There are 3 of these per game in code base. Can do in separate change because is not directly related to strcpy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created task with #1781
|
|
||
| FilenameList filenameList; | ||
| TheFileSystem->getFileListInDirectory(AsciiString(dirBuf), AsciiString("*.w3d"), filenameList, FALSE); | ||
| TheFileSystem->getFileListInDirectory(AsciiString(".\\data\\Editor\\Molds\\"), AsciiString("*.w3d"), filenameList, FALSE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove AsciiString. You already did in another file too.
| strlcat(fileBuf, "/", ARRAY_SIZE(findBuf)); | ||
| strlcat(fileBuf, token.str(), ARRAY_SIZE(findBuf)); | ||
| strlcpy(fileBuf, TEST_STRING, ARRAY_SIZE(fileBuf)); | ||
| strlcat(fileBuf, "/", ARRAY_SIZE(fileBuf)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can merge this cat with the one above. TEST_STRING "/" is valid.
|
Obsoleted pull |
Replacing
strcpyforstrlcpywhere applicableTODO: