Skip to content

Conversation

@Skyaero42
Copy link

@Skyaero42 Skyaero42 commented Oct 18, 2025

Replacing strcpy for strlcpy where applicable

  • No replacement when strcpy contains a magic string (verified that magic string fits into target parameter)
  • As with strlcat, ASCIIString and UnicodeString have not been touched.
  • No replacement when target parameter is a pointer instead of a char[].

TODO:

  • Replicate in Generals.

@Skyaero42 Skyaero42 added this to the Stability fixes milestone Oct 18, 2025
@Skyaero42 Skyaero42 self-assigned this Oct 18, 2025
@Skyaero42 Skyaero42 added Minor Severity: Minor < Major < Critical < Blocker Refactor Edits the code with insignificant behavior changes, is never user facing Stability Concerns stability of the runtime labels Oct 18, 2025
@Skyaero42 Skyaero42 force-pushed the skyaero/strcpy-to-strlcpy branch from 498a8e9 to db9bc33 Compare October 18, 2025 20:05
@Skyaero42 Skyaero42 force-pushed the skyaero/strcpy-to-strlcpy branch from db9bc33 to a11e0e0 Compare October 19, 2025 19:27
@Skyaero42 Skyaero42 marked this pull request as ready for review October 19, 2025 19:27
@Skyaero42 Skyaero42 changed the title fix: Replace strcat with strlcat for robustness fix: Replace strcpy with strlcpy for robustness Oct 21, 2025
@CollabCheck-StockholmUni

This comment was marked as off-topic.

Copy link

@xezon xezon left a 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));
Copy link

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert was not changed

@Skyaero42 Skyaero42 force-pushed the skyaero/strcpy-to-strlcpy branch 5 times, most recently from 38188de to 40dee38 Compare November 1, 2025 22:01
@Skyaero42
Copy link
Author

Changes addressing review comments added in a separate commit

@Skyaero42 Skyaero42 force-pushed the skyaero/strcpy-to-strlcpy branch from 40dee38 to fd90bbe Compare November 1, 2025 22:07
@Skyaero42 Skyaero42 requested a review from xezon November 1, 2025 22:13
Copy link

@xezon xezon left a 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));
Copy link

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;
Copy link

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));
Copy link

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];
Copy link

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));
Copy link

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));
Copy link

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 )
Copy link

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.

Copy link

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);
Copy link

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));
Copy link

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.

@xezon
Copy link

xezon commented Nov 6, 2025

Obsoleted pull

@xezon xezon closed this Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Minor Severity: Minor < Major < Critical < Blocker Refactor Edits the code with insignificant behavior changes, is never user facing Stability Concerns stability of the runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants