Skip to content

Resolved Issue #2939 #2940

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

Closed
wants to merge 1 commit into from
Closed

Conversation

avibega23
Copy link

  1. Input Validation in main() function:
    Issue: The original code did not check for non-positive values of n (array size). As a result, entering a value of n <= 0 would cause buffer overflow or undefined behavior when the array was accessed.

Change: Added input validation for the array size n to ensure it is positive. If n <= 0, the program prints an error message and terminates gracefully:

if (n <= 0) {
std::cerr << "Error: Array size must be a positive integer.\n";
return 1;
}

  1. Handling Empty Arrays in median_of_medians() function:
    Issue: In the original code, if the array was empty or the median vector m ended up being empty, the program would try to access m[0], causing a buffer overflow.

Change: Before accessing m[(sz - 1) / 2], a check was added to ensure that the median vector m is not empty. If the vector is empty, an error message is printed, and the program exits:

if (m.empty()) {
std::cerr << "Error: Median vector is empty.\n";
exit(1);
}

  1. Graceful Error Handling for Invalid Inputs:
    Issue: The code previously did not handle invalid inputs properly, and it would crash with a segmentation fault when invalid input was given.

Change: Instead of continuing with invalid or empty inputs, the program now handles such inputs gracefully by printing error messages and terminating cleanly, thus preventing crashes and undefined behavior.

Example: If n <= 0 is entered, the program prints:

"Error: Array size must be a positive integer."

  1. Edge Case Handling in Test Cases:
    Issue: The original test cases did not cover edge cases like empty arrays or non-positive sizes.

Change: Though not explicitly mentioned in the test section, handling of invalid inputs was prioritized in the main function, ensuring no test cases would be executed for invalid inputs.

@Bimaah
Copy link

Bimaah commented Apr 29, 2025 via email

@@ -131,7 +136,11 @@ int main()
int n = 0;
std::cout << "Enter Size of Array: ";
std::cin >> n;
std::vector<int> a(n);
if (n <= 0) {
std::cerr << "Error: Array size must be a positive integer.\n";
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldn't this throw an error instead?

Copy link
Contributor

This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale Author has not responded to the comments for over 2 weeks label Jun 19, 2025
Copy link
Contributor

Please ping one of the maintainers once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to ask for help in our Gitter channel or our Discord server. Thank you for your contributions!

@github-actions github-actions bot closed this Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Author has not responded to the comments for over 2 weeks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants