{"record":{"id":"e51445e445e5286a","repo":"mikefarah/yq","slug":"cannot-modulo-by-0","errorCode":null,"errorMessage":"cannot modulo by 0","messagePattern":"cannot modulo by 0","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_modulo.go","lineNumber":57,"sourceCode":"\t\t// custom tag - we have to have a guess\n\t\tlhsTag = lhs.guessTagFromCustomType()\n\t\tlhsIsCustom = true\n\t}\n\n\tif lhsTag == \"!!int\" && rhsTag == \"!!int\" {\n\t\ttarget.Kind = ScalarNode\n\t\ttarget.Style = lhs.Style\n\n\t\tformat, lhsNum, err := parseInt64(lhs.Value)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\t_, rhsNum, err := parseInt64(rhs.Value)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif rhsNum == 0 {\n\t\t\treturn fmt.Errorf(\"cannot modulo by 0\")\n\t\t}\n\t\tremainder := lhsNum % rhsNum\n\n\t\ttarget.Tag = lhs.Tag\n\t\ttarget.Value = fmt.Sprintf(format, remainder)\n\t} else if (lhsTag == \"!!int\" || lhsTag == \"!!float\") && (rhsTag == \"!!int\" || rhsTag == \"!!float\") {\n\t\ttarget.Kind = ScalarNode\n\t\ttarget.Style = lhs.Style\n\n\t\tlhsNum, err := strconv.ParseFloat(lhs.Value, 64)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\trhsNum, err := strconv.ParseFloat(rhs.Value, 64)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tremainder := math.Mod(lhsNum, rhsNum)","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_modulo.go#L39-L75","documentation":"moduloScalars verified both operands parse as int64 and then checks the divisor before computing lhsNum % rhsNum — a rhs of integer zero would panic the runtime (integer division by zero), so the guard converts it into an explicit error. This is the modulo equivalent of a divide-by-zero check.","triggerScenarios":"Thrown at pkg/yqlib/operator_modulo.go:57 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Filter zero divisors: select(.mod != 0) before applying %","Substitute a non-zero default: .n % (.mod // 1)","Fix the data so the modulus operand is never 0"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}