为什么会出现空字节?即使在“消毒”流之后

ret*_*oot 5 null streamreader go

我一直在想为什么某些字符串中会出现空字节。下面举例。

{"gender":"fema\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000le"}
Run Code Online (Sandbox Code Playgroud)

我基本上从 HTTP 请求中包装了一个 io.Reader 并解码为一个结构体。见下文

func bodyToStruct(res *http.Request, v gojay.UnmarshalerJSONObject) error {
    var reader io.ReadCloser
    var err error
    switch res.Header.Get("Content-Encoding") {
    case "gzip":
        reader, err = pool.Gzip.GetReader(res.Body)
        if err != nil {
            return err
        }
        defer pool.Gzip.PutReader(reader)
    case "deflate":
        reader = flate.NewReader(res.Body)
        defer reader.Close()
    default:
        reader = res.Body
    }

    decoder := gojay.BorrowDecoder(streams.NewNullByteRemoverStream(reader)) //wrapped in NewNullByteRemoverStream
    defer decoder.Release()

    return decoder.DecodeObject(v)
}
Run Code Online (Sandbox Code Playgroud)

我尝试了多种方法来尝试删除空字节,我假设它们是来自 Android 客户端的请求。

从早期堆栈线程的帮助中,我能够将以下实现部署到生产中,以尝试删除空字节。

package streams

import (
    "io"
)

// NullByte is a stream wrapper that should remove null bytes from the byte stream as well as reject any and all control bytes
type NullByte struct {
    Reader io.Reader
}

// NewNullByteRemoverStream creates a new NullByte reader which passes passes the parent stream through and remove null bytes
func NewNullByteRemoverStream(reader io.ReadCloser) *NullByte {
    return &NullByte{
        Reader: reader,
    }
}

func (s *NullByte) Read(p []byte) (n int, err error) {
    n, err = s.Reader.Read(p)
    var nn int
    for i := 0; i < n; i++ {
        if p[i] >= 32 && p[i] <= 126 {
            p[nn] = p[i]
            nn++
        } 
    }
    return nn, err
}
Run Code Online (Sandbox Code Playgroud)

我什至尝试删除这里看到的 \u0000 的字符串文字(也在生产中测试了一点)

package streams

import (
    "io"
)

const _unicodeCodePointLength = 6

var (
    _sControlByte   = byte(92)
    _sNullByteBlock = []byte{92, 117, 48, 48, 48, 48}
)

// NullByte is a stream wrapper that should remove null bytes from the byte stream as well as reject any and all control bytes
type NullByte struct {
    Reader io.Reader
    state  int
}

// NewNullByteRemoverStream creates a new NullByte reader which passes passes the parent stream through and remove null bytes
// as well as \u0000 as a string representation
func NewNullByteRemoverStream(reader io.ReadCloser) *NullByte {
    return &NullByte{
        Reader: reader,
    }
}

func (s *NullByte) Read(p []byte) (n int, err error) {
    n, err = s.Reader.Read(p)

    var nn, i int
    for i < n {
        if p[i] == _sControlByte {
            s.state = 0
        }

        if p[i] == _sControlByte || s.state > 0 {
            var broke bool
            if p[i] == _sControlByte {
                stop := 0
                for j := i; j < n; j++ {
                    if stop == _unicodeCodePointLength {
                        break
                    }
                    if p[j] != _sNullByteBlock[stop] {
                        broke = true
                        break
                    }
                    stop++
                }

                if broke {
                    p[nn] = p[i]
                    i++
                    nn++
                    s.state = 0
                    continue
                }
            }

            if s.state < _unicodeCodePointLength {
                i++
                s.state++
                continue
            }
        }

        if p[i] != 0 {
            p[nn] = p[i]
            nn++
        }
        i++
    }
    return nn, err
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这两个版本都不能解决这个问题。我可以在生产日志中看到 \u0000 出现在日志的百分比中。我认为通过将 io.Reader 响应包装在上面的消毒剂中,问题就会停止。我可以从测试中看到空字节 0 和 \u0000 被删除......但问题在生产中仍然存在。我怀疑问题仍然出在客户的要求上。这是因为该问题仅在特定客户端版本中出现。其他应用程序版本和平台不会触发空字节出现在字符串中,并且所有客户端都与相同的中央服务器通信。我没有想法了。我不知道为什么上面的消毒剂在 JSON 解码器将数据加载到支柱之前不删除空字节。有没有人有任何见解?

Mar*_*ark 1

编辑:这是不正确的,即使它可能巧合地解决了问题。无论是否使用缓冲区,都应删除空字节。

很难说为什么会出现空字节。但是流读取器不丢弃空值的问题可能是因为它们缺少自己的缓冲区。这是一个带有自己的缓冲区( playground)的空丢弃读取器的示例:

type DropReader struct {
    buf    []byte
    reader io.Reader
    nulls  int
    reads  int
}

func (dr *DropReader) Read(data []byte) (int, error) {

    n, err := dr.reader.Read(dr.buf)

    dr.reads++

    j := 0
    for i := 0; i < n; i++ {

        c := dr.buf[i]

        if c == 0 {
            dr.nulls++
            continue
        }

        data[j] = c
        j++

    }

    return j, err
}
Run Code Online (Sandbox Code Playgroud)