Skip to content
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

log: fix formatting of big.Int #22679

Merged
merged 3 commits into from
Apr 16, 2021
Merged

log: fix formatting of big.Int #22679

merged 3 commits into from
Apr 16, 2021

Conversation

fjl
Copy link
Contributor

@fjl fjl commented Apr 15, 2021

The implementation of formatLogfmtBigInt had two issues: it crashed when
the number was actually large enough to hit the big integer case, and
modified the big.Int while formatting it.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0xee55cb]

goroutine 1 [running]:
math/big.(*Int).QuoRem(0xc00030c340, 0xc00030c340, 0xc00000ee20, 0x0, 0x4bdbf7, 0x4)
	/usr/local/go/src/math/big/int.go:239 +0x4b
math/big.(*Int).DivMod(0xc00030c340, 0xc00030c340, 0xc00000ee20, 0x0, 0xc000098709, 0x4c7422)
	/usr/local/go/src/math/big/int.go:301 +0xdd
github.com/ethereum/go-ethereum/log.formatLogfmtBigInt(0xc00030c340, 0x100, 0xe)
	/usr/home/fjl/go-ethereum/log/format.go:449 +0x11c
github.com/ethereum/go-ethereum/log.formatLogfmtValue(0x4b4480, 0xc00030c340, 0x1, 0x0, 0x0)
	/usr/home/fjl/go-ethereum/log/format.go:346 +0x6e
github.com/ethereum/go-ethereum/log.logfmt(0xc0002e88a0, 0xc000214600, 0x8, 0x8, 0x20, 0x1)
	/usr/home/fjl/go-ethereum/log/format.go:169 +0xfe
github.com/ethereum/go-ethereum/log.TerminalFormat.func1(0xc0001fefc0, 0xc002fc7e58, 0x11183f1, 0xc000276b40)
	/usr/home/fjl/go-ethereum/log/format.go:143 +0x4ab
github.com/ethereum/go-ethereum/log.formatFunc.Format(0xc00032f600, 0xc0001fefc0, 0x32, 0x32, 0xc002fc7dc0)
	/usr/home/fjl/go-ethereum/log/format.go:68 +0x30
github.com/ethereum/go-ethereum/log.StreamHandler.func1(0xc0001fefc0, 0x10, 0x11204e0)
	/usr/home/fjl/go-ethereum/log/handler.go:43 +0x4f
github.com/ethereum/go-ethereum/log.funcHandler.Log(0xc000275b90, 0xc0001fefc0, 0xc002fc7ed0, 0x111ff20)
	/usr/home/fjl/go-ethereum/log/handler.go:31 +0x30
github.com/ethereum/go-ethereum/log.SyncHandler.func1(0xc0001fefc0, 0x0, 0x0)
	/usr/home/fjl/go-ethereum/log/handler.go:57 +0x82
github.com/ethereum/go-ethereum/log.funcHandler.Log(0xc0004da600, 0xc0001fefc0, 0x59d400, 0xc0000001a8)
	/usr/home/fjl/go-ethereum/log/handler.go:31 +0x30
github.com/ethereum/go-ethereum/log.LazyHandler.func1(0xc0001fefc0, 0xa, 0xc00008cc00)
	/usr/home/fjl/go-ethereum/log/handler.go:299 +0x337
github.com/ethereum/go-ethereum/log.funcHandler.Log(0xc0004da620, 0xc0001fefc0, 0xc002fc7fa0, 0xe12aa5)
	/usr/home/fjl/go-ethereum/log/handler.go:31 +0x30
github.com/ethereum/go-ethereum/log.(*GlogHandler).Log(0xc000265570, 0xc0001fefc0, 0xe0, 0x41da40)
	/usr/home/fjl/go-ethereum/log/handler_glog.go:202 +0x68b
github.com/ethereum/go-ethereum/log.(*swapHandler).Log(0xc000040680, 0xc0001fefc0, 0xb020d8, 0xb02138)
	/usr/home/fjl/go-ethereum/log/handler_go14.go:14 +0x82
github.com/ethereum/go-ethereum/log.(*logger).write(0x1b27880, 0x4e1b8f, 0x1f, 0x3, 0xc002fc8568, 0x8, 0x8, 0x2)
	/usr/home/fjl/go-ethereum/log/logger.go:134 +0x27d
github.com/ethereum/go-ethereum/log.Info(...)
	/usr/home/fjl/go-ethereum/log/root.go:44
github.com/ethereum/go-ethereum/light.(*LightChain).loadLastState(0xc000250600, 0x1b65860, 0xc0000b2010)
	/usr/home/fjl/go-ethereum/light/lightchain.go:169 +0x477

fjl added 2 commits April 15, 2021 23:53
The implementation of formatLogfmtBigInt had two issues: it crashed when
the number was actually large enough to hit the big integer case, and
modified the big.Int while formatting it.
@fjl fjl requested a review from karalabe April 15, 2021 22:02
@fjl fjl added this to the 1.10.3 milestone Apr 15, 2021
@karalabe
Copy link
Member

Huh, I may have modified the code later. I did try those paths.

log/format.go Outdated
text := n.String()
buf := make([]byte, 0, len(text)+len(text)/3)
comma := 0
for _, c := range text {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to construct it backwards, otherwise you don't know where the commas need to be

=== RUN   TestPrettyBigInt
    format_test.go:76: invalid output 111,222,333,444,555,678,999,00, want 11,122,233,344,455,567,899,900
    format_test.go:76: invalid output -111,222,333,444,555,678,999,00, want -11,122,233,344,455,567,899,900

s string
}{
{"111222333444555678999", "111,222,333,444,555,678,999"},
{"-111222333444555678999", "-111,222,333,444,555,678,999"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add

		{"11122233344455567899900", "11,122,233,344,455,567,899,900"},
		{"-11122233344455567899900", "-11,122,233,344,455,567,899,900"},

the test will fail

@karalabe karalabe merged commit fda93f6 into ethereum:master Apr 16, 2021
atif-konasl pushed a commit to frozeman/pandora-execution-engine that referenced this pull request Oct 15, 2021
* log: fix formatting of big.Int

The implementation of formatLogfmtBigInt had two issues: it crashed when
the number was actually large enough to hit the big integer case, and
modified the big.Int while formatting it.

* log: don't call FormatLogfmtInt64 for int16

* log: separate from decimals back, not front

Co-authored-by: Péter Szilágyi <peterke@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants