-
Notifications
You must be signed in to change notification settings - Fork 5k
fix truncated event log message #41327
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6a49d8f
fix truncated event log
intxgo 934ec84
changelog
intxgo c72e941
fix warning
intxgo 6b11207
fix golint
intxgo a6a0271
playing hide an catch with CI
intxgo 2a3b3eb
size in bytes
intxgo 9bbf354
Merge branch 'main' into fix-truncated-log
intxgo 15727de
review
intxgo 2437ead
code review
intxgo 05347c4
add comment, unify code path
intxgo 332eb03
refactor code
intxgo 187f11a
Merge branch 'main' into fix-truncated-log
intxgo 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
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
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
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 |
|---|---|---|
|
|
@@ -403,35 +403,35 @@ func FormatEventString( | |
| } | ||
|
|
||
| var bufferPtr *byte | ||
| if renderBuf != nil { | ||
| if len(renderBuf) > 0 { | ||
|
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. 👍 |
||
| bufferPtr = &renderBuf[0] | ||
| } | ||
|
|
||
| // EvtFormatMessage operates with WCHAR buffer, assuming the size of the buffer in characters. | ||
| // https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtformatmessage | ||
| var bufferNeeded uint32 | ||
| bufferSize := uint32(len(renderBuf) / 2) | ||
| var wcharBufferUsed uint32 | ||
| wcharBufferSize := uint32(len(renderBuf) / 2) | ||
|
|
||
| err := _EvtFormatMessage(ph, eventHandle, 0, 0, nil, messageFlag, bufferSize, bufferPtr, &bufferNeeded) | ||
| if err != nil && err != windows.ERROR_INSUFFICIENT_BUFFER { //nolint:errorlint // This is an errno. | ||
| err := _EvtFormatMessage(ph, eventHandle, 0, 0, nil, messageFlag, wcharBufferSize, bufferPtr, &wcharBufferUsed) | ||
| if err != nil && !errors.Is(err, windows.ERROR_INSUFFICIENT_BUFFER) { | ||
| return fmt.Errorf("failed in EvtFormatMessage: %w", err) | ||
| } else if err == nil { | ||
| // Windows API returns a null terminated WCHAR C-style string in the buffer. bufferNeeded applies | ||
| // only when ERROR_INSUFFICIENT_BUFFER is returned. Luckily the UTF16ToUTF8Bytes/UTF16ToString | ||
| // functions stop at null termination. Note, as signaled in a comment at the end of this function, | ||
| // this behavior is bad for EvtFormatMessageKeyword as then the API returns a list of null terminated | ||
| // strings in the buffer (it's fine for now as we don't use this parameter value). | ||
| return common.UTF16ToUTF8Bytes(renderBuf, out) | ||
| // wcharBufferUsed indicates the size used internally to render the message. When called with nil buffer | ||
| // EvtFormatMessage returns ERROR_INSUFFICIENT_BUFFER, but otherwise succeeds copying only up to | ||
| // wcharBufferSize to our buffer, truncating the message if our buffer was too small. | ||
| if wcharBufferUsed <= wcharBufferSize { | ||
| return common.UTF16ToUTF8Bytes(renderBuf[:wcharBufferUsed*2], out) | ||
| } | ||
| } | ||
|
|
||
| // Get a buffer from the pool and adjust its length. | ||
| bb := sys.NewPooledByteBuffer() | ||
| defer bb.Free() | ||
|
|
||
| bb.Reserve(int(bufferNeeded * 2)) | ||
| bufferSize = bufferNeeded | ||
| bb.Reserve(int(wcharBufferUsed * 2)) | ||
| wcharBufferSize = wcharBufferUsed | ||
|
|
||
| err = _EvtFormatMessage(ph, eventHandle, 0, 0, nil, messageFlag, bufferSize, bb.PtrAt(0), &bufferNeeded) | ||
| err = _EvtFormatMessage(ph, eventHandle, 0, 0, nil, messageFlag, wcharBufferSize, bb.PtrAt(0), &wcharBufferUsed) | ||
| if err != nil { | ||
| return fmt.Errorf("failed in EvtFormatMessage: %w", err) | ||
| } | ||
|
|
@@ -550,20 +550,36 @@ func evtRenderProviderName(renderBuf []byte, eventHandle EvtHandle) (string, err | |
| } | ||
|
|
||
| func renderXML(eventHandle EvtHandle, flag EvtRenderFlag, renderBuf []byte, out io.Writer) error { | ||
| var bufferUsed, propertyCount uint32 | ||
| err := _EvtRender(0, eventHandle, flag, uint32(len(renderBuf)), | ||
| &renderBuf[0], &bufferUsed, &propertyCount) | ||
| if err == ERROR_INSUFFICIENT_BUFFER { //nolint:errorlint // This is an errno or nil. | ||
| return sys.InsufficientBufferError{Cause: err, RequiredSize: int(bufferUsed)} | ||
| var bufferUsed, bufferSize, propertyCount uint32 | ||
| var bufferPtr *byte | ||
|
|
||
| bufferSize = uint32(len(renderBuf)) | ||
| if bufferSize > 0 { | ||
| bufferPtr = &renderBuf[0] | ||
| } | ||
| if err != nil { | ||
| err := _EvtRender(0, eventHandle, flag, bufferSize, bufferPtr, &bufferUsed, &propertyCount) | ||
| if err != nil && !errors.Is(err, windows.ERROR_INSUFFICIENT_BUFFER) { | ||
| return err | ||
| } else if err == nil { | ||
| // bufferUsed indicates the size used internally to render the message. When called with nil buffer | ||
| // EvtRender returns ERROR_INSUFFICIENT_BUFFER, but otherwise succeeds copying only up to | ||
| // bufferSize to our buffer, truncating the message if our buffer was too small. | ||
| if bufferUsed <= bufferSize { | ||
| return common.UTF16ToUTF8Bytes(renderBuf[:bufferUsed], out) | ||
| } | ||
| } | ||
|
|
||
| if int(bufferUsed) > len(renderBuf) { | ||
| return fmt.Errorf("Windows EvtRender reported that wrote %d bytes "+ | ||
| "to the buffer, but the buffer can only hold %d bytes", | ||
| bufferUsed, len(renderBuf)) | ||
| // Get a buffer from the pool and adjust its length. | ||
| bb := sys.NewPooledByteBuffer() | ||
| defer bb.Free() | ||
|
|
||
| bb.Reserve(int(bufferUsed)) | ||
| bufferSize = bufferUsed | ||
|
|
||
| err = _EvtRender(0, eventHandle, flag, bufferSize, bb.PtrAt(0), &bufferUsed, &propertyCount) | ||
| if err != nil { | ||
| return fmt.Errorf("failed in EvtRender: %w", err) | ||
| } | ||
| return common.UTF16ToUTF8Bytes(renderBuf[:bufferUsed], out) | ||
|
|
||
| return common.UTF16ToUTF8Bytes(bb.Bytes(), out) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.