gpt4 book ai didi

go - 为什么会出现空字节?即使在 "sanitizing"之后流

转载 作者:行者123 更新时间:2023-12-01 19:26:31 26 4
gpt4 key购买 nike

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

{"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"}

我基本上从 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)
}

我尝试了多种方法来尝试删除空字节,我假设它们是来自 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
}

我什至尝试删除这里看到的\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
}

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

最佳答案

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

很难说为什么会出现空字节。但是流读取器不丢弃空值的问题可能是因为它们缺少自己的缓冲区。这是一个带有自己的缓冲区( 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
}

关于go - 为什么会出现空字节?即使在 "sanitizing"之后流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62207934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com