diff options
author | Mark H Weaver <mhw@netris.org> | 2013-08-16 22:54:39 -0400 |
---|---|---|
committer | Mark H Weaver <mhw@netris.org> | 2013-08-16 22:54:39 -0400 |
commit | 088cfb7d761b01a2620d78f10e8dbcaa07485a32 (patch) | |
tree | f1b59329d0d3713726e42aca62ffcacbe1d60b8b | |
parent | 8d5d0425ce10dcf035fbf717852938291261bd7e (diff) | |
download | guile-088cfb7d761b01a2620d78f10e8dbcaa07485a32.tar.gz |
Improved error checking in bytevector->uint-list and bytevector->sint-list.
Partially fixes <http://bugs.gnu.org/15100>.
Reported by Göran Weinholt <goran@weinholt.se>.
* libguile/bytevectors.c (INTEGERS_TO_LIST): Make sure SIZE isn't 0.
Allow SIZE to be greater than the bytevector length, for consistency
with allowing extra bytes at the end when the bytevector length is
non-zero. Use scm_from_size_t instead of scm_from_uint.
* test-suite/tests/bytevectors.test: Add tests. Remove a test that
checks for an exception when SIZE is greater than the bytevector
length.
-rw-r--r-- | libguile/bytevectors.c | 8 | ||||
-rw-r--r-- | test-suite/tests/bytevectors.test | 10 |
2 files changed, 12 insertions, 6 deletions
diff --git a/libguile/bytevectors.c b/libguile/bytevectors.c index cf41f2fb5..be8b654cb 100644 --- a/libguile/bytevectors.c +++ b/libguile/bytevectors.c @@ -1099,20 +1099,18 @@ SCM_DEFINE (scm_bytevector_sint_set_x, "bytevector-sint-set!", 5, 0, 0, \ SCM_VALIDATE_BYTEVECTOR (1, bv); \ SCM_VALIDATE_SYMBOL (2, endianness); \ - c_size = scm_to_uint (size); \ + c_size = scm_to_unsigned_integer (size, 1, (size_t) -1); \ \ c_len = SCM_BYTEVECTOR_LENGTH (bv); \ - if (SCM_UNLIKELY (c_len == 0)) \ + if (SCM_UNLIKELY (c_len < c_size)) \ lst = SCM_EOL; \ - else if (SCM_UNLIKELY (c_len < c_size)) \ - scm_out_of_range (FUNC_NAME, size); \ else \ { \ const char *c_bv; \ \ c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \ \ - lst = scm_make_list (scm_from_uint (c_len / c_size), \ + lst = scm_make_list (scm_from_size_t (c_len / c_size), \ SCM_UNSPECIFIED); \ for (i = 0, pair = lst; \ i <= c_len - c_size; \ diff --git a/test-suite/tests/bytevectors.test b/test-suite/tests/bytevectors.test index 67fc6801f..524ce86b5 100644 --- a/test-suite/tests/bytevectors.test +++ b/test-suite/tests/bytevectors.test @@ -155,9 +155,17 @@ (let ((b (make-bytevector 0))) (null? (bytevector->uint-list b (endianness big) 2)))) + (pass-if "bytevector->sint-list [length < word size]" + (let ((b (make-bytevector 1))) + (null? (bytevector->sint-list b (endianness big) 2)))) + (pass-if-exception "bytevector->sint-list [out-of-range]" exception:out-of-range - (bytevector->sint-list (make-bytevector 6) (endianness little) 8)) + (bytevector->sint-list (make-bytevector 6) (endianness little) -1)) + + (pass-if-exception "bytevector->uint-list [out-of-range]" + exception:out-of-range + (bytevector->uint-list (make-bytevector 6) (endianness little) 0)) (pass-if "bytevector->sint-list [off-by-one]" (equal? (bytevector->sint-list (make-bytevector 31 #xff) |