Skip to content

Commit

Permalink
add 32-bit tests; return errors in case of string-to-int overflows (#276
Browse files Browse the repository at this point in the history
)
  • Loading branch information
aler9 committed May 8, 2023
1 parent 3d54961 commit a54a594
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 55 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ jobs:
go-version: "1.20"

- run: make test-highlevel-nodocker

test32:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3

- run: make test32
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ help:
@echo " mod-tidy run go mod tidy"
@echo " format format source files"
@echo " test run tests"
@echo " test32 run tests on a 32-bit system"
@echo " test-highlevel run high-level tests"
@echo " lint run linter"
@echo " bench run benchmarks"
Expand Down
2 changes: 1 addition & 1 deletion internal/highleveltests/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (c *container) wait() int {
exec.Command("docker", "wait", "gortsplib-test-"+c.name).Run()
out, _ := exec.Command("docker", "inspect", "gortsplib-test-"+c.name,
"--format={{.State.ExitCode}}").Output()
code, _ := strconv.ParseInt(string(out[:len(out)-1]), 10, 64)
code, _ := strconv.ParseInt(string(out[:len(out)-1]), 10, 32)
return int(code)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/base/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ func (res *Response) Unmarshal(br *bufio.Reader) error {
}
statusCodeStr := string(byts[:len(byts)-1])

statusCode64, err := strconv.ParseInt(statusCodeStr, 10, 32)
tmp, err := strconv.ParseUint(statusCodeStr, 10, 31)
if err != nil {
return fmt.Errorf("unable to parse status code")
}
res.StatusCode = StatusCode(statusCode64)
res.StatusCode = StatusCode(tmp)

byts, err = readBytesLimited(br, '\r', 255)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/formats/av1.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (f *AV1) unmarshal(payloadType uint8, clock string, codec string, rtpmap st
for key, val := range fmtp {
switch key {
case "level-idx":
n, err := strconv.ParseUint(val, 10, 64)
n, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid level-idx: %v", val)
}
Expand All @@ -33,7 +33,7 @@ func (f *AV1) unmarshal(payloadType uint8, clock string, codec string, rtpmap st
f.LevelIdx = &v2

case "profile":
n, err := strconv.ParseUint(val, 10, 64)
n, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid profile: %v", val)
}
Expand All @@ -42,7 +42,7 @@ func (f *AV1) unmarshal(payloadType uint8, clock string, codec string, rtpmap st
f.Profile = &v2

case "tier":
n, err := strconv.ParseUint(val, 10, 64)
n, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid tier: %v", val)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func findClockRate(payloadType uint8, rtpMap string) (int, error) {
return 0, fmt.Errorf("invalid rtpmap (%v)", rtpMap)
}

v, err := strconv.ParseInt(tmp[1], 10, 64)
v, err := strconv.ParseUint(tmp[1], 10, 31)
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/h264.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (f *H264) unmarshal(payloadType uint8, clock string, codec string, rtpmap s
}

case "packetization-mode":
tmp, err := strconv.ParseInt(val, 10, 64)
tmp, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid packetization-mode (%v)", val)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/h265.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (f *H265) unmarshal(payloadType uint8, clock string, codec string, rtpmap s
}

case "sprop-max-don-diff":
tmp, err := strconv.ParseInt(val, 10, 64)
tmp, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid sprop-max-don-diff (%v)", fmtp)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/formats/lpcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func (f *LPCM) unmarshal(payloadType uint8, clock string, codec string, rtpmap s

tmp := strings.SplitN(clock, "/", 2)

tmp1, err := strconv.ParseInt(tmp[0], 10, 64)
tmp1, err := strconv.ParseUint(tmp[0], 10, 31)
if err != nil {
return err
}
f.SampleRate = int(tmp1)

if len(tmp) >= 2 {
tmp1, err := strconv.ParseInt(tmp[1], 10, 64)
tmp1, err := strconv.ParseUint(tmp[1], 10, 31)
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/formats/mpeg4_audio_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (f *MPEG4AudioGeneric) unmarshal(
}

case "profile-level-id":
tmp, err := strconv.ParseInt(val, 10, 64)
tmp, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid profile-level-id: %v", val)
}
Expand All @@ -65,22 +65,22 @@ func (f *MPEG4AudioGeneric) unmarshal(
}

case "sizelength":
n, err := strconv.ParseUint(val, 10, 64)
if err != nil {
n, err := strconv.ParseUint(val, 10, 31)
if err != nil || n > 100 {
return fmt.Errorf("invalid AAC SizeLength: %v", val)
}
f.SizeLength = int(n)

case "indexlength":
n, err := strconv.ParseUint(val, 10, 64)
if err != nil {
n, err := strconv.ParseUint(val, 10, 31)
if err != nil || n > 100 {
return fmt.Errorf("invalid AAC IndexLength: %v", val)
}
f.IndexLength = int(n)

case "indexdeltalength":
n, err := strconv.ParseUint(val, 10, 64)
if err != nil {
n, err := strconv.ParseUint(val, 10, 31)
if err != nil || n > 100 {
return fmt.Errorf("invalid AAC IndexDeltaLength: %v", val)
}
f.IndexDeltaLength = int(n)
Expand Down
18 changes: 4 additions & 14 deletions pkg/formats/mpeg4_audio_latm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ func (f *MPEG4AudioLATM) unmarshal(
for key, val := range fmtp {
switch key {
case "profile-level-id":
tmp, err := strconv.ParseInt(val, 10, 64)
tmp, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid profile-level-id: %v", val)
}

f.ProfileLevelID = int(tmp)

case "bitrate":
tmp, err := strconv.ParseInt(val, 10, 64)
tmp, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid bitrate: %v", val)
}
Expand All @@ -47,12 +47,7 @@ func (f *MPEG4AudioLATM) unmarshal(
f.Bitrate = &v

case "cpresent":
tmp, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return fmt.Errorf("invalid cpresent: %v", val)
}

v := (tmp == 1)
v := (val == "1")
f.CPresent = &v

case "config":
Expand All @@ -68,12 +63,7 @@ func (f *MPEG4AudioLATM) unmarshal(
}

case "sbr-enabled":
tmp, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return fmt.Errorf("invalid SBR-enabled: %v", val)
}

v := (tmp == 1)
v := (val == "1")
f.SBREnabled = &v
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/mpeg4_video_es.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (f *MPEG4VideoES) unmarshal(
for key, val := range fmtp {
switch key {
case "profile-level-id":
tmp, err := strconv.ParseInt(val, 10, 64)
tmp, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid profile-level-id: %v", val)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/formats/opus.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func (f *Opus) unmarshal(payloadType uint8, clock string, codec string, rtpmap s
return fmt.Errorf("invalid clock (%v)", clock)
}

sampleRate, err := strconv.ParseInt(tmp[0], 10, 64)
sampleRate, err := strconv.ParseUint(tmp[0], 10, 31)
if err != nil || sampleRate != 48000 {
return fmt.Errorf("invalid sample rate: %d", sampleRate)
}

channelCount, err := strconv.ParseInt(tmp[1], 10, 64)
channelCount, err := strconv.ParseUint(tmp[1], 10, 31)
if err != nil || channelCount != 2 {
return fmt.Errorf("invalid channel count: %d", channelCount)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/formats/vorbis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func (f *Vorbis) unmarshal(payloadType uint8, clock string, codec string, rtpmap
return fmt.Errorf("invalid clock (%v)", clock)
}

sampleRate, err := strconv.ParseInt(tmp[0], 10, 64)
sampleRate, err := strconv.ParseUint(tmp[0], 10, 31)
if err != nil {
return err
}
f.SampleRate = int(sampleRate)

channelCount, err := strconv.ParseInt(tmp[1], 10, 64)
channelCount, err := strconv.ParseUint(tmp[1], 10, 31)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/formats/vp8.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (f *VP8) unmarshal(payloadType uint8, clock string, codec string, rtpmap st
for key, val := range fmtp {
switch key {
case "max-fr":
n, err := strconv.ParseUint(val, 10, 64)
n, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid max-fr: %v", val)
}
Expand All @@ -32,7 +32,7 @@ func (f *VP8) unmarshal(payloadType uint8, clock string, codec string, rtpmap st
f.MaxFR = &v2

case "max-fs":
n, err := strconv.ParseUint(val, 10, 64)
n, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid max-fs: %v", val)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/formats/vp9.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (f *VP9) unmarshal(payloadType uint8, clock string, codec string, rtpmap st
for key, val := range fmtp {
switch key {
case "max-fr":
n, err := strconv.ParseUint(val, 10, 64)
n, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid max-fr: %v", val)
}
Expand All @@ -33,7 +33,7 @@ func (f *VP9) unmarshal(payloadType uint8, clock string, codec string, rtpmap st
f.MaxFR = &v2

case "max-fs":
n, err := strconv.ParseUint(val, 10, 64)
n, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid max-fs: %v", val)
}
Expand All @@ -42,7 +42,7 @@ func (f *VP9) unmarshal(payloadType uint8, clock string, codec string, rtpmap st
f.MaxFS = &v2

case "profile-id":
n, err := strconv.ParseUint(val, 10, 64)
n, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid profile-id: %v", val)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/headers/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (h *Session) Unmarshal(v base.HeaderValue) error {

for k, v := range kvs {
if k == "timeout" {
iv, err := strconv.ParseUint(v, 10, 64)
iv, err := strconv.ParseUint(v, 10, 32)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/headers/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
func parsePorts(val string) (*[2]int, error) {
ports := strings.Split(val, "-")
if len(ports) == 2 {
port1, err := strconv.ParseInt(ports[0], 10, 64)
port1, err := strconv.ParseUint(ports[0], 10, 31)
if err != nil {
return &[2]int{0, 0}, fmt.Errorf("invalid ports (%v)", val)
}

port2, err := strconv.ParseInt(ports[1], 10, 64)
port2, err := strconv.ParseUint(ports[1], 10, 31)
if err != nil {
return &[2]int{0, 0}, fmt.Errorf("invalid ports (%v)", val)
}
Expand All @@ -27,7 +27,7 @@ func parsePorts(val string) (*[2]int, error) {
}

if len(ports) == 1 {
port1, err := strconv.ParseInt(ports[0], 10, 64)
port1, err := strconv.ParseUint(ports[0], 10, 31)
if err != nil {
return &[2]int{0, 0}, fmt.Errorf("invalid ports (%v)", val)
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func (h *Transport) Unmarshal(v base.HeaderValue) error {
h.InterleavedIDs = ports

case "ttl":
tmp, err := strconv.ParseUint(v, 10, 64)
tmp, err := strconv.ParseUint(v, 10, 32)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/media/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func getFormatAttribute(attributes []psdp.Attribute, payloadType uint8, key stri
if attr.Key == key {
v := strings.TrimSpace(attr.Value)
if parts := strings.SplitN(v, " ", 2); len(parts) == 2 {
if tmp, err := strconv.ParseInt(parts[0], 10, 8); err == nil && uint8(tmp) == payloadType {
if tmp, err := strconv.ParseUint(parts[0], 10, 8); err == nil && uint8(tmp) == payloadType {
return parts[1]
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func (m *Media) unmarshal(md *psdp.MediaDescription) error {
}
}

tmp, err := strconv.ParseInt(payloadType, 10, 8)
tmp, err := strconv.ParseUint(payloadType, 10, 8)
if err != nil {
return err
}
Expand Down
21 changes: 17 additions & 4 deletions scripts/test.mk
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
LBITS := $(shell getconf LONG_BIT)
ifeq ($(LBITS),64)
RACE=-race
endif

test-examples:
go build -o /dev/null ./examples/...

test-pkg:
go test -v -race -coverprofile=coverage-pkg.txt ./pkg/...
go test -v $(RACE) -coverprofile=coverage-pkg.txt ./pkg/...

test-root:
go test -v -race -coverprofile=coverage-root.txt .
go test -v $(RACE) -coverprofile=coverage-root.txt .

test-nodocker: test-examples test-pkg test-root

define DOCKERFILE_TEST
FROM $(BASE_IMAGE)
ARG ARCH
FROM $$ARCH/$(BASE_IMAGE)
RUN apk add --no-cache make git gcc musl-dev pkgconfig ffmpeg-dev
WORKDIR /s
COPY go.mod go.sum ./
Expand All @@ -20,8 +26,15 @@ endef
export DOCKERFILE_TEST

test:
echo "$$DOCKERFILE_TEST" | docker build -q . -f - -t temp
echo "$$DOCKERFILE_TEST" | docker build -q . -f - -t temp --build-arg ARCH=amd64
docker run --rm -it \
--name temp \
temp \
make test-nodocker

test32:
echo "$$DOCKERFILE_TEST" | docker build -q . -f - -t temp --build-arg ARCH=i386
docker run --rm \
--name temp \
temp \
make test-nodocker
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func extractPort(address string) (int, error) {
return 0, err
}

tmp2, err := strconv.ParseInt(tmp, 10, 64)
tmp2, err := strconv.ParseUint(tmp, 10, 16)
if err != nil {
return 0, err
}
Expand Down
Loading

0 comments on commit a54a594

Please sign in to comment.