Skip to content

Commit

Permalink
json to input: retry if decompress too large
Browse files Browse the repository at this point in the history
  • Loading branch information
tsahee committed Jul 24, 2024
1 parent 9ea15b9 commit f96aafd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
10 changes: 9 additions & 1 deletion arbcompress/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ package arbcompress
#include "arbitrator.h"
*/
import "C"
import "fmt"
import (
"errors"
"fmt"
)

type u8 = C.uint8_t
type u32 = C.uint32_t
Expand Down Expand Up @@ -44,6 +47,8 @@ func Compress(input []byte, level uint32, dictionary Dictionary) ([]byte, error)
return output, nil
}

var ErrOutputWontFit = errors.New("output won't fit in maxsize")

func Decompress(input []byte, maxSize int) ([]byte, error) {
return DecompressWithDictionary(input, maxSize, EmptyDictionary)
}
Expand All @@ -54,6 +59,9 @@ func DecompressWithDictionary(input []byte, maxSize int, dictionary Dictionary)
inbuf := sliceToBuffer(input)

status := C.brotli_decompress(inbuf, outbuf, C.Dictionary(dictionary))
if status == C.BrotliStatus_NeedsMoreOutput {
return nil, ErrOutputWontFit
}
if status != C.BrotliStatus_Success {
return nil, fmt.Errorf("failed decompression: %d", status)
}
Expand Down
19 changes: 16 additions & 3 deletions validator/server_api/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package server_api
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -152,9 +153,21 @@ func ValidationInputFromJson(entry *InputJSON) (*validator.ValidationInput, erro
if err != nil {
return nil, err
}
uncompressed, err := arbcompress.Decompress(decoded, 30000000)
if err != nil {
return nil, err
maxSize := 2_000_000
var uncompressed []byte
for {
uncompressed, err = arbcompress.Decompress(decoded, maxSize)
if errors.Is(err, arbcompress.ErrOutputWontFit) {
if maxSize >= 128_000_000 {
return nil, errors.New("failed decompression: too large")
}
maxSize = maxSize * 4
continue
}
if err != nil {
return nil, err
}
break
}
archWasms[moduleHash] = uncompressed
}
Expand Down

0 comments on commit f96aafd

Please sign in to comment.