{"record":{"id":"09c6e3edaac98c9f","repo":"wmjordan/PDFPatcher","slug":"0-6","errorCode":null,"errorMessage":"转换精度不能小于 0 或大于 6。","messagePattern":"转换精度不能小于 0 或大于 6。","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"App/Model/UnitConverter.cs","lineNumber":26,"sourceCode":"\t{\r\n\t\tinternal const string ToStringFormat = \"0.###\";\r\n\t\tconst string Null = \"null\";\r\n\r\n\t\t/// <summary>\r\n\t\t/// 获取单位转换因数。\r\n\t\t/// </summary>\r\n\t\t[XmlIgnore]\r\n\t\tpublic float UnitFactor { get; private set; }\r\n\r\n\t\tprivate int _Precision;\r\n\t\tprivate float _PreservedValue;\r\n\t\t///<summary>获取或指定转换精度的值。</summary>\r\n\t\t[XmlIgnore]\r\n\t\tpublic int Precision {\r\n\t\t\tget => _Precision;\r\n\t\t\tset {\r\n\t\t\t\tif (value < 0 || value > 6) {\r\n\t\t\t\t\tthrow new ArgumentException(\"转换精度不能小于 0 或大于 6。\");\r\n\t\t\t\t}\r\n\t\t\t\t_Precision = value;\r\n\t\t\t\t_PreservedValue = (float)Math.Pow(0.1, _Precision);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tprivate string _Unit;\r\n\t\t///<summary>获取或指定转换使用的单位。</summary>\r\n\t\t[XmlAttribute(\"单位\")]\r\n\t\tpublic string Unit {\r\n\t\t\tget => _Unit;\r\n\t\t\tset {\r\n\t\t\t\tvar f = ValueHelper.MapValue(value, Constants.Units.Names, Constants.Units.Factors, 0);\r\n\t\t\t\tif (f == 0) {\r\n\t\t\t\t\tthrow new ArgumentException(\"尺寸单位无效。\");\r\n\t\t\t\t}\r\n\t\t\t\tUnitFactor = f;\r\n\t\t\t\t_Unit = value;\r","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Model/UnitConverter.cs#L8-L44","documentation":"The UnitConverter.Precision setter clamps precision to the inclusive range 0–6; anything outside throws ArgumentException. Precision controls how many decimal places are kept when converting between PDF points and display units (it is used to compute _PreservedValue = 0.1^precision). The bound exists because the converter formats with ToStringFormat '0.###' (3 digits) by default but allows up to 6.","triggerScenarios":"Assigning UnitConverter.Precision a value less than 0 or greater than 6, or deserializing a configuration/options object whose Precision property (backed by this setter) is out of range.","commonSituations":"User sets an unrealistic precision in the options dialog; a serialized options file with a corrupt precision value; copying a precision constant from another library with a different scale.","solutions":["Clamp the precision value to [0, 6] before assigning it to UnitConverter.Precision.","Validate deserialized options files for a precision field in range before applying them.","If a finer granularity is genuinely needed, reconsider the design rather than widening the bound (the formatting and preserved-value math assume ≤6)."],"exampleFix":"// before\nconverter.Precision = 8;\n\n// after\nconverter.Precision = Math.Clamp(desired, 0, 6);","handlingStrategy":"validation","validationCode":"int p = desiredPrecision;\nif (p < 0 || p > 6) p = Math.Clamp(p, 0, 6);\nconverter.Precision = p;","typeGuard":"static bool IsValidPrecision(int p) => p >= 0 && p <= 6;","tryCatchPattern":null,"preventionTips":["Always clamp user-supplied precision to [0,6].","Validate options-file precision field on load.","Document the 0–6 bound next to the Precision property."],"tags":["validation","unit-conversion","argument","configuration"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}