lightningd: don't crash when truncating large log messages#9331
Open
whitslack wants to merge 1 commit into
Open
lightningd: don't crash when truncating large log messages#9331whitslack wants to merge 1 commit into
whitslack wants to merge 1 commit into
Conversation
It's not okay to call free() on the pointer to a truncated log message that was allocated by tal_fmt() in cap_header(). Let's call tal_free() instead, and rather than calling vasprintf() to malloc the log message in the first place, let's call tal_vfmt(). Also, since we're now always using a tallocated string for the log message, let's have cap_header() take ownership of it and either free it if it truncated the message (and is returning a different pointer to the truncated message) or else return the taken original pointer without freeing it. Also, log_io()'s str and data parameters are marked TAKES, but the function was not actually taking them, so fix that up too. Also, don't call strlen() on a string returned by tal_fmt(). The returned pointer is guaranteed to have tal_count() == strlen() + 1, so there's no sense in scanning through the string to find its length. Changelog-None
whitslack
commented
Jul 19, 2026
| @@ -685,8 +684,8 @@ void logv(struct logger *log, enum log_level level, | |||
| l.time, | |||
| l.prefix->prefix, | |||
| logmsg); | |||
| free(logmsg); | |||
Collaborator
Author
There was a problem hiding this comment.
This is where the crash was occurring. The crash happens if cap_header() tallocates and returns a new string to hold the truncated message. Then we'll be trying to free() a tal pointer. Boom.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
It's not okay to call
free()on the pointer to a truncated log message that was allocated bytal_fmt()incap_header(). Let's calltal_free()instead, and rather than callingvasprintf()to malloc the log message in the first place, let's calltal_vfmt().Also, since we're now always using a tallocated string for the log message, let's have
cap_header()take ownership of it and either free it if it truncated the message (and is returning a different pointer to the truncated message) or else return the taken original pointer without freeing it.Also,
log_io()'sstranddataparameters are markedTAKES, but the function was not actually taking them, so fix that up too.Also, don't call
strlen()on a string returned bytal_fmt(). The returned pointer is guaranteed to havetal_count() == strlen() + 1, so there's no sense in scanning through the string to find its length.Checklist
Before submitting the PR, ensure the following tasks are completed. If an item is not applicable to your PR, please mark it as checked:
tools/lightning-downgradeN/A