Skip to content

Error handling for oversized UDP payloads #4

@beka-birhanu

Description

@beka-birhanu

Description:
Currently, the for loop handling UDP payloads does not explicitly check if the payload size exceeds the defined buffer size (s.readBufferSize). This could lead to unintended behavior, as oversized payloads are truncated without an explicit error or warning.

Proposed Improvement:
Modify the code to allocate a buffer with an additional byte (s.readBufferSize + 1) to detect oversized payloads. If a payload exceeds s.readBufferSize, log an error (ErrMaximumPayloadSizeLimit) and discard the payload to prevent processing invalid data.

Current Code:

for {
	select {
	case <-s.stop:
		return
	default:
		buf := make([]byte, s.readBufferSize)
		n, addr, err := s.conn.ReadFromUDP(buf)
		if err != nil {
			if errors.Is(err, net.ErrClosed) {
				continue
			}
			s.logger.Printf("error while reading from udp: %s", err)
			continue
		}
		s.rawRecords <- rawRecord{
			payload: buf[0:n],
			addr:    addr,
		}
	}
}

Suggested Code:

for {
...
		buf := make([]byte, s.readBufferSize + 1) // Allocate extra space to detect oversized payloads.
		n, addr, err := s.conn.ReadFromUDP(buf)
...
		if n > s.readBufferSize {
			s.logger.Println(ErrMaximumPayloadSizeLimit)
			continue
		}
...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions