On Mon, Feb 18, 2008 at 03:20:37PM +0100, Jim Meyering wrote:
"Daniel P. Berrange" <berrange(a)redhat.com> wrote:
> +static int virStorageSize(virConnectPtr conn,
> + const char *unit,
> + const char *val,
> + unsigned long long *ret) {
> + unsigned long long mult;
> + char *end;
> +
> + if (!unit) {
> + mult = 1;
> + } else {
> + switch (unit[0]) {
> + case 'k':
> + case 'K':
> + mult = 1024ull;
> + break;
> +
> + case 'm':
> + case 'M':
> + mult = 1024ull * 1024ull;
> + break;
> +
> + case 'g':
> + case 'G':
> + mult = 1024ull * 1024ull * 1024ull;
> + break;
> +
> + case 't':
> + case 'T':
> + mult = 1024ull * 1024ull * 1024ull * 1024ull;
> + break;
From our current perspective, 'T' looks big enough, but you might want
to add 'P', 'Y', and 'Z', if only so the code doesn't seem
clueless
when reporting "unknown size units" :-)
Yeah, might as well.
> + *ret = strtoull(val, &end, 10);
> + if (end && *end) {
> + virStorageReportError(conn, VIR_ERR_XML_ERROR, "malformed capacity
element");
> + return -1;
> + }
> + *ret *= mult;
If you change the guard to e.g.,
if (*end || *ret > ULLONG_MAX / mult) {
virStorageReportError(conn, VIR_ERR_XML_ERROR,
"malformed capacity element");
you avoid risk of overflow.
Ok, thanks
Dan.
--
|=- Red Hat, Engineering, Emerging Technologies, Boston. +1 978 392 2496 -=|
|=- Perl modules:
http://search.cpan.org/~danberr/ -=|
|=- Projects:
http://freshmeat.net/~danielpb/ -=|
|=- GnuPG: 7D3B9505 F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 -=|