Files
w3cjandClaude Opus 5.5 0b85f5f557 fix(size): a size that isn't a number is refused, never read as 0
Size.Backend dropped strconv's errors, so a VM's size in vm.json that
isn't a number, such as "cpus": "four" after an edit by hand, read as
0, the image's, and the VM started with the image's size without a
word. Backend now reads each part with the check that took it
(settings.CPUCount, settings.GiB, which ParseCPUs and ParseGiB now
use), and returns an error naming the key and value. storedSize turns
a stored one into size_stored_invalid, naming the VM. Every start
(bringUp, before anything runs), a restart's stop (stopToStart, so a
running VM keeps running, as a refused proxy start leaves it), devvm
size and a remake's check refuse with it; devvm size --KEY sets it
again. ParseCPUs and ParseGiB also refuse a number too big for an int
or a float64, which for --disk reached Lima as +Inf.

Lima's originOf read base's disk with ParseFloat and dropped its
error: "NaN", "Inf" and "1e400" passed its check of a size above 0.
Only devvm's own Origin writes that step (gibString of BaseDiskGiB), so
no user, VM or limactl output reaches it; it now reads it with
settings.GiB and fails naming the value.

Every other discarded strconv error outside tests goes too:
- hostgit.Bool reads a boolean as git does (yes, on, whole numbers)
  and says when a value isn't one; GIT_CONFIG_NOSYSTEM, which
  strconv.ParseBool read as false for "yes", uses it, as does the clone's
  copy of a repo's config (gitBool, which had its own copy).
- image.GlibcOlder checks both versions itself and reports ok, so one
  that isn't a version is never read as 0.
- leaktest and state's lock order panic on a goroutine number
  runtime.Stack doesn't print today, rather than reading it as 0.
- cli's test-build overrides fail on a number that isn't one (a test
  build's panic), the stub limactl refuses a size it can't read, and
  opencodeMajor's fallback to 0 is explicit.

Tests: TestSizeBackend, TestStoredSizeInvalid (a stopped VM and a
running one's restart), TestSizeNumbers, new TestParseSizes and
TestOriginOfRefuses cases, TestParseUnknownFormat, TestBool,
TestUserFilesOf's git booleans, TestGlibc's invalid versions; each
fails with its fix reverted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-16 08:22:47 -07:00

37 lines
1.2 KiB
Go

package image
import (
"regexp"
"strconv"
"strings"
)
// glibcPattern is a glibc version as devvm records one: major.minor, as
// getconf GNU_LIBC_VERSION and the GLIBC_ versions programs need have it.
var glibcPattern = regexp.MustCompile(`^[0-9]{1,3}\.[0-9]{1,3}$`)
// ValidGlibc reports whether s is a glibc version as devvm records one: a
// --from base's (2.36) and the newest a tool image needs.
func ValidGlibc(s string) bool { return glibcPattern.MatchString(s) }
// GlibcOlder reports whether glibc version a is older than b: 2.9 is
// older than 2.28. ok is false, and older with it, unless both are
// ValidGlibc, so a version that isn't one is never read as 0.
func GlibcOlder(a, b string) (older, ok bool) {
am, an, aok := glibcParts(a)
bm, bn, bok := glibcParts(b)
if !aok || !bok {
return false, false
}
return am < bm || am == bm && an < bn, true
}
// glibcParts is a ValidGlibc version's major and minor numbers; ok is
// false for one that isn't.
func glibcParts(v string) (major, minor int, ok bool) {
a, b, _ := strings.Cut(v, ".")
major, errMajor := strconv.Atoi(a)
minor, errMinor := strconv.Atoi(b)
return major, minor, errMajor == nil && errMinor == nil && ValidGlibc(v)
}