Commit 691744c4 authored by Iustin Pop's avatar Iustin Pop
Browse files

Ensure all int/float conversions are handled right



int()/float() can raise either ValueError (in case of int("a")), or
TypeError (in case of int(None)). We had many bugs over time due to
this, and a recent one was just diagnosed, so we go over the codebase
and replace all 'except ValueError' with 'except (TypeError,
ValueError)' that protect such conversions (there were no 'except
TypeError' cases that needed a ValueError added).
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent 146afafb
......@@ -811,7 +811,7 @@ def _GetVGInfo(vg_name):
"vg_free": int(round(float(valarr[1]), 0)),
"pv_count": int(valarr[2]),
}
except ValueError, err:
except (TypeError, ValueError), err:
logging.exception("Fail to parse vgs output")
else:
logging.error("vgs output has the wrong number of fields (expected"
......
......@@ -459,7 +459,7 @@ class LogicalVolume(BlockDev):
try:
major = int(major)
minor = int(minor)
except ValueError, err:
except (TypeError, ValueError), err:
logging.error("lvs major/minor cannot be parsed: %s", str(err))
try:
......@@ -874,7 +874,7 @@ class BaseDRBD(BlockDev): # pylint: disable-msg=W0223
result.fail_reason, result.output)
try:
sectors = int(result.stdout)
except ValueError:
except (TypeError, ValueError):
_ThrowError("Invalid output from blockdev: '%s'", result.stdout)
bytes = sectors * 512
if bytes < 128 * 1024 * 1024: # less than 128MiB
......
......@@ -827,7 +827,7 @@ def GenerateTable(headers, fields, separator, data,
if unitfields.Matches(fields[idx]):
try:
val = int(val)
except ValueError:
except (TypeError, ValueError):
pass
else:
val = row[idx] = utils.FormatUnit(val, units)
......@@ -902,7 +902,7 @@ def ParseTimespec(value):
if value[-1] not in suffix_map:
try:
value = int(value)
except ValueError:
except (TypeError, ValueError):
raise errors.OpPrereqError("Invalid time specification '%s'" % value)
else:
multiplier = suffix_map[value[-1]]
......@@ -912,7 +912,7 @@ def ParseTimespec(value):
" suffix passed)")
try:
value = int(value) * multiplier
except ValueError:
except (TypeError, ValueError):
raise errors.OpPrereqError("Invalid time specification '%s'" % value)
return value
......
......@@ -4655,7 +4655,7 @@ class LUCreateInstance(LogicalUnit):
raise errors.OpPrereqError("Missing disk size")
try:
size = int(size)
except ValueError:
except (TypeError, ValueError):
raise errors.OpPrereqError("Invalid disk size '%s'" % size)
self.disks.append({"size": size, "mode": mode})
......@@ -6012,7 +6012,7 @@ class LUSetInstanceParams(LogicalUnit):
raise errors.OpPrereqError("Required disk parameter size missing")
try:
size = int(size)
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid disk size parameter: %s" %
str(err))
disk_dict['size'] = size
......@@ -6970,7 +6970,7 @@ class IAllocator(object):
" '%s'" % (nname, attr))
try:
remote_info[attr] = int(remote_info[attr])
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpExecError("Node '%s' returned invalid value"
" for '%s': %s" % (nname, attr, err))
# compute memory used by primary instances
......
......@@ -1002,7 +1002,7 @@ class HttpMessageReader(object):
if hdr_content_length:
try:
self.content_length = int(hdr_content_length)
except ValueError:
except (TypeError, ValueError):
self.content_length = None
if self.content_length is not None and self.content_length < 0:
self.content_length = None
......
......@@ -122,7 +122,7 @@ class _HttpServerToClientMessageReader(http.HttpMessageReader):
status = int(status)
if status < 100 or status > 999:
status = -1
except ValueError:
except (TypeError, ValueError):
status = -1
if status == -1:
......
......@@ -118,7 +118,7 @@ class XenHypervisor(hv_base.BaseHypervisor):
data[2] = int(data[2])
data[3] = int(data[3])
data[5] = float(data[5])
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.HypervisorError("Can't parse output of xm list,"
" line: %s, error: %s" % (line, err))
......
......@@ -669,7 +669,7 @@ class Instance(TaggableObject):
try:
idx = int(idx)
return self.disks[idx]
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err))
except IndexError:
raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
......
......@@ -502,7 +502,7 @@ def ReadPidFile(pidfile):
try:
pid = int(pf.read())
except ValueError, err:
except (TypeError, ValueError), err:
logging.info("Can't parse pid file contents", exc_info=True)
return 0
......
......@@ -118,7 +118,7 @@ def ImportInstance(opts, args):
if opts.nics:
try:
nic_max = max(int(nidx[0])+1 for nidx in opts.nics)
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err))
nics = [{}] * nic_max
for nidx, ndict in opts.nics:
......@@ -146,7 +146,7 @@ def ImportInstance(opts, args):
opts.disks = [(0, {"size": opts.sd_size})]
try:
disk_max = max(int(didx[0])+1 for didx in opts.disks)
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
disks = [{}] * disk_max
for didx, ddict in opts.disks:
......@@ -155,7 +155,7 @@ def ImportInstance(opts, args):
raise errors.OpPrereqError("Missing size for disk %d" % didx)
try:
ddict["size"] = utils.ParseUnit(ddict["size"])
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
(didx, err))
disks[didx] = ddict
......
......@@ -296,7 +296,7 @@ def AddInstance(opts, args):
if opts.nics:
try:
nic_max = max(int(nidx[0])+1 for nidx in opts.nics)
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err))
nics = [{}] * nic_max
for nidx, ndict in opts.nics:
......@@ -324,7 +324,7 @@ def AddInstance(opts, args):
opts.disks = [(0, {"size": opts.sd_size})]
try:
disk_max = max(int(didx[0])+1 for didx in opts.disks)
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
disks = [{}] * disk_max
for didx, ddict in opts.disks:
......@@ -333,7 +333,7 @@ def AddInstance(opts, args):
raise errors.OpPrereqError("Missing size for disk %d" % didx)
try:
ddict["size"] = utils.ParseUnit(ddict["size"])
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
(didx, err))
disks[didx] = ddict
......@@ -459,7 +459,7 @@ def BatchCreate(opts, args):
for elem in specs['disk_size']:
try:
size = utils.ParseUnit(elem)
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid disk size '%s' for"
" instance %s: %s" %
(elem, name, err))
......@@ -674,7 +674,7 @@ def GrowDisk(opts, args):
disk = args[1]
try:
disk = int(disk)
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid disk index: %s" % str(err))
amount = utils.ParseUnit(args[2])
op = opcodes.OpGrowDisk(instance_name=instance, disk=disk, amount=amount,
......@@ -806,7 +806,7 @@ def ReplaceDisks(opts, args):
else:
try:
disks = [int(i) for i in opts.disks.split(",")]
except ValueError, err:
except (TypeError, ValueError), err:
raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
cnt = [opts.on_primary, opts.on_secondary,
new_2ndary is not None, iallocator is not None].count(True)
......@@ -1211,14 +1211,14 @@ def SetInstanceParams(opts, args):
try:
nic_op = int(nic_op)
opts.nics[idx] = (nic_op, nic_dict)
except ValueError:
except (TypeError, ValueError):
pass
for idx, (disk_op, disk_dict) in enumerate(opts.disks):
try:
disk_op = int(disk_op)
opts.disks[idx] = (disk_op, disk_dict)
except ValueError:
except (TypeError, ValueError):
pass
if disk_op == constants.DDM_ADD:
if 'size' not in disk_dict:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment