-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
Resolved Issue #2939 #2940
Conversation
Para de me mandar e-mails seus desgraçados
Em ter., 29 de abr. de 2025, 08:47, Avinoor Singh ***@***.***>
escreveu:
…
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;
}
2. 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);
}
3. 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."
4. 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.
------------------------------
You can view, comment on, or merge this pull request online at:
#2940
Commit Summary
- e8243aa
<e8243aa>
Resolved Issue #2939
File Changes
(1 file <https://github.com/TheAlgorithms/C-Plus-Plus/pull/2940/files>)
- *M* search/median_search.cpp
<https://github.com/TheAlgorithms/C-Plus-Plus/pull/2940/files#diff-b8d10c25804fdac896d6e6f43a12f21332c7b44431237612a6a3f3b107620729>
(17)
Patch Links:
- https://github.com/TheAlgorithms/C-Plus-Plus/pull/2940.patch
- https://github.com/TheAlgorithms/C-Plus-Plus/pull/2940.diff
—
Reply to this email directly, view it on GitHub
<#2940>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BFBT2GMM6IR3UIFPEC4MVWD235RCXAVCNFSM6AAAAAB4CN5OSOVHI2DSMVQWIX3LMV43ASLTON2WKOZTGAZDONJYGE4DQNY>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
@@ -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"; |
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.
shouldn't this throw an error instead?
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. |
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! |
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;
}
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);
}
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."
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.