Skip to content

Commit e5edaf4

Browse files
committed
Adding value verifications for mem_limit string representation
1 parent 019468d commit e5edaf4

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

docker/client.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,26 @@ def _container_config(self, image, command, hostname=None, user=None,
112112
]
113113

114114
if isinstance(mem_limit, six.string_types):
115-
units = {'k': 1024,
116-
'm': 1024*1024,
117-
'g': 1024*1024*1024}
118-
suffix = mem_limit[-1].lower()
119-
if suffix in units.keys():
120-
mem_limit = int(mem_limit[:-1]) * units[suffix]
115+
if len(mem_limit) == 0:
116+
mem_limit = 0
117+
else:
118+
units = {'k': 1024,
119+
'm': 1024*1024,
120+
'g': 1024*1024*1024}
121+
suffix = mem_limit[-1].lower()
122+
123+
if suffix in units.keys():
124+
try:
125+
digits = int(mem_limit[:-1])
126+
except ValueError:
127+
message = ('Failed converting the string value for mem_limit ({0}) to a number.')
128+
raise errors.DockerException(message.format(mem_limit[:-1]))
129+
130+
mem_limit = digits * units[suffix]
131+
else:
132+
message = ('The specified value for mem_limit parameter ({0}) should specify'
133+
' the units. The postfix should be one of the `k` `m` `g` characters' )
134+
raise errors.DockerException(message.format(mem_limit))
121135

122136
if isinstance(ports, list):
123137
exposed_ports = {}

0 commit comments

Comments
 (0)