Skip to content

Commit

Permalink
Disallow NaN values when float parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kageurufu committed Dec 6, 2024
1 parent 815b23e commit 24daba7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions klippy/configfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (C) 2016-2021 Kevin O'Connor <[email protected]>
#
# 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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -209,7 +211,7 @@ def getfloatlist(
default,
seps=(sep,),
count=count,
parser=float,
parser=mathutil.safe_float,
note_valid=note_valid,
)

Expand Down
5 changes: 4 additions & 1 deletion klippy/gcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -124,7 +127,7 @@ def get_float(
return self.get(
name,
default,
parser=float,
parser=mathutil.safe_float,
minval=minval,
maxval=maxval,
above=above,
Expand Down
7 changes: 7 additions & 0 deletions klippy/mathutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
######################################################################
Expand Down

0 comments on commit 24daba7

Please sign in to comment.