Skip to content

Commit 573256a

Browse files
committed
Add error checking
1 parent e3e2fcb commit 573256a

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

internal/dinosql/parser.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,21 +558,29 @@ func expandStmt(c core.Catalog, raw nodes.RawStmt, node nodes.Node) ([]edit, err
558558
}
559559

560560
func editQuery(raw string, a []edit) (string, error) {
561+
if len(a) == 0 {
562+
return raw, nil
563+
}
561564
sort.Slice(a, func(i, j int) bool { return a[i].Location > a[j].Location })
562-
// TODO: Check bounds
563565
s := raw
564566
for _, edit := range a {
565-
// fmt.Printf("edit q %q\n", s)
566-
// fmt.Printf("edit e %q\n", edit)
567567
start := edit.Location
568+
if start > len(s) {
569+
return "", fmt.Errorf("edit start location is out of bounds")
570+
}
571+
if len(edit.New) <= 0 {
572+
return "", fmt.Errorf("empty edit contents")
573+
}
574+
if len(edit.Old) <= 0 {
575+
return "", fmt.Errorf("empty edit contents")
576+
}
568577
stop := edit.Location + len(edit.Old) - 1 // Assumes edit.New is non-empty
569578
if stop < len(s) {
570579
s = s[:start] + edit.New + s[stop+1:]
571580
} else {
572581
s = s[:start] + edit.New
573582
}
574583
}
575-
// fmt.Printf("edit fixed %q\n", s)
576584
return s, nil
577585
}
578586

0 commit comments

Comments
 (0)