using calloc instead of malloc to guarantee FFI allocated objects are zero initialized

This commit is contained in:
Alex Shinn 2011-10-12 22:34:41 +09:00
parent a2fa55ec62
commit 28e75d50fe

View file

@ -703,9 +703,9 @@
((and (not (type-result? a)) (type-array a) (not (string-type? a))) ((and (not (type-result? a)) (type-array a) (not (string-type? a)))
(if (not (number? (type-array a))) (if (not (number? (type-array a)))
(cat " tmp" (type-index a) (cat " tmp" (type-index a)
" = (" (type-c-name (type-base a)) "*) malloc(" " = (" (type-c-name (type-base a)) "*) calloc("
"(sexp_unbox_fixnum(sexp_length(ctx, arg" (type-index a) "(sexp_unbox_fixnum(sexp_length(ctx, arg" (type-index a)
"))+1) * sizeof(tmp" (type-index a) "[0]));\n")) "))+1), sizeof(tmp" (type-index a) "[0]));\n"))
(cat " for (i=0, res=arg" (type-index a) (cat " for (i=0, res=arg" (type-index a)
"; sexp_pairp(res); res=sexp_cdr(res), i++) {\n" "; sexp_pairp(res); res=sexp_cdr(res), i++) {\n"
" tmp" (type-index a) "[i] = " " tmp" (type-index a) "[i] = "
@ -720,7 +720,7 @@
(not (type-auto-expand? a)) (not (type-auto-expand? a))
(or (not (type-array a)) (or (not (type-array a))
(not (integer? len)))) (not (integer? len))))
(cat " tmp" (type-index a) " = malloc(1 + " (cat " tmp" (type-index a) " = calloc(1, 1 + "
(if (and (symbol? len) (not (eq? len 'null))) (if (and (symbol? len) (not (eq? len 'null)))
(lambda () (cat (lambda () (scheme->c-converter 'unsigned-int len)) (lambda () (cat (lambda () (scheme->c-converter 'unsigned-int len))
"*sizeof(tmp" (type-index a) "[0])")) "*sizeof(tmp" (type-index a) "[0])"))
@ -852,7 +852,7 @@
" free(tmp" i ");\n")) " free(tmp" i ");\n"))
(cat " len" i " *= 2;\n" (cat " len" i " *= 2;\n"
" tmp" i " tmp" i
" = malloc(len" i "*sizeof(tmp" i "[0]));\n" " = calloc(len" i ", sizeof(tmp" i "[0]));\n"
" goto loop;\n"))))) " goto loop;\n")))))
(else (else
" res = SEXP_FALSE;\n")) " res = SEXP_FALSE;\n"))
@ -1091,7 +1091,7 @@
" res = sexp_alloc_tagged(ctx, sexp_sizeof(cpointer), sexp_type_tag(" " res = sexp_alloc_tagged(ctx, sexp_sizeof(cpointer), sexp_type_tag("
(type-id-name name) (type-id-name name)
"));\n" "));\n"
" r = sexp_cpointer_value(res) = malloc(sizeof(" " r = sexp_cpointer_value(res) = calloc(1, sizeof("
(or (type-struct-type name) "") " " (type-name name) "));\n" (or (type-struct-type name) "") " " (type-name name) "));\n"
" memset(r, 0, sizeof(" " memset(r, 0, sizeof("
(or (type-struct-type name) "") " " (type-name name) "));\n" (or (type-struct-type name) "") " " (type-name name) "));\n"
@ -1167,7 +1167,7 @@
*funcs*) *funcs*)
(cat "static char* sexp_concat_env_string (sexp x) {\n" (cat "static char* sexp_concat_env_string (sexp x) {\n"
" int klen=sexp_string_length(sexp_car(x)), vlen=sexp_string_length(sexp_cdr(x));\n" " int klen=sexp_string_length(sexp_car(x)), vlen=sexp_string_length(sexp_cdr(x));\n"
" char *res = (char*) malloc(klen+vlen+2);\n" " char *res = (char*) calloc(1, klen+vlen+2);\n"
" strncpy(res, sexp_string_data(sexp_car(x)), klen);\n" " strncpy(res, sexp_string_data(sexp_car(x)), klen);\n"
" res[sexp_string_length(sexp_car(x))] = '=';\n" " res[sexp_string_length(sexp_car(x))] = '=';\n"
" strncpy(res+sexp_string_length(sexp_car(x)), sexp_string_data(sexp_cdr(x)), vlen);\n" " strncpy(res+sexp_string_length(sexp_car(x)), sexp_string_data(sexp_cdr(x)), vlen);\n"