From 8ea9cc713f52fd0a1357aa5cded97ceb234b669a Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 15 Jan 2019 06:57:16 -0800 Subject: ffi: bugfix: char array shouldn't null terminate. * ffi.c (ffi_char_array_put): The char array put operation should only null terminate when the null_term flag is set; i.e. it's a zarray type. The bug here is that when a Lisp string of length > N is put into an (array N char), the C array gets null terminated, which is wrong. Only in the case when the string is exactly of length N is there no null termination. In all cases when the length >= N, we want truncation without null termination. --- ffi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'ffi.c') diff --git a/ffi.c b/ffi.c index fc21c7de..b11f0d42 100644 --- a/ffi.c +++ b/ffi.c @@ -2258,9 +2258,11 @@ static void ffi_char_array_put(struct txr_ffi_type *tft, val str, mem_t *dst, } else { char *u8str = utf8_dup_to(wstr); memcpy(dst, u8str, nelem); - dst[nelem - 1] = 0; free(u8str); } + + if (nt) + dst[nelem - 1] = 0; } static val ffi_wchar_array_get(struct txr_ffi_type *tft, mem_t *src, -- cgit v1.2.3