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

[Merged by Bors] - Fix nil pointer panic on error #5035

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ Support for old certificate sync protocol is dropped. This update is incompatibl
### Features

### Improvements

* [#4998](https://github.com/spacemeshos/go-spacemesh/pull/4998) First phase of state size reduction.
Ephemeral data are deleted and state compacted at the time of upgrade. In steady-state, data is pruned periodically.
* [#5021](https://github.com/spacemeshos/go-spacemesh/pull/5021) Drop support for old certificate sync protocol.
* [#5024](https://github.com/spacemeshos/go-spacemesh/pull/5024) Active set will be saved in state separately from ballots.
* [#5035](https://github.com/spacemeshos/go-spacemesh/pull/5035) Fix possible nil pointer panic when node fails to persist nipost builder state.

## v1.1.5

Expand Down
26 changes: 13 additions & 13 deletions activation/nipost_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"encoding/binary"
"errors"
"fmt"
"hash/crc64"
"io"
Expand All @@ -22,30 +23,31 @@
)

func write(path string, data []byte) error {
tmp, err := os.Create(fmt.Sprintf("%s.tmp", path))
tmpName := fmt.Sprintf("%s.tmp", path)
tmp, err := os.Create(tmpName)
if err != nil {
return fmt.Errorf("create temporary file %s: %w", tmp.Name(), err)
return fmt.Errorf("create temporary file %s: %w", tmpName, err)

Check warning on line 29 in activation/nipost_state.go

View check run for this annotation

Codecov / codecov/patch

activation/nipost_state.go#L29

Added line #L29 was not covered by tests
}

checksum := crc64.New(crc64.MakeTable(crc64.ISO))
w := io.MultiWriter(tmp, checksum)
if _, err = w.Write(data); err != nil {
_ = tmp.Close()
if _, err := w.Write(data); err != nil {
err = errors.Join(err, tmp.Close())

Check warning on line 35 in activation/nipost_state.go

View check run for this annotation

Codecov / codecov/patch

activation/nipost_state.go#L35

Added line #L35 was not covered by tests
return fmt.Errorf("write data %v: %w", tmp.Name(), err)
}

crc := make([]byte, crc64.Size)
binary.BigEndian.PutUint64(crc, checksum.Sum64())
if _, err = tmp.Write(crc); err != nil {
_ = tmp.Close()
if _, err := tmp.Write(crc); err != nil {
err = errors.Join(err, tmp.Close())

Check warning on line 42 in activation/nipost_state.go

View check run for this annotation

Codecov / codecov/patch

activation/nipost_state.go#L42

Added line #L42 was not covered by tests
return fmt.Errorf("write checksum %s: %w", tmp.Name(), err)
}

if err = tmp.Close(); err != nil {
if err := tmp.Close(); err != nil {
return fmt.Errorf("failed to close tmp file %s: %w", tmp.Name(), err)
}

if err = atomic.ReplaceFile(tmp.Name(), path); err != nil {
if err := atomic.ReplaceFile(tmp.Name(), path); err != nil {
return fmt.Errorf("save file from %s, %s: %w", tmp.Name(), path, err)
}

Expand All @@ -57,7 +59,6 @@
if err != nil {
return nil, fmt.Errorf("open file %s: %w", path, err)
}

defer file.Close()

fInfo, err := file.Stat()
Expand All @@ -70,20 +71,19 @@

data := make([]byte, fInfo.Size()-crc64.Size)
checksum := crc64.New(crc64.MakeTable(crc64.ISO))
if _, err = io.TeeReader(file, checksum).Read(data); err != nil {
if _, err := io.TeeReader(file, checksum).Read(data); err != nil {
return nil, fmt.Errorf("read file %s: %w", path, err)
}

saved := make([]byte, crc64.Size)
if _, err = file.Read(saved); err != nil {
if _, err := file.Read(saved); err != nil {
return nil, fmt.Errorf("read checksum %s: %w", path, err)
}

savedChecksum := binary.BigEndian.Uint64(saved)

if savedChecksum != checksum.Sum64() {
return nil, fmt.Errorf(
"wrong checksum 0x%X, computed 0x%X", savedChecksum, checksum.Sum64())
return nil, fmt.Errorf("wrong checksum 0x%X, computed 0x%X", savedChecksum, checksum.Sum64())
}

return data, nil
Expand Down