From b99480e1a72eb97355e27c06f2726773143ac5ca Mon Sep 17 00:00:00 2001 From: Reuben Thomas Date: Tue, 16 Jan 2018 11:07:44 +0000 Subject: [PATCH] Recode.pyx: allow NULs in recoded data MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Expose recode_buffer_to_buffer rather than recode_string, and check return value. This makes some tests fail (see Debian bug #271939) Add a comment explaining why we prefer the buffer APIs, and don’t bother with string APIs at all. Also add a FIXME to expose file APIs. --- tests/Recode.pyx | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/Recode.pyx b/tests/Recode.pyx index eff60f8..9c559f4 100644 --- a/tests/Recode.pyx +++ b/tests/Recode.pyx @@ -443,11 +443,6 @@ cdef extern from "common.h": bool recode_scan_request(RECODE_REQUEST, char *) bool recode_format_table( RECODE_REQUEST, recode_programming_language, char *) - char *recode_string(RECODE_CONST_REQUEST, char *) - bool recode_string_to_buffer( - RECODE_CONST_REQUEST, char *, char **, size_t *, size_t *) - bool recode_string_to_file( - RECODE_CONST_REQUEST, char *, FILE *) bool recode_buffer_to_buffer( RECODE_CONST_REQUEST, char *, size_t, char **, size_t *, size_t *) bool recode_buffer_to_file( @@ -648,19 +643,33 @@ cdef class Request: if not ok: raise error - def string(self, char *text): - cdef char *result - result = recode_string(self.request, text) - if result is NULL: + def string(self, text): + cdef char *input = text + cdef size_t input_len = len(text) + cdef char *output = NULL + cdef size_t output_len + cdef size_t output_allocated = 0 + result = recode_buffer_to_buffer(self.request, input, input_len, &output, &output_len, &output_allocated) + if result is False or output is NULL: raise error - return result + return output[:output_len] + # Unexposed APIs: + + # Don't expose recode_string; always check return value + # + #char *recode_string( + # RECODE_CONST_REQUEST, char *) + + # Prefer buffer APIs, which allow NUL characters. + # #bool recode_string_to_buffer( # RECODE_CONST_REQUEST, char *, char **, size_t *, size_t *) #bool recode_string_to_file( # RECODE_CONST_REQUEST, char *, FILE *) - #bool recode_buffer_to_buffer( - # RECODE_CONST_REQUEST, char *, size_t, char **, size_t *, size_t *) + + # FIXME: Expose these? + # #bool recode_buffer_to_file( # RECODE_CONST_REQUEST, char *, size_t, FILE *) #bool recode_file_to_buffer( -- 2.40.0