Fix list validation of *list->bytevector procedures.
authorMark H Weaver <mhw@netris.org>
Sun, 14 Oct 2018 06:22:22 +0000 (02:22 -0400)
committerMark H Weaver <mhw@netris.org>
Sun, 14 Oct 2018 06:27:23 +0000 (02:27 -0400)
Fixes <https://bugs.gnu.org/32938>.
Reported by Josh Datko <jbd@cryptotronix.com>.

* libguile/validate.h (SCM_VALIDATE_LIST_COPYLEN)
(SCM_VALIDATE_NONEMPTYLIST_COPYLEN): Use '!=' instead of '>=' to
validate the result of 'scm_ilength' after it has been stored in
the user variable 'cvar'.
* test-suite/tests/bytevectors.test: Add tests.  Use '#:use-module'
instead of ':use-module' in 'define-module' form.

libguile/validate.h
test-suite/tests/bytevectors.test

index a1b1b553a728fa4e9c2784d16b3a5c4548e320a9..23786554705aa7e80efc55fcaf90cb0433bd470e 100644 (file)
@@ -3,8 +3,8 @@
 #ifndef SCM_VALIDATE_H
 #define SCM_VALIDATE_H
 
-/* Copyright (C) 1999, 2000, 2001, 2002, 2004, 2006, 2007, 2009,
- *   2011, 2012, 2013, 2014 Free Software Foundation, Inc.
+/* Copyright (C) 1999-2002, 2004, 2006, 2007, 2009, 2011-2014,
+ *   2018 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
     SCM_ASSERT (scm_ilength (lst) > 0, lst, pos, FUNC_NAME); \
   } while (0)
 
+/* Note: we use (cvar != -1) instead of (cvar >= 0) below
+   in case 'cvar' is of unsigned type. */
 #define SCM_VALIDATE_LIST_COPYLEN(pos, lst, cvar) \
   do { \
     cvar = scm_ilength (lst); \
-    SCM_ASSERT (cvar >= 0, lst, pos, FUNC_NAME); \
+    SCM_ASSERT (cvar != -1, lst, pos, FUNC_NAME); \
   } while (0)
 
+/* Note: we use (cvar != -1 && cvar != 0) instead of
+   (cvar >= 1) below in case 'cvar' is of unsigned type. */
 #define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos, lst, cvar) \
   do { \
     cvar = scm_ilength (lst); \
-    SCM_ASSERT (cvar >= 1, lst, pos, FUNC_NAME); \
+    SCM_ASSERT (cvar != -1 && cvar != 0, lst, pos, FUNC_NAME); \
   } while (0)
 
 #define SCM_VALIDATE_ALISTCELL(pos, alist) \
index f0d9f19835dd32e7e6a494d3c7ed3019ed3dbfa7..5d4568d82550f52d6cd509095337f7b2d555dba3 100644 (file)
@@ -1,6 +1,6 @@
 ;;;; bytevectors.test --- R6RS bytevectors. -*- mode: scheme; coding: utf-8; -*-
 ;;;;
-;;;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
+;;;; Copyright (C) 2009-2015, 2018 Free Software Foundation, Inc.
 ;;;;
 ;;;; Ludovic Courtès
 ;;;;
 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 (define-module (test-bytevector)
-  :use-module (test-suite lib)
-  :use-module (system base compile)
-  :use-module (rnrs bytevectors)
-  :use-module (srfi srfi-4))
+  #:use-module (test-suite lib)
+  #:use-module (system base compile)
+  #:use-module (rnrs bytevectors)
+  #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-4))
 
 (define exception:decoding-error
   (cons 'decoding-error "input (locale conversion|decoding) error"))
       (equal? lst
               (bytevector->u8-list (u8-list->bytevector lst)))))
 
+  (pass-if-exception "u8-list->bytevector [invalid argument type]"
+      exception:wrong-type-arg
+    (u8-list->bytevector 'not-a-list))
+
+  (pass-if-exception "u8-list->bytevector [circular list]"
+      exception:wrong-type-arg
+    (u8-list->bytevector (circular-list 1 2 3)))
+
   (pass-if "bytevector-uint-{ref,set!} [small]"
     (let ((b (make-bytevector 15)))
       (bytevector-uint-set! b 0 #x1234
                            (bytevector-u8-set! bv 3 #xff)
                            bv)))
 
+  (pass-if-exception "sint-list->bytevector [invalid argument type]"
+      exception:wrong-type-arg
+    (sint-list->bytevector 'not-a-list (endianness big) 2))
+
+  (pass-if-exception "uint-list->bytevector [invalid argument type]"
+      exception:wrong-type-arg
+    (uint-list->bytevector 'not-a-list (endianness big) 2))
+
+  (pass-if-exception "sint-list->bytevector [circular list]"
+      exception:wrong-type-arg
+    (sint-list->bytevector (circular-list 1 2 3) (endianness big)
+                           2))
+
+  (pass-if-exception "uint-list->bytevector [circular list]"
+      exception:wrong-type-arg
+    (uint-list->bytevector (circular-list 1 2 3) (endianness big)
+                           2))
+
   (pass-if-exception "sint-list->bytevector [out-of-range]"
     exception:out-of-range
     (sint-list->bytevector (list 0 0 (expt 2 16)) (endianness big)