mirror of
https://github.com/slackhq/nebula.git
synced 2026-08-15 07:17:00 +02:00
overlay/tio: guard Offload.Write against zero-length buffers
Write took &buf[0] before calling writeWithScratch, so the len==0 guard in the helper could never run -- a zero-length buffer panicked on the index instead of returning. Hoist the guard above the indexing and fold writeWithScratch into Write since it was the only caller and duplicated the iovec setup. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -276,22 +276,16 @@ func (r *Offload) decodeRead(pktLen int) error {
|
||||
}
|
||||
|
||||
func (r *Offload) Write(buf []byte) (int, error) {
|
||||
if len(buf) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
iovs := [2]unix.Iovec{
|
||||
{Base: &validVnetHdr[0]},
|
||||
{Base: &buf[0]},
|
||||
}
|
||||
iovs[0].SetLen(virtio.Size)
|
||||
iovs[1].SetLen(len(buf))
|
||||
return r.writeWithScratch(buf, &iovs)
|
||||
}
|
||||
|
||||
func (r *Offload) writeWithScratch(buf []byte, iovs *[2]unix.Iovec) (int, error) {
|
||||
if len(buf) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
iovs[1].Base = &buf[0]
|
||||
iovs[1].SetLen(len(buf))
|
||||
return r.rawWrite(unsafe.Slice(&iovs[0], len(iovs)))
|
||||
return r.rawWrite(unsafe.Slice(&iovs[0], 2))
|
||||
}
|
||||
|
||||
func (r *Offload) rawWrite(iovs []unix.Iovec) (int, error) {
|
||||
|
||||
@@ -854,3 +854,15 @@ func TestDecodeReadFitsMaxTSOAtDrainThreshold(t *testing.T) {
|
||||
t.Fatalf("got %d segments, want %d", gotSegs, wantSegs)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOffloadWriteZeroLength: a zero-length Write must be a no-op, not a
|
||||
// panic. The guard used to live below the &buf[0] that tripped on it.
|
||||
func TestOffloadWriteZeroLength(t *testing.T) {
|
||||
tf := &Offload{fd: -1} // any write reaching the fd would fail loudly
|
||||
for _, buf := range [][]byte{nil, {}} {
|
||||
n, err := tf.Write(buf)
|
||||
if n != 0 || err != nil {
|
||||
t.Errorf("Write(len=0) = (%d, %v), want (0, nil)", n, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user