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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.