Skip to content

Conversation

@Khush0031
Copy link

Fix : #3055

Description of Change

This fixes an out-of-bounds access in the string counting sort implementation located at sorting/counting_sort_string.cpp. The previous implementation wrote into an empty std::string (output) without pre-allocating it, which causes undefined behavior and potential crashes.

Changes

  • countSort now takes std::string& (modify in-place).
  • output is pre-allocated to the input length.
  • Uses unsigned char when indexing the count array to avoid negative/undefined indexing.
  • Builds output with right-to-left iteration to preserve stability.
  • Early return for empty or single-character strings.

Testing

  • Basic test program included (locally) was used to verify expected sorted outputs for several strings including edge cases (empty string, single char).

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

…output, use unsigned char indexing, stable placement)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant