NebulousLabs / Sia

Blockchain-based marketplace for file storage. Project has moved to GitLab: https://gitlab.com/NebulousLabs/Sia
https://sia.tech
MIT License
2.71k stars 442 forks source link

cannot read a persisted json object file with manual checksum #3173

Open wincss opened 5 years ago

wincss commented 5 years ago

I modified some content in miner.json and changed checksum to manual

"Miner Settings" "0.5.0" "manual" { ... content here ... }

siad treat it as a wrong checksum and ignore the file content.

https://github.com/NebulousLabs/Sia/blob/master/persist/json.go#L70

    // Determine whether the leading bytes contain a checksum. A proper checksum
    // will be 67 bytes (quote, 64 byte checksum, quote, newline). A manual
    // checksum will be the characters "manual\n" (9 characters). If neither
    // decode correctly, it is assumed that there is no checksum at all.
    var checksum crypto.Hash
    err = json.Unmarshal(remainingBytes[:67], &checksum)
    if err == nil && checksum == crypto.HashBytes(remainingBytes[68:]) {
        // Checksum is proper, and matches the data. Update the data portion to
        // exclude the checksum.
        remainingBytes = remainingBytes[68:]
    } else {
        // Cryptographic checksum failed, try interpreting a manual checksum.
        var manualChecksum string
        err := json.Unmarshal(remainingBytes[:8], &manualChecksum)    // Line 70
        if err == nil && manualChecksum == "manual" {
            // Manual checksum is proper. Update the remaining data to exclude
            // the manual checksum.
            remainingBytes = remainingBytes[9:]    // Line 74
        }
    }

In line 70, it try to json.Unmarshal with remainingBytes[:8] which is only 7 bytes. json.Unmarshal([]byte("\"manual"), &manualChecksum) is always return an error (unexpected end of JSON input), it should be changed to remainingBytes[:9], and also, the remainingBytes[9:] in line 74 should be changed to remainingBytes[10:].

Stack Trace or error message No

Expected Behavior treat "manual" as a correct checksum

How to reproduce it (as minimally and precisely as possible)

  1. run siad with miner module (siad -M gctwm)
  2. modify third line of /miner/miner.json to "manual"
  3. restart siad with miner module
  4. it takes very long time to load miner module because latestChangeID from this file is ignored by siad.

Environment