Fix potentially truncated int casts in $GENERATE code (#1212)

These were flagged by GitHub CodeQL code scanning as potential
vulnerabilities or issues. Fixing them is easy and they are incorrect.

Adding tests is less easy because int is 64-bits on most systems,
including those we test on, so we can't consistently provoke a failure
here.
This commit is contained in:
Tom Thorogood 2021-01-30 19:37:06 +10:30 committed by GitHub
parent 731b191cab
commit f9dc403cff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 14 deletions

View File

@ -75,10 +75,10 @@ func (zp *ZoneParser) generate(l lex) (RR, bool) {
r := &generateReader{ r := &generateReader{
s: s, s: s,
cur: int(start), cur: start,
start: int(start), start: start,
end: int(end), end: end,
step: int(step), step: step,
file: zp.file, file: zp.file,
lex: &l, lex: &l,
@ -94,10 +94,10 @@ type generateReader struct {
s string s string
si int si int
cur int cur int64
start int start int64
end int end int64
step int step int64
mod bytes.Buffer mod bytes.Buffer
@ -173,7 +173,7 @@ func (r *generateReader) ReadByte() (byte, error) {
return '$', nil return '$', nil
} }
var offset int var offset int64
// Search for { and } // Search for { and }
if r.s[si+1] == '{' { if r.s[si+1] == '{' {
@ -188,7 +188,7 @@ func (r *generateReader) ReadByte() (byte, error) {
if errMsg != "" { if errMsg != "" {
return 0, r.parseError(errMsg, si+3+sep) return 0, r.parseError(errMsg, si+3+sep)
} }
if r.start+offset < 0 || int64(r.end) + int64(offset) > 1<<31-1 { if r.start+offset < 0 || r.end+offset > 1<<31-1 {
return 0, r.parseError("bad offset in $GENERATE", si+3+sep) return 0, r.parseError("bad offset in $GENERATE", si+3+sep)
} }
@ -208,7 +208,7 @@ func (r *generateReader) ReadByte() (byte, error) {
} }
// Convert a $GENERATE modifier 0,0,d to something Printf can deal with. // Convert a $GENERATE modifier 0,0,d to something Printf can deal with.
func modToPrintf(s string) (string, int, string) { func modToPrintf(s string) (string, int64, string) {
// Modifier is { offset [ ,width [ ,base ] ] } - provide default // Modifier is { offset [ ,width [ ,base ] ] } - provide default
// values for optional width and type, if necessary. // values for optional width and type, if necessary.
var offStr, widthStr, base string var offStr, widthStr, base string
@ -240,8 +240,8 @@ func modToPrintf(s string) (string, int, string) {
} }
if width == 0 { if width == 0 {
return "%" + base, int(offset), "" return "%" + base, offset, ""
} }
return "%0" + widthStr + base, int(offset), "" return "%0" + widthStr + base, offset, ""
} }

View File

@ -162,7 +162,7 @@ func TestGenerateModToPrintf(t *testing.T) {
tests := []struct { tests := []struct {
mod string mod string
wantFmt string wantFmt string
wantOffset int wantOffset int64
wantErr bool wantErr bool
}{ }{
{"0,0,d", "%d", 0, false}, {"0,0,d", "%d", 0, false},