-
-
Notifications
You must be signed in to change notification settings - Fork 843
ICU-23155 Fix buffer overflow in SSearchTest::offsetTest() #3546
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
Open
gvictor
wants to merge
1
commit into
unicode-org:main
Choose a base branch
from
gvictor:ICU23155
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+31
−22
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,9 @@ | |
| #include "ssearch.h" | ||
| #include "xmlparser.h" | ||
|
|
||
| #include <iomanip> | ||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <stdio.h> // for snprintf | ||
|
|
||
| char testId[100]; | ||
|
|
@@ -524,40 +527,48 @@ UBool OrderList::matchesAt(int32_t offset, const OrderList &other) const | |
| return true; | ||
| } | ||
|
|
||
| static char *printOffsets(char *buffer, size_t n, OrderList &list) | ||
| static char* printOffsets(std::string &buffer, OrderList &list) | ||
| { | ||
| int32_t size = list.size(); | ||
| char *s = buffer; | ||
| buffer.clear(); | ||
| buffer.reserve(size * 10 + 2); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this some kind of performance optimization, to avoid having to dynamically resize the buffer? If so, I suggest removing it, this is test code, the possible performance gain is irrelevant compared to having a few extra lines of code to maintain. |
||
| std::stringstream s(buffer); | ||
|
|
||
| for(int32_t i = 0; i < size; i += 1) { | ||
| const Order *order = list.get(i); | ||
|
|
||
| if (i != 0) { | ||
| s += snprintf(s, n, ", "); | ||
| s << ", "; | ||
| } | ||
|
|
||
| s += snprintf(s, n, "(%d, %d)", order->lowOffset, order->highOffset); | ||
| s << "(" << order->lowOffset << ", " << order->highOffset << ")"; | ||
| } | ||
|
|
||
| return buffer; | ||
| return buffer.data(); | ||
| } | ||
|
|
||
| static char *printOrders(char *buffer, size_t n, OrderList &list) | ||
| static char* printOrders(std::string &buffer, OrderList &list) | ||
| { | ||
| int32_t size = list.size(); | ||
| char *s = buffer; | ||
| buffer.clear(); | ||
| // 10 chars is enough room for 8 hex digits plus ", " | ||
| buffer.reserve(size * 10 + 2); | ||
| std::stringstream s(buffer); | ||
|
|
||
| for(int32_t i = 0; i < size; i += 1) { | ||
| const Order *order = list.get(i); | ||
|
|
||
| if (i != 0) { | ||
| s += snprintf(s, n, ", "); | ||
| s << ", "; | ||
| } | ||
|
|
||
| s += snprintf(s, n, "%8.8X", order->order); | ||
| s << std::hex | ||
| << std::uppercase | ||
| << std::setw(8) | ||
| << std::setfill('0') | ||
| << order->order; | ||
| } | ||
|
|
||
| return buffer; | ||
| return buffer.data(); | ||
| } | ||
|
|
||
| void SSearchTest::offsetTest() | ||
|
|
@@ -631,9 +642,7 @@ void SSearchTest::offsetTest() | |
| errcheckln(status, "Failed to create collator in offsetTest! - %s", u_errorName(status)); | ||
| return; | ||
| } | ||
| char buffer[4096]; // A bit of a hack... just happens to be long enough for all the test cases... | ||
| // We could allocate one that's the right size by (CE_count * 10) + 2 | ||
| // 10 chars is enough room for 8 hex digits plus ", ". 2 extra chars for "[" and "]" | ||
| std::string buffer; | ||
|
|
||
| col->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status); | ||
|
|
||
|
|
@@ -673,20 +682,20 @@ void SSearchTest::offsetTest() | |
|
|
||
| if (forwardList.compare(backwardList)) { | ||
| logln("Works with \"%s\"", test[i]); | ||
| logln("Forward offsets: [%s]", printOffsets(buffer, sizeof(buffer), forwardList)); | ||
| // logln("Backward offsets: [%s]", printOffsets(buffer, sizeof(buffer), backwardList)); | ||
| logln("Forward offsets: [%s]", printOffsets(buffer, forwardList)); | ||
| // logln("Backward offsets: [%s]", printOffsets(buffer, backwardList)); | ||
|
|
||
| logln("Forward CEs: [%s]", printOrders(buffer, sizeof(buffer), forwardList)); | ||
| // logln("Backward CEs: [%s]", printOrders(buffer, sizeof(buffer), backwardList)); | ||
| logln("Forward CEs: [%s]", printOrders(buffer, forwardList)); | ||
| // logln("Backward CEs: [%s]", printOrders(buffer, backwardList)); | ||
|
|
||
| logln(); | ||
| } else { | ||
| errln("Fails with \"%s\"", test[i]); | ||
| infoln("Forward offsets: [%s]", printOffsets(buffer, sizeof(buffer), forwardList)); | ||
| infoln("Backward offsets: [%s]", printOffsets(buffer, sizeof(buffer), backwardList)); | ||
| infoln("Forward offsets: [%s]", printOffsets(buffer, forwardList)); | ||
| infoln("Backward offsets: [%s]", printOffsets(buffer, backwardList)); | ||
|
|
||
| infoln("Forward CEs: [%s]", printOrders(buffer, sizeof(buffer), forwardList)); | ||
| infoln("Backward CEs: [%s]", printOrders(buffer, sizeof(buffer), backwardList)); | ||
| infoln("Forward CEs: [%s]", printOrders(buffer, forwardList)); | ||
| infoln("Backward CEs: [%s]", printOrders(buffer, backwardList)); | ||
|
|
||
| infoln(); | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This would all become considerably simpler if the caller wasn't required to pass in any buffer at all and the function simply returned an
std::stringobject by value. That's what any normal C++ code would do nowadays.