Report a reformatted failpoint header instead of panicking - #158
Open
arpitjain099 wants to merge 1 commit into
Open
Report a reformatted failpoint header instead of panicking#158arpitjain099 wants to merge 1 commit into
arpitjain099 wants to merge 1 commit into
Conversation
ToComments detects a generated header by looking for ", __fpErr := __fp_" on
an "if" line, then reads the type out of the same line with
strings.Split(strings.Split(l, ".("), 1)[0]
The generator writes that header as one line, but gofmt splits it, which
leaves the type assertion on a later line. The header match still fires on
the "if" line, so the split returns one element and indexing it panics.
Reproduced with three ordinary commands and no hand-edited file:
gofail enable .
gofmt -w svc.go # or any editor format-on-save
gofail disable .
panic: runtime error: index out of range [1] with length 1
go.etcd.io/gofail/code.ToComments code/rewrite.go:115
main.xfrmFile gofail.go:62
main.main gofail.go:177
The crash also leaves a zero-byte svc.go.tmp behind, and every later enable
or disable on that tree then fails with "open ...svc.go.tmp: file exists"
until it is deleted by hand.
Return an error naming the failpoint instead. gofail.go already reports the
error and stops, so the source file is left intact and no temp file is
orphaned. Teaching ToComments to read the reformatted shape would be a
larger change; this only stops the panic.
Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: arpitjain099 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
AruneshDwivedi
added a commit
to AruneshDwivedi/gofail
that referenced
this pull request
Aug 29, 2026
When gofmt reformats a failpoint header across multiple lines, the type assertion lands on a later line than the 'if'. ToComments would panic with index out of range when trying to extract the type from the header line. Return a descriptive error instead, preventing the crash and the wedged .tmp file that leaves the tool unusable until manually cleaned up. Fixes etcd-io#158 Signed-off-by: Arunesh Dwivedi <arunesh.devops@gmail.com>
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.
ToCommentsspots a generated header by looking for, __fpErr := __fp_on anifline, then reads the failpoint's type out of that same line:The generator writes that header as a single line, so this normally works. gofmt splits it across several lines, which leaves the type assertion on a later line than the
if. The header match still fires on theifline,strings.Split(l, ".(")returns one element, and indexing it panics.Three ordinary commands reproduce it, no hand-edited file:
There is a second effect worth knowing about. The crash leaves a zero-byte
svc.go.tmpbehind, and after that everyenableanddisableon the tree fails:so the tool stays wedged until someone deletes the temp file by hand.
gofail.go:174walks every.gofile under the given paths, so one reformatted file aborts the whole run.With the change that same input reports itself and leaves the tree alone:
No panic, no orphaned
.tmp, and a normalenablethendisableround trip still restores the original source byte for byte.Deliberately scoped: teaching
ToCommentsto parse the reformatted multi-line shape would be a bigger change and a separate discussion. This only stops the panic and the wedged temp file. The new test feeds the gofmt'd header straight toToCommentsand asserts an error; it panics without the change.