Skip to content

Commit

Permalink
fix _bytes/nul-terminated for NULL results
Browse files Browse the repository at this point in the history
  • Loading branch information
mflatt committed Jan 2, 2020
1 parent e97639e commit 8ae53d9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pkgs/racket-test-core/tests/racket/foreign-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ typedef unsigned char byte;
#define X
#endif

X void* return_null() { return NULL; }

X int add1_int_int (int x) { return x + 1; }
X int add1_byte_int (byte x) { return x + 1; }
X byte add1_int_byte (int x) { return x + 1; }
Expand Down
5 changes: 5 additions & 0 deletions pkgs/racket-test-core/tests/racket/foreign-test.rktl
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,11 @@
free)])
(free p)))

(let ([return_null (get-ffi-obj 'return_null test-lib (_fun -> _bytes/nul-terminated))])
(test #f return_null))
(let ([return_null (get-ffi-obj 'return_null test-lib (_fun -> (_bytes/nul-terminated o 20)))])
(test #f return_null))

;; Test equality and hashing of c pointers:
(let ([seventeen1 (cast 17 _intptr _pointer)]
[seventeen2 (cast 17 _intptr _pointer)]
Expand Down
9 changes: 5 additions & 4 deletions racket/collects/ffi/unsafe.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -1161,16 +1161,17 @@
(define _bytes/nul-terminated
(make-ctype _bytes
(lambda (bstr) (and bstr (bytes-append bstr #"\0")))
(lambda (bstr) (bytes-copy bstr))))
(lambda (bstr) (and bstr (bytes-copy bstr)))))
(provide (rename-out [_bytes/nul-terminated* _bytes/nul-terminated]))
(define-fun-syntax _bytes/nul-terminated*
(syntax-id-rules (o)
[(_ o n) (type: _pointer
pre: (make-bytes n)
;; post is needed when this is used as a function output type
post: (x => (let ([s (make-bytes n)])
(memcpy s x n)
s)))]
post: (x => (and x
(let ([s (make-bytes n)])
(memcpy s x n)
s))))]
[(_ . xs) (_bytes/nul-teriminated . xs)]
[_ _bytes/nul-terminated]))

Expand Down
2 changes: 2 additions & 0 deletions racket/src/racket/src/salloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3020,6 +3020,8 @@ Scheme_Object *scheme_dump_gc_stats(int c, Scheme_Object *p[])
# endif
#endif

scheme_console_printf("JIT-generated code: %ld\n", scheme_code_page_total);

#if MZ_PRECISE_GC_TRACE
if (for_each_struct) {
scheme_console_printf("Begin Struct\n");
Expand Down

0 comments on commit 8ae53d9

Please sign in to comment.