Changeset 39995 in project


Ignore:
Timestamp:
04/13/21 20:46:50 (5 weeks ago)
Author:
Kon Lovett
Message:

check for buffer overflow (module was supposed to be unsafe but bad idea), add test

Location:
release/5/string-utils/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • release/5/string-utils/trunk/string-hexadecimal.scm

    r38937 r39995  
    55
    66(declare
    7   (bound-to-procedure
    8     ##sys#signal-hook
    9     ##sys#make-string))
     7  (bound-to-procedure ##sys#signal-hook ##sys#make-string))
    108
    119(module string-hexadecimal
  • release/5/string-utils/trunk/string-utils.egg

    r39927 r39995  
    33
    44((synopsis "String Utilities")
    5  (version "2.5.1")
     5 (version "2.5.2")
    66 (category data)
    77 (author "Kon Lovett")
  • release/5/string-utils/trunk/tests/string-utils-test.scm

    r39956 r39995  
    4141
    4242(test-group "To Hex"
    43         (let ((t (make-string (* 2 3))))
    44                 (blob_to_hex t (string->blob "12abc34") 2 5)
     43
     44  (let* ((len 3) (t (make-string (* 2 len))))
     45                (test-error (begin (blob_to_hex t (string->blob "12abc34") 2 (+ 2 len)) t)) )
     46
     47        (let* ((len 3) (t (make-string (* 2 len))))
     48                (blob_to_hex t (string->blob "12abc34") 2 len)
    4549                (test "616263" t) )
    4650  ;
    47         (let ((t (make-string (* 2 3))))
    48                 (u8vec_to_hex t (u8vector 1 2 #x61 #x62 #x63 3 4) 2 5)
     51        (let* ((len 3) (t (make-string (* 2 len))))
     52                (u8vec_to_hex t (u8vector 1 2 #x61 #x62 #x63 3 4) 2 len)
    4953                (test "616263" t) )
    5054  ;
    51         (let ((t (make-string (* 2 2))))
    52                 (s8vec_to_hex t (s8vector 1 2 -45 -54 3 4) 2 4)
     55        (let* ((len 2) (t (make-string (* 2 len))))
     56                (s8vec_to_hex t (s8vector 1 2 -45 -54 3 4) 2 len)
    5357                (test "d3ca" t) )
    5458)
  • release/5/string-utils/trunk/to-hex.scm

    r39956 r39995  
    33;;;; Kon Lovett, Aug '17
    44;;;; Kon Lovett, Aug '10
     5
     6(declare
     7  (bound-to-procedure ##sys#signal-hook))
    58
    69#>
     
    5558(import (chicken type))
    5659(import (chicken foreign))
     60(import (chicken fixnum))
     61(import (chicken blob))
    5762
    5863;;
     
    9297;;
    9398
     99(define (check-string-buffer loc obj len)
     100  (unless (string? obj)
     101    (##sys#signal-hook #:type-error loc "bad argument type - not a string" obj) )
     102  (unless (<= len (string-length obj))
     103    (##sys#signal-hook #:bounds-error loc "insufficient string length for buffer" obj len) )
     104  obj )
     105
     106(define (check-blob-buffer loc obj len)
     107  (unless (blob? obj)
     108    (##sys#signal-hook #:type-error loc "bad argument type - not a blob" obj) )
     109  (unless (<= len (blob-size obj))
     110    (##sys#signal-hook #:bounds-error loc "insufficient blob size for buffer" obj len) )
     111  obj )
     112
     113;;
     114
    94115(define (str_to_hex out in off len)
    95   (C_str_to_hex out in off len)
     116  (C_str_to_hex (check-string-buffer 'str_to_hex out (fx* len 2)) in off len)
    96117  out )
    97118
    98119(define (blob_to_hex out in off len)
    99   (C_blob_to_hex out in off len)
     120  (C_blob_to_hex (check-string-buffer 'blob_to_hex out (fx* len 2)) in off len)
    100121  out )
    101122
    102123(define (u8vec_to_hex out in off len)
    103   (C_u8vec_to_hex out in off len)
     124  (C_u8vec_to_hex (check-string-buffer 'u8vec_to_hex out (fx* len 2)) in off len)
    104125  out )
    105126
    106127(define (s8vec_to_hex out in off len)
    107   (C_s8vec_to_hex out in off len)
     128  (C_s8vec_to_hex (check-string-buffer 's8vec_to_hex out (fx* len 2)) in off len)
    108129  out )
    109130
    110131(define (mem_to_hex out in off len)
    111   (C_mem_to_hex out in off len)
     132  (C_mem_to_hex (check-string-buffer 'mem_to_hex out (fx* len 2)) in off len)
    112133  out )
    113134
     
    115136
    116137(define (hex_to_str out in off len)
    117   (C_hex_to_str out in off len)
     138  (C_hex_to_str (check-string-buffer 'hex_to_str out (fx/ len 2)) in off len)
    118139  out )
    119140
    120141(define (hex_to_blob out in off len)
    121   (C_hex_to_str out in off len)
     142  (C_hex_to_str (check-blob-buffer 'hex_to_blob out (fx/ len 2)) in off len)
    122143  out )
    123144
Note: See TracChangeset for help on using the changeset viewer.