From c9c8d8b2dd9657cee7c2630782c4910d9d31498c Mon Sep 17 00:00:00 2001 From: Sam Maloney <56830868+sam-maloney@users.noreply.github.com> Date: Fri, 14 Feb 2025 10:42:44 +0100 Subject: [PATCH] jobspec: enforce constraints on range key values Problem: certain nonsensical combinations of range keys are currently not checked for when initializing ranges Add checks ensuring that specified min/max/operator/operand combinations are self-consistent --- resource/libjobspec/jobspec.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/resource/libjobspec/jobspec.cpp b/resource/libjobspec/jobspec.cpp index 34cae0faf..33daa1c50 100644 --- a/resource/libjobspec/jobspec.cpp +++ b/resource/libjobspec/jobspec.cpp @@ -91,16 +91,31 @@ void parse_yaml_count (Resource &res, const YAML::Node &cnode) if (res.count.min < 1) { throw parse_error (cnode["min"], "\"min\" must be greater than zero"); } - if (res.count.max < 1) { - throw parse_error (cnode["max"], "\"max\" must be greater than zero"); - } if (res.count.max < res.count.min) { throw parse_error (cnode["max"], "\"max\" must be greater than or equal to \"min\""); } switch (res.count.oper) { case '+': + if (res.count.operand < 1) { + throw parse_error (cnode["operand"], + "\"operand\" must be greater than zero for addition '+'"); + } + break; case '*': + if (res.count.operand < 2) { + throw parse_error (cnode["operand"], + "\"operand\" must be greater than one for multiplication '*'"); + } + break; case '^': + if (res.count.operand < 2) { + throw parse_error (cnode["operand"], + "\"operand\" must be greater than one for exponentiation '^'"); + } + if (res.count.min < 2) { + throw parse_error (cnode["min"], + "\"min\" must be greater than one for exponentiation '^'"); + } break; default: throw parse_error (cnode["operator"], "Invalid count operator");