Skip to content

Commit

Permalink
Revive #19 (as the PR is closed
Browse files Browse the repository at this point in the history
due to the source repo being deleted by the author, I can only reproduce
the commits again)

If the code execution flow after the failpoint type conversion doesn't
jump to other parts of the code, the `__badType` will be executed,
causing irrelevant error messages to be printed.

For example, consider the following snippet
```go
// gofail: var SomeFuncString string
// log.Println("SomeFuncString", SomeFuncString)
```

It will be generated into the following snippet
```go
if vSomeFuncString, __fpErr := __fp_SomeFuncString.Acquire(); __fpErr == nil {
	SomeFuncString, __fpTypeOK := vSomeFuncString.(string)
	if !__fpTypeOK {
		goto __badTypeSomeFuncString
	}
	log.Println("SomeFuncString", SomeFuncString)
__badTypeSomeFuncString:
	__fp_SomeFuncString.BadType(vSomeFuncString, "string")
}
```

As you can see, because `log.println` doesn't jump to other parts of the
code, after printing the log, the code will continue to execute
`__badTypeSomeFuncString`, thus, printing the following message:
`"SomeFuncString" got value Hello_world of type "string" but expected type "string"`

The solution is to add a `__nomock` label, in the case of type
conversion succeeded, thus the `__badType` label won't be executed.

Reference:
- demo code https://github.com/henrybear327/gofail-perf-fix-demo/tree/issue/val_error

Signed-off-by: Chun-Hung Tseng <[email protected]>
  • Loading branch information
henrybear327 authored and Chun-Hung Tseng committed May 3, 2024
1 parent 0e62c1e commit f0353a7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions code/failpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func (fp *Failpoint) hdr(varname string) string {
}

func (fp *Failpoint) footer() string {
return "; __badType" + fp.name + ": " +
fp.Runtime() + ".BadType(v" + fp.name + ", \"" + fp.varType + "\"); };"
return "; goto __nomock" + fp.name + "; __badType" + fp.name + ": " +
fp.Runtime() + ".BadType(v" + fp.name + ", \"" + fp.varType + "\"); __nomock" + fp.name + ": };"
}

func (fp *Failpoint) flushSingle(dst io.Writer) error {
Expand Down
4 changes: 2 additions & 2 deletions code/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func ToComments(wdst io.Writer, rsrc io.Reader) ([]*Failpoint, error) {
unmatchedBraces += opening - closing
if unmatchedBraces == 0 {
// strip off badType footer
lTrim = strings.Split(lTrim, "; __badType")[0]
lTrim = strings.Split(lTrim, "; goto __nomock")[0]
}
s := ws + "//" + wsPrefix(l, ws)[1:] + lTrim + "\n"
dst.WriteString(s)
Expand All @@ -114,7 +114,7 @@ func ToComments(wdst io.Writer, rsrc io.Reader) ([]*Failpoint, error) {
n := strings.Split(strings.Split(l, "__fp_")[1], ".")[0]
t := strings.Split(strings.Split(l, ".(")[1], ")")[0]
dst.WriteString(ws + pfx + " var " + n + " " + t + "\n")
if !strings.Contains(l, "; __badType") {
if !strings.Contains(l, "; goto __nomock") {
// not single liner
unmatchedBraces = 1
}
Expand Down

0 comments on commit f0353a7

Please sign in to comment.