From 24daba7e0e036aed65d1b4ea96f2b90422283826 Mon Sep 17 00:00:00 2001 From: Frank Tackitt Date: Fri, 6 Dec 2024 08:39:37 -0700 Subject: [PATCH] Disallow NaN values when float parsing --- klippy/configfile.py | 6 ++++-- klippy/gcode.py | 5 ++++- klippy/mathutil.py | 7 +++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/klippy/configfile.py b/klippy/configfile.py index 36cad0b77..e157c9bd8 100644 --- a/klippy/configfile.py +++ b/klippy/configfile.py @@ -3,7 +3,7 @@ # Copyright (C) 2016-2021 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import sys, os, glob, re, time, logging, configparser, io +import sys, os, glob, re, time, logging, configparser, io, mathutil from extras.danger_options import get_danger_options error = configparser.Error @@ -49,6 +49,8 @@ def _get_wrapper( "Option '%s' in section '%s' must be specified" % (option, self.section) ) + if parser is float: + parser = mathutil.safe_float try: v = parser(self.section, option) except self.error as e: @@ -209,7 +211,7 @@ def getfloatlist( default, seps=(sep,), count=count, - parser=float, + parser=mathutil.safe_float, note_valid=note_valid, ) diff --git a/klippy/gcode.py b/klippy/gcode.py index e788d31f2..79304caeb 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -4,6 +4,7 @@ # # This file may be distributed under the terms of the GNU GPLv3 license. import os, re, logging, collections, shlex +import mathutil class CommandError(Exception): @@ -74,6 +75,8 @@ def get( above=None, below=None, ): + if parser is float: + parser = mathutil.safe_float value = self._params.get(name) if value is None: if default is self.sentinel: @@ -124,7 +127,7 @@ def get_float( return self.get( name, default, - parser=float, + parser=mathutil.safe_float, minval=minval, maxval=maxval, above=above, diff --git a/klippy/mathutil.py b/klippy/mathutil.py index a53ad6d79..6226db6ca 100644 --- a/klippy/mathutil.py +++ b/klippy/mathutil.py @@ -7,6 +7,13 @@ import queuelogger +def safe_float(v: str) -> float: + f = float(v) + if math.isnan(f): + raise ValueError(f"{v} is not a valid float") + return f + + ###################################################################### # Coordinate descent ######################################################################