internal/postgres: check parsing errors & fix patch parsing

This CL adds error checking for the semver parsing in InsertVersion.
It also fixes patch parsing so that it properly isolates the number
string when there is a prerelease.

Fixes b/129717943

Change-Id: I542b533d9508027917b1f9fa63867607b31d23fc
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/443170
Reviewed-by: Julie Qiu <julieqiu@google.com>
This commit is contained in:
Channing Kimble-Brown 2019-04-02 00:28:59 -04:00 коммит произвёл Julie Qiu
Родитель 0c16a4b73e
Коммит cf00ac867d
1 изменённых файлов: 13 добавлений и 4 удалений

Просмотреть файл

@ -470,12 +470,21 @@ func (db *DB) InsertVersion(version *internal.Version) error {
}
versionSplit := strings.Split(version.Version, ".")
major, _ := strconv.ParseInt(strings.TrimPrefix("v", versionSplit[0]), 0, 64)
minor, _ := strconv.ParseInt(versionSplit[1], 0, 64)
patch, _ := strconv.ParseInt(strings.Split(versionSplit[2], semver.Prerelease(version.Version))[0], 0, 64)
major, err := strconv.Atoi(strings.TrimPrefix(versionSplit[0], "v"))
if err != nil {
return fmt.Errorf("strconv.Atoi(%q): %v", strings.TrimPrefix(versionSplit[0], "v"), err)
}
minor, err := strconv.Atoi(versionSplit[1])
if err != nil {
return fmt.Errorf("strconv.Atoi(%q): %v", versionSplit[1], err)
}
patch, err := strconv.Atoi(strings.Split(versionSplit[2], "-")[0])
if err != nil {
return fmt.Errorf("strconv.Atoi(%q): %v", strings.Split(versionSplit[2], "-")[0], err)
}
prerelease, err := padPrerelease(semver.Prerelease(version.Version))
if err != nil {
return fmt.Errorf("error padding prerelease: %v", err)
return fmt.Errorf("padPrerelease(%q): %v", semver.Prerelease(version.Version), err)
}
if _, err := tx.Exec(