Skip to content

Commit

Permalink
Check compressor options strictly during compressor initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
CAFxX authored Jan 19, 2022
1 parent 4642d33 commit d267a7c
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 12 deletions.
8 changes: 7 additions & 1 deletion contrib/andybalholm/brotli/brotli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"sync"

"github.com/CAFxX/httpcompression/contrib/internal/utils"
"github.com/andybalholm/brotli"
)

Expand All @@ -26,7 +27,12 @@ func New(opts Options) (c *compressor, err error) {
c, err = nil, fmt.Errorf("panic: %v", r)
}
}()
_ = brotli.NewWriterOptions(nil, opts)

tw := brotli.NewWriterOptions(io.Discard, opts)
if err := utils.CheckWriter(tw); err != nil {
return nil, fmt.Errorf("brotli: writer initialization: %w", err)
}

c = &compressor{opts: opts}
return c, nil
}
Expand Down
10 changes: 3 additions & 7 deletions contrib/google/cbrotli/brotli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"runtime"

"github.com/CAFxX/httpcompression/contrib/internal/utils"
"github.com/google/brotli/go/cbrotli"
)

Expand All @@ -18,13 +19,8 @@ type compressor struct {

func New(opts cbrotli.WriterOptions) (c *compressor, err error) {
tw := cbrotli.NewWriter(io.Discard, opts)
n, err := tw.Write([]byte("test"))
if n != 4 || err != nil {
return nil, fmt.Errorf("cbrotli: writer initialization (write): %d, %w", n, err)
}
err = tw.Close()
if err != nil {
return nil, fmt.Errorf("cbrotli: writer initialization (close): %w", err)
if err := utils.CheckWriter(tw); err != nil {
return nil, fmt.Errorf("cbrotli: writer initialization: %w", err)
}

c = &compressor{opts: opts}
Expand Down
24 changes: 24 additions & 0 deletions contrib/internal/utils/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import (
"fmt"
"io"
)

func CheckWriter(w io.Writer) error {
const data = "test"
n, err := w.Write([]byte(data))
if err != nil {
return fmt.Errorf("write: %w", err)
}
if n != len(data) {
return fmt.Errorf("write: short write: %d", n)
}
if w, ok := w.(io.Closer); ok {
err := w.Close()
if err != nil {
return fmt.Errorf("close: %w", err)
}
}
return nil
}
14 changes: 13 additions & 1 deletion contrib/klauspost/gzip/gzip.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package gzip

import (
"fmt"
"io"
"sync"

"github.com/CAFxX/httpcompression/contrib/internal/utils"
"github.com/klauspost/compress/gzip"
)

Expand All @@ -22,10 +24,20 @@ type Options struct {
}

func New(opts Options) (c *compressor, err error) {
_, err = gzip.NewWriterLevel(nil, opts.Level)
defer func() {
if r := recover(); r != nil {
c, err = nil, fmt.Errorf("panic: %v", r)
}
}()

tw, err := gzip.NewWriterLevel(io.Discard, opts.Level)
if err != nil {
return nil, err
}
if err := utils.CheckWriter(tw); err != nil {
return nil, fmt.Errorf("gzip: writer initialization: %w", err)
}

c = &compressor{opts: opts}
return c, nil
}
Expand Down
16 changes: 14 additions & 2 deletions contrib/klauspost/pgzip/pgzip.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package pgzip

import (
"fmt"
"io"
"sync"

"github.com/CAFxX/httpcompression/contrib/internal/utils"
"github.com/klauspost/pgzip"
)

Expand All @@ -24,14 +26,24 @@ type Options struct {
}

func New(opts Options) (c *compressor, err error) {
gw, err := pgzip.NewWriterLevel(nil, opts.Level)
defer func() {
if r := recover(); r != nil {
c, err = nil, fmt.Errorf("panic: %v", r)
}
}()

tw, err := pgzip.NewWriterLevel(io.Discard, opts.Level)
if err != nil {
return nil, err
}
err = gw.SetConcurrency(opts.BlockSize, opts.Blocks)
err = tw.SetConcurrency(opts.BlockSize, opts.Blocks)
if err != nil {
return nil, err
}
if err := utils.CheckWriter(tw); err != nil {
return nil, fmt.Errorf("pgzip: writer initialization: %w", err)
}

c = &compressor{opts: opts}
return c, nil
}
Expand Down
15 changes: 14 additions & 1 deletion contrib/klauspost/zstd/zstd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package zstd

import (
"fmt"
"io"
"sync"

"github.com/CAFxX/httpcompression/contrib/internal/utils"
"github.com/klauspost/compress/zstd"
)

Expand All @@ -18,11 +20,22 @@ type compressor struct {
}

func New(opts ...zstd.EOption) (c *compressor, err error) {
defer func() {
if r := recover(); r != nil {
c, err = nil, fmt.Errorf("panic: %v", r)
}
}()

opts = append([]zstd.EOption(nil), opts...)
_, err = zstd.NewWriter(nil, opts...)

tw, err := zstd.NewWriter(io.Discard, opts...)
if err != nil {
return nil, err
}
if err := utils.CheckWriter(tw); err != nil {
return nil, fmt.Errorf("zstd: writer initialization: %w", err)
}

c = &compressor{opts: opts}
return c, nil
}
Expand Down
13 changes: 13 additions & 0 deletions contrib/valyala/gozstd/zstd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package gozstd

import (
"fmt"
"io"
"sync"

"github.com/CAFxX/httpcompression/contrib/internal/utils"
"github.com/valyala/gozstd"
)

Expand All @@ -17,6 +19,17 @@ type compressor struct {
}

func New(opts gozstd.WriterParams) (c *compressor, err error) {
defer func() {
if r := recover(); r != nil {
c, err = nil, fmt.Errorf("panic: %v", r)
}
}()

tw := gozstd.NewWriterParams(io.Discard, &opts)
if err := utils.CheckWriter(tw); err != nil {
return nil, fmt.Errorf("gozstd: writer initialization: %w", err)
}

c = &compressor{opts: opts}
return c, nil
}
Expand Down
1 change: 1 addition & 0 deletions contrib/valyala/gozstd/zstd_race_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build race
// +build race

package gozstd_test
Expand Down

0 comments on commit d267a7c

Please sign in to comment.