Skip to content

Conversation

@yuri91
Copy link
Member

@yuri91 yuri91 commented May 1, 2025

the implementation of to_string() passes the address of the first element of a default-initialized string to snprintf. Normally, this is always a valid address because of the sso, but in Cheerp we don't do it so the size is 0 and the address is invalid. As a fix we initialize the string with an initial size > 0.

…mization

the implementation of to_string() passes the address of the first
element of a default-initialized string to snprintf.
Normally, this is always a valid address because of the sso, but in
Cheerp we don't do it so the size is 0 and the address is invalid.
As a fix we initialize the string with an initial size > 0.
@yuri91 yuri91 requested review from alexp-sssup and Copilot May 1, 2025 13:27
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses a bug in the implementation of to_string() caused by relying on small string optimization, which is not available in Cheerp.

  • Replace default-initialized string construction with a fixed-size initialization to ensure a valid internal buffer.
  • Adjust the string size to match its capacity after initialization.


template <>
struct initial_string<string> {
string operator()() const {
Copy link

Copilot AI May 1, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding a comment explaining the choice of the fixed size '20' to clarify its sufficiency for preventing the invalid address issue in Cheerp.

Suggested change
string operator()() const {
string operator()() const {
// The fixed size '20' is chosen as an initial buffer size to ensure
// sufficient space for typical use cases and to prevent invalid address
// issues in Cheerp. The size can dynamically grow if needed.

Copilot uses AI. Check for mistakes.
@alexp-sssup alexp-sssup merged commit dd7412f into master May 7, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants