{"record":{"id":"9393daaaa07fc2fd","repo":"mikefarah/yq","slug":"panic-err-on-parsefloat-failure-while-sorting-num","errorCode":null,"errorMessage":"panic(err) on ParseFloat failure while sorting numeric values","messagePattern":"panic\\(err\\) on ParseFloat failure while sorting numeric values","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/yqlib/operator_sort.go","lineNumber":178,"sourceCode":"\t} else if lhsTag == \"!!int\" && rhsTag == \"!!int\" {\n\t\t_, lhsNum, err := parseInt64(lhs.Value)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\t_, rhsNum, err := parseInt64(rhs.Value)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tif lhsNum < rhsNum {\n\t\t\treturn -1\n\t\t} else if lhsNum > rhsNum {\n\t\t\treturn 1\n\t\t}\n\t\treturn 0\n\t} else if (lhsTag == \"!!int\" || lhsTag == \"!!float\") && (rhsTag == \"!!int\" || rhsTag == \"!!float\") {\n\t\tlhsNum, err := strconv.ParseFloat(lhs.Value, 64)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\trhsNum, err := strconv.ParseFloat(rhs.Value, 64)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tif lhsNum == rhsNum {\n\t\t\treturn 0\n\t\t} else if lhsNum < rhsNum {\n\t\t\treturn -1\n\t\t}\n\n\t\treturn 1\n\t}\n\n\treturn strings.Compare(lhs.Value, rhs.Value)\n}\n","sourceCodeStart":160,"sourceCodeEnd":195,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_sort.go#L160-L195","documentation":"The sort compare function panics when comparing values tagged !!int/!!float (mixed) and strconv.ParseFloat fails on the left operand at operator_sort.go:176-179. This branch is reached when one side is !!float, so even a valid-looking int value gets re-parsed as float; any value string ParseFloat rejects (NaN-like words, underscores, hex, empty) triggers the panic inside sort.Less, killing the process.","triggerScenarios":"sort/sort_by on an array containing at least one !!float element alongside a node whose .Value cannot be parsed as a float64 despite its numeric tag: e.g. `\"1.2.3\"` tagged !!float, `\"\"` tagged !!int, `0x1F` tagged !!int, or `Inf`/`NaN` spellings unsupported by ParseFloat.","commonSituations":"Mixing integers and floats in one array where one value is malformed; data converted from formats that allow hex/octal numeric literals (YAML 1.1 `0x1F`, `1_000`) which Go's ParseFloat rejects; empty-string placeholders tagged numeric by a pipeline; scientific-notation edge cases.","solutions":["Scan the array before sorting and fix values whose tags are numeric but whose text is not ParseFloat-compatible: `yq '.[] | select(tag == \"!!float\" or tag == \"!!int\")'.","Normalize YAML 1.1 hex/underscore literals to decimal, or retag them !!str for lexicographic sort: `sort_by(.v | tag=\"!!str\")`.","Replace empty/placeholder numerics with real numbers or strings before sorting.","Force the whole field to !!str when numeric fidelity is not required, avoiding both numeric branches entirely.","Upgrade yq so ParseFloat failures degrade to string comparison instead of panicking."],"exampleFix":"# before\nyq 'sort_by(.score)' m.yaml   # .score: [1.5, 0x1F] -> ParseFloat(\"0x1F\") panics\n\n# after\nyq '(.score[] | select(tag == \"!!int\" and (. =~ \"^0x\"))) |= (tag=\"!!str\")' m.yaml | yq 'sort_by(.score)'","handlingStrategy":"validation","validationCode":"# find numeric-tagged values Go ParseFloat would reject\nyq '.[] | select(tag == \"!!int\" or tag == \"!!float\") | select(test(\"^[-+]?([0-9]*\\\\.)?[0-9]+([eE][-+]?[0-9]+)?$\") | not)' m.yaml","typeGuard":"// Go: float parseability guard\nfunc isParseableFloat(s string) bool {\n    _, err := strconv.ParseFloat(s, 64)\n    return err == nil\n}","tryCatchPattern":"out, err := func() (out string, err error) {\n    defer func() { if r := recover(); r != nil { err = fmt.Errorf(\"sort ParseFloat panic: %v\", r) } }()\n    out = runYq(\"sort_by(.score)\")\n    return\n}()","preventionTips":["Sanitize mixed int/float arrays before sorting; drop or retag placeholders like empty strings","Convert YAML 1.1 literals (0x10, 1_000) to decimal or !!str upstream","Keep locale-formatted numbers out of numerically tagged fields","Validate numeric columns from CSV/JSON imports before running sort_by"],"tags":["panic","sorting","yq","parsefloat"],"backgroundTag":"sort-numeric-parse-panic","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}