From 21a3155f8296175180e348f10a6a9af88e1ac87d Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 23 Feb 2012 14:56:51 +0000 Subject: [PATCH] Move variable checking to check_valid_variable_name function. --- daemon/daemon.ml | 18 +----------------- lib/whenexpr.ml | 19 +++++++++++++++++++ lib/whenexpr.mli | 4 ++++ 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/daemon/daemon.ml b/daemon/daemon.ml index 6c3799c..542c7a4 100644 --- a/daemon/daemon.ml +++ b/daemon/daemon.ml @@ -94,23 +94,7 @@ and proc_set_variable (name, value) = if !debug then Syslog.notice "remote call: set_variable %s" name; try - (* Don't permit certain names. *) - if name = "JOBSERIAL" then - failwith "JOBSERIAL variable cannot be set"; - - let len = String.length name in - if len = 0 then - failwith "variable name is an empty string"; - if name.[0] <> '_' && not (isalpha name.[0]) then - failwith "variable name must start with alphabetic character or underscore"; - - let rec loop i = - if i >= len then () - else if name.[i] <> '_' && not (isalnum name.[i]) then - failwith "variable name contains non-alphanumeric non-underscore character" - else loop (i+1) - in - loop 1; + check_valid_variable_name name; let value = variable_of_rpc value in state := Whenstate.set_variable !state name value; diff --git a/lib/whenexpr.ml b/lib/whenexpr.ml index 8ccde2c..4e46968 100644 --- a/lib/whenexpr.ml +++ b/lib/whenexpr.ml @@ -616,3 +616,22 @@ let next_periodexpr = let t0 = Date.make 1970 1 1 in let t' = Date.add t0 (Date.Period.month months) in Date.to_unixfloat t' + +let check_valid_variable_name name = + (* Don't permit certain names. *) + if name = "JOBSERIAL" then + failwith "JOBSERIAL variable cannot be set"; + + let len = String.length name in + if len = 0 then + failwith "variable name is an empty string"; + if name.[0] <> '_' && not (isalpha name.[0]) then + failwith "variable name must start with alphabetic character or underscore"; + + let rec loop i = + if i >= len then () + else if name.[i] <> '_' && not (isalnum name.[i]) then + failwith "variable name contains non-alphanumeric non-underscore character" + else loop (i+1) + in + loop 1 diff --git a/lib/whenexpr.mli b/lib/whenexpr.mli index 12ba584..7d1c2de 100644 --- a/lib/whenexpr.mli +++ b/lib/whenexpr.mli @@ -161,3 +161,7 @@ val next_periodexpr : float -> periodexpr -> float midnight UTC on the 1st day of the year when [(y - 1970) mod i == 0]. This returns midnight on the 1st day of the next year > t. *) + +val check_valid_variable_name : string -> unit +(** Check that [name] is a valid variable name that users are permitted + to set, and raise [Failure] if it is not. *) -- 1.8.3.1