Merge pull request #22 from MikeIsAStar/prevent-invalid-packets-from-stopping-server

NATNEG / SB: Prevent invalid packets from stopping the server
This commit is contained in:
Sketch
2023-12-08 21:39:53 -05:00
committed by GitHub
3 changed files with 83 additions and 17 deletions

View File

@@ -2,6 +2,7 @@ package common
import (
"bytes"
"errors"
"math/rand"
)
@@ -25,9 +26,14 @@ func RandomHexString(n int) string {
return string(b)
}
func GetString(buf []byte) string {
func GetString(buf []byte) (string, error) {
nullTerminator := bytes.IndexByte(buf, 0)
return string(buf[:nullTerminator])
if nullTerminator == -1 {
return "", errors.New("buf is not null-terminated")
}
return string(buf[:nullTerminator]), nil
}
// IsUppercaseAlphanumeric checks if the given string is composed exclusively of uppercase alphanumeric characters.