diff options
author | Richard Jones <rjones@redhat.com> | 2010-01-22 11:01:06 +0000 |
---|---|---|
committer | Richard Jones <rjones@redhat.com> | 2010-01-25 12:07:34 +0000 |
commit | 58abe782bf7137526b4a5c7e6d5d2b145e3b09d2 (patch) | |
tree | 7a58d84c5e3ae7362c386611e2b65047d31d2952 /regressions | |
parent | 306077ac965cde0a5605782921d33a72bb77c382 (diff) | |
download | libguestfs-58abe782bf7137526b4a5c7e6d5d2b145e3b09d2.tar.gz libguestfs-58abe782bf7137526b4a5c7e6d5d2b145e3b09d2.tar.xz libguestfs-58abe782bf7137526b4a5c7e6d5d2b145e3b09d2.zip |
guestfish: Use xstrtol to parse integers (RHBZ#557655).
Current code uses atoi to parse the generator Int type and
atoll to parse the generator Int64 type. The problem with the
ato* functions is that they don't cope with errors very well,
and they cannot parse numbers that begin with 0.. or 0x..
for octal and hexadecimal respectively.
This replaces the atoi call with a call to Gnulib xstrtol
and the atoll call with a call to Gnulib xstrtoll.
The generated code looks like this for all Int arguments:
{
strtol_error xerr;
long r;
xerr = xstrtol (argv[0], NULL, 0, &r, "");
if (xerr != LONGINT_OK) {
fprintf (stderr,
_("%s: %s: invalid integer parameter (%s returned %d)\n"),
cmd, "memsize", "xstrtol", xerr);
return -1;
}
/* The Int type in the generator is a signed 31 bit int. */
if (r < (-(2LL<<30)) || r > ((2LL<<30)-1)) {
fprintf (stderr, _("%s: %s: integer out of range\n"), cmd, "memsize");
return -1;
}
/* The check above should ensure this assignment does not overflow. */
memsize = r;
}
and like this for all Int64 arguments (note we don't need the
range check for these):
{
strtol_error xerr;
long long r;
xerr = xstrtoll (argv[1], NULL, 0, &r, "");
if (xerr != LONGINT_OK) {
fprintf (stderr,
_("%s: %s: invalid integer parameter (%s returned %d)\n"),
cmd, "size", "xstrtoll", xerr);
return -1;
}
size = r;
}
Note this also fixes an unrelated bug in guestfish handling of
RBufferOut. We were using 'fwrite' without checking the return
value, and this could have caused silent failures, eg. in the case
where there was not enough disk space to store the resulting file,
or even if the program was interrupted (but continued) during the
write.
Replace this with Gnulib 'full-write', and check the return value
and report errors.
Diffstat (limited to 'regressions')
-rw-r--r-- | regressions/Makefile.am | 4 | ||||
-rw-r--r-- | regressions/rhbz557655-expected.out | 22 | ||||
-rwxr-xr-x | regressions/rhbz557655.sh | 83 |
3 files changed, 108 insertions, 1 deletions
diff --git a/regressions/Makefile.am b/regressions/Makefile.am index 7ceb0cee..3ce4b8d5 100644 --- a/regressions/Makefile.am +++ b/regressions/Makefile.am @@ -26,6 +26,7 @@ include $(top_srcdir)/subdir-rules.mk TESTS = \ rhbz503169c10.sh \ rhbz503169c13.sh \ + rhbz557655.sh \ test-cancellation-download-librarycancels.sh \ test-cancellation-upload-daemoncancels.sh \ test-find0.sh \ @@ -55,4 +56,5 @@ TESTS_ENVIRONMENT = \ EXTRA_DIST = \ $(FAILING_TESTS) \ $(SKIPPED_TESTS) \ - $(TESTS) + $(TESTS) \ + rhbz557655-expected.out diff --git a/regressions/rhbz557655-expected.out b/regressions/rhbz557655-expected.out new file mode 100644 index 00000000..7d37e842 --- /dev/null +++ b/regressions/rhbz557655-expected.out @@ -0,0 +1,22 @@ +0 +16 +8 +-1073741824 +1073741823 +set-memsize: memsize: integer out of range +set-memsize: memsize: integer out of range +set-memsize: memsize: integer out of range +set-memsize: memsize: integer out of range +set-memsize: memsize: invalid integer parameter (xstrtol returned 4) +set-memsize: memsize: invalid integer parameter (xstrtol returned 2) +set-memsize: memsize: invalid integer parameter (xstrtol returned 2) +set-memsize: memsize: invalid integer parameter (xstrtol returned 2) +1234 +1234 +1234 +libguestfs: error: truncate_size: ftruncate: /test: File too large +truncate-size: size: invalid integer parameter (xstrtoll returned 1) +truncate-size: size: invalid integer parameter (xstrtoll returned 4) +truncate-size: size: invalid integer parameter (xstrtoll returned 2) +truncate-size: size: invalid integer parameter (xstrtoll returned 2) +truncate-size: size: invalid integer parameter (xstrtoll returned 2) diff --git a/regressions/rhbz557655.sh b/regressions/rhbz557655.sh new file mode 100755 index 00000000..6ef0b70d --- /dev/null +++ b/regressions/rhbz557655.sh @@ -0,0 +1,83 @@ +#!/bin/bash - +# libguestfs +# Copyright (C) 2009 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +# Regression test for: +# https://bugzilla.redhat.com/show_bug.cgi?id=557655 +# "guestfish number parsing should not use atoi, should support '0...' for octal and '0x...' for hexadecimal" + +set -e +rm -f test.out +export LANG=C + +../fish/guestfish >> test.out 2>&1 <<EOF +# set-memsize is just a convenient non-daemon function that +# takes a single integer argument. +set-memsize 0 +get-memsize +set-memsize 0x10 +get-memsize +set-memsize 010 +get-memsize +set-memsize -1073741824 +get-memsize +set-memsize 1073741823 +get-memsize + +# the following should all provoke error messages: +-set-memsize -9000000000000000 +-set-memsize 9000000000000000 +-set-memsize 0x900000000000 +-set-memsize 07777770000000000000 +-set-memsize ABC +-set-memsize 09 +-set-memsize 123K +-set-memsize 123L +EOF + +../fish/guestfish >> test.out 2>&1 <<EOF +alloc test1.img 10M +run +part-disk /dev/sda mbr +mkfs ext2 /dev/sda1 +mount /dev/sda1 / + +touch /test + +# truncate-size takes an Int64 argument +truncate-size /test 1234 +filesize /test +truncate-size /test 0x4d2 +filesize /test +truncate-size /test 02322 +filesize /test + +# should parse OK, but underlying filesystem will reject it: +-truncate-size /test 0x7fffffffffffffff + +# larger than 64 bits, should be an error: +-truncate-size /test 0x10000000000000000 + +# these should all provoke parse errors: +-truncate-size /test ABC +-truncate-size /test 09 +-truncate-size /test 123K +-truncate-size /test 123L +EOF + +diff -u test.out rhbz557655-expected.out +rm test.out test1.img |