-
Notifications
You must be signed in to change notification settings - Fork 1k
lightningd: don't crash when truncating large log messages #9331
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
whitslack
wants to merge
5
commits into
ElementsProject:master
Choose a base branch
from
whitslack:log-msg-truncation-crash
base: master
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.
+40
−15
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ed3740
lightningd: don't crash when truncating large log messages
whitslack f3a9f38
alternative take logic
whitslack 30f80d2
take(new), take two
whitslack 6a5aee9
lightningd: test for very long log entries
Lagrang3 772d0af
squash! lightningd: don't crash when truncating large log messages
Lagrang3 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 |
|---|---|---|
|
|
@@ -264,18 +264,26 @@ static void del_front_log(struct log_book *log) | |
| } | ||
|
|
||
| /* We truncate genuinely giant messages */ | ||
| static char *cap_header(const tal_t *ctx, struct log_hdr *hdr, const char *msg) | ||
| static char *cap_header(const tal_t *ctx, struct log_hdr *hdr, const char *msg TAKES) | ||
| { | ||
| const size_t max = sizeof(((struct log_book *)0)->ringbuf) / 64; | ||
| if (hdr->msglen > max) { | ||
| msg = tal_fmt(ctx, "[TRUNCATED message from %zu bytes]: %.*s", | ||
| char *new; | ||
| new = tal_fmt(ctx, "[TRUNCATED message from %zu bytes]: %.*s", | ||
| hdr->msglen, (int)max, msg); | ||
| hdr->msglen = strlen(msg); | ||
| if (taken(msg)) | ||
| tal_free(msg); | ||
| msg = take(new); | ||
| hdr->msglen = tal_count(msg) - 1; | ||
| } | ||
| if (hdr->iolen > max) { | ||
| msg = tal_fmt(ctx, "[TRUNCATED IO from %zu bytes]: %.*s", | ||
| char *new; | ||
| new = tal_fmt(ctx, "[TRUNCATED IO from %zu bytes]: %.*s", | ||
| hdr->iolen, (int)hdr->msglen, msg); | ||
| hdr->msglen = strlen(msg); | ||
| if (taken(msg)) | ||
| tal_free(msg); | ||
| msg = take(new); | ||
| hdr->msglen = tal_count(msg) - 1; | ||
| hdr->iolen = max; | ||
| } | ||
| return cast_const(char *, msg); | ||
|
|
@@ -660,12 +668,8 @@ void logv(struct logger *log, enum log_level level, | |
| size_t log_len; | ||
| char *logmsg; | ||
|
|
||
| /* This is WARN_UNUSED_RESULT, because everyone should somehow deal | ||
| * with OOM, even though nobody does. */ | ||
| if (vasprintf(&logmsg, fmt, ap) == -1) | ||
| abort(); | ||
|
|
||
| log_len = strlen(logmsg); | ||
| logmsg = tal_vfmt(tmpctx, fmt, ap); | ||
| log_len = tal_count(logmsg) - 1; | ||
|
|
||
| /* Sanitize any non-printable characters, and replace with '?' */ | ||
| for (size_t i=0; i<log_len; i++) | ||
|
|
@@ -676,7 +680,7 @@ void logv(struct logger *log, enum log_level level, | |
| maybe_print(log, &l, logmsg, NULL); | ||
| maybe_notify_log(log, &l, logmsg); | ||
|
|
||
| logmsg = cap_header(tmpctx, &l, logmsg); | ||
| logmsg = cap_header(tmpctx, &l, take(logmsg)); | ||
| add_entry(log->log_book, &l, logmsg, NULL); | ||
|
|
||
| if (call_notifier) | ||
|
|
@@ -685,8 +689,9 @@ 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where the crash was occurring. The crash happens if |
||
|
|
||
| if (taken(logmsg)) | ||
| tal_free(logmsg); | ||
| errno = save_errno; | ||
| } | ||
|
|
||
|
|
@@ -700,20 +705,25 @@ void log_io(struct logger *log, enum log_level dir, | |
|
|
||
| assert(dir == LOG_IO_IN || dir == LOG_IO_OUT); | ||
|
|
||
| init_log_hdr(log, &l, dir, node_id, strlen(str), len); | ||
| size_t str_len = strlen(str); | ||
| init_log_hdr(log, &l, dir, node_id, str_len, len); | ||
|
|
||
| if (l.level >= log->print_level) | ||
| log_to_files(log->log_book->prefix, log->prefix->prefix, l.level, | ||
| l.nc ? &l.nc->node_id : NULL, | ||
| log->need_refiltering ? &log->log_book->print_filters : NULL, | ||
| &l.time, str, strlen(str), | ||
| &l.time, str, str_len, | ||
| data, len, | ||
| log->log_book->print_timestamps, | ||
| log->log_book->default_print_level, | ||
| log->log_book->log_files); | ||
|
|
||
| str = cap_header(tmpctx, &l, str); | ||
| add_entry(log->log_book, &l, str, data); | ||
| if (taken(str)) | ||
| tal_free(str); | ||
| if (taken(data)) | ||
| tal_free(data); | ||
| errno = save_errno; | ||
| } | ||
|
|
||
|
|
||
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
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.