From 8bd015f04a63b254d8e27b7b0cb2ac9e267a8d2f Mon Sep 17 00:00:00 2001 From: Nelson Ferreira Date: Fri, 20 Jan 2012 12:28:05 -0500 Subject: [PATCH] Fix additional transcoding assertions * src/buffer.h (TO_EXTERNAL_FORMAT): Remove the bad assert (ie not valid in all conditions). * src/buffer.h (TO_INTERNAL_FORMAT): Ditto. * src/buffer.h (DFC_SOURCE_DATA_TO_ARGS): Do the proper assert to the source conversion. * src/buffer.h (DFC_SOURCE_C_STRING_TO_ARGS): Ditto. * src/buffer.h (DFC_SOURCE_LISP_OPAQUE_TO_ARGS): Ditto. * src/buffer.h (DFC_ALLOCA_USE_CONVERTED_DATA): Do the proper assert to the converted data. * src/buffer.h (DFC_MALLOC_USE_CONVERTED_DATA): Ditto. * src/buffer.h (DFC_C_STRING_ALLOCA_USE_CONVERTED_DATA): Ditto. * src/buffer.h (DFC_C_STRING_MALLOC_USE_CONVERTED_DATA): Ditto. * src/buffer.h (DFC_LISP_STRING_USE_CONVERTED_DATA): Ditto. * src/buffer.h (DFC_LISP_OPAQUE_USE_CONVERTED_DATA): Ditto. * src/alloc.c (ALLOCATE_FIXED_TYPE): Make sure to detect failed allocations. * src/alloc.c (ALLOCATE_ATOMIC_FIXED_TYPE): Ditto. * src/alloc.c (make_uninit_string): Make sure the uninit string is valid after construction. * src/opaque.c (make_opaque): Make sure to detect failed allocations even in the fatal crash scenario. Signed-off-by: Nelson Ferreira --- src/alloc.c | 25 ++++++++++++++++--------- src/buffer.h | 30 ++++++++++++++++++++++-------- src/opaque.c | 29 +++++++++++++++++------------ 3 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/alloc.c b/src/alloc.c index 55af853..6e1e860 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -837,11 +837,13 @@ static int gc_count_num_##type##_freelist #define ALLOCATE_FIXED_TYPE(type, structtype, result) \ do { \ result = xnew(structtype); \ + assert(result != NULL); \ INCREMENT_CONS_COUNTER(sizeof(structtype), #type); \ } while (0) #define ALLOCATE_ATOMIC_FIXED_TYPE(type, structtype, result) \ do { \ result = xnew_atomic(structtype); \ + assert(result != NULL); \ INCREMENT_CONS_COUNTER(sizeof(structtype), #type); \ } while (0) @@ -2892,7 +2894,7 @@ allocate_string_chars_struct(Lisp_String *string_it_goes_with, Lisp_Object make_uninit_string(Bytecount length) { - Lisp_String *s; + Lisp_String *s = NULL; #if !defined HAVE_BDWGC || !defined EF_USE_BDWGC EMACS_INT fullsize = STRING_FULLSIZE(length); #endif /* !BDWGC */ @@ -2907,17 +2909,22 @@ Lisp_Object make_uninit_string(Bytecount length) set_lheader_implementation(&s->lheader, &lrecord_string); string_register_finaliser(s); -#if defined HAVE_BDWGC && defined EF_USE_BDWGC { - Bufbyte *foo = xnew_atomic_array(Bufbyte, length+1); - set_string_data(s, foo); - } + Bufbyte *foo = NULL; +#if defined HAVE_BDWGC && defined EF_USE_BDWGC + foo = xnew_atomic_array(Bufbyte, length+1); + assert(foo != NULL); #else - set_string_data(s, BIG_STRING_FULLSIZE_P(fullsize) - ? xnew_atomic_array(Bufbyte, length + 1) - : allocate_string_chars_struct(s, fullsize)->chars); + if (BIG_STRING_FULLSIZE_P(fullsize)) { + foo = xnew_atomic_array(Bufbyte, length + 1); + assert(foo != NULL); + } else { + foo = allocate_string_chars_struct(s, fullsize)->chars; + assert(foo != NULL); + } #endif - + set_string_data(s, foo); + } set_string_length(s, length); s->plist = Qnil; #ifdef EF_USE_COMPRE diff --git a/src/buffer.h b/src/buffer.h index 64ad7f8..0492730 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -1099,7 +1099,6 @@ do { \ dfc_simplified_sink_type, &dfc_sink); \ \ DFC_##sink_type##_USE_CONVERTED_DATA (sink); \ - assert((sink)!=NULL); \ } while (0) #define TO_INTERNAL_FORMAT(source_type, source, sink_type, sink, coding_system) \ @@ -1131,7 +1130,6 @@ do { \ dfc_simplified_sink_type, &dfc_sink); \ \ DFC_##sink_type##_USE_CONVERTED_DATA (sink); \ - assert((sink)!=NULL); \ } while (0) #ifdef FILE_CODING @@ -1195,12 +1193,15 @@ dfc_convert_to_internal_format(dfc_conversion_type source_type, do { \ dfc_source.data.ptr = DFC_CPP_CAR val; \ dfc_source.data.len = DFC_CPP_CDR val; \ + assert(dfc_source.data.ptr != NULL); \ dfc_simplified_source_type = DFC_TYPE_DATA; \ } while (0) #define DFC_SOURCE_C_STRING_TO_ARGS(val) \ do { \ - dfc_source.data.len = \ - strlen((const char*)(dfc_source.data.ptr = (val))); \ + dfc_source.data.ptr = (val); \ + assert(dfc_source.data.ptr != NULL); \ + dfc_source.data.len = strlen((const char*) \ + (dfc_source.data.ptr)); \ dfc_simplified_source_type = DFC_TYPE_DATA; \ } while (0) #define DFC_SOURCE_LISP_STRING_TO_ARGS(val) \ @@ -1222,6 +1223,7 @@ dfc_convert_to_internal_format(dfc_conversion_type source_type, Lisp_Opaque *dfc_slota = XOPAQUE (val); \ dfc_source.data.ptr = OPAQUE_DATA (dfc_slota); \ dfc_source.data.len = OPAQUE_SIZE (dfc_slota); \ + assert(dfc_source.data.ptr != NULL); \ dfc_simplified_source_type = DFC_TYPE_DATA; \ } while (0) @@ -1261,6 +1263,7 @@ typedef union { } *dfc_aliasing_voidpp; #define DFC_ALLOCA_USE_CONVERTED_DATA(sink) do { \ void *dfc_sink_ret = alloca(dfc_sink.data.len + 1); \ + assert(dfc_sink_ret != NULL); \ memcpy(dfc_sink_ret, dfc_sink.data.ptr, \ dfc_sink.data.len + 1); \ ((dfc_aliasing_voidpp)&(DFC_CPP_CAR sink))->p = \ @@ -1270,6 +1273,7 @@ typedef union { #define DFC_MALLOC_USE_CONVERTED_DATA(sink) \ do { \ void *dfc_sink_ret = xmalloc_atomic(dfc_sink.data.len + 1); \ + assert(dfc_sink_ret != NULL); \ memcpy(dfc_sink_ret, dfc_sink.data.ptr, \ dfc_sink.data.len + 1); \ ((dfc_aliasing_voidpp)&(DFC_CPP_CAR sink))->p = \ @@ -1279,6 +1283,7 @@ typedef union { #define DFC_C_STRING_ALLOCA_USE_CONVERTED_DATA(sink) \ do { \ void *dfc_sink_ret = alloca (dfc_sink.data.len + 1); \ + assert(dfc_sink_ret != NULL); \ memcpy(dfc_sink_ret, dfc_sink.data.ptr, \ dfc_sink.data.len + 1); \ (sink) = dfc_sink_ret; \ @@ -1286,15 +1291,24 @@ typedef union { #define DFC_C_STRING_MALLOC_USE_CONVERTED_DATA(sink) \ do { \ void *dfc_sink_ret = xmalloc_atomic(dfc_sink.data.len + 1); \ + assert(dfc_sink_ret != NULL); \ memcpy(dfc_sink_ret, dfc_sink.data.ptr, \ dfc_sink.data.len + 1); \ (sink) = dfc_sink_ret; \ } while (0) #define DFC_LISP_STRING_USE_CONVERTED_DATA(sink) \ - sink = make_string( \ - (const Bufbyte*)dfc_sink.data.ptr, dfc_sink.data.len) -#define DFC_LISP_OPAQUE_USE_CONVERTED_DATA(sink) \ - sink = make_opaque(dfc_sink.data.ptr, dfc_sink.data.len) + do { \ + sink = make_string((const Bufbyte*)dfc_sink.data.ptr, \ + dfc_sink.data.len); \ + assert(!NILP(sink)); \ + } while (0) +#define DFC_LISP_OPAQUE_USE_CONVERTED_DATA(sink) \ + do { \ + sink = make_opaque(dfc_sink.data.ptr, \ + dfc_sink.data.len); \ + assert(!NILP(sink)); \ + } while (0) + #define DFC_LISP_LSTREAM_USE_CONVERTED_DATA(sink) /* data already used */ #define DFC_LISP_BUFFER_USE_CONVERTED_DATA(sink) \ Lstream_delete (XLSTREAM (dfc_sink.lisp_object)) diff --git a/src/opaque.c b/src/opaque.c index 1901c3a..80f6472 100644 --- a/src/opaque.c +++ b/src/opaque.c @@ -76,20 +76,25 @@ make_opaque(const void *data, size_t size) { Lisp_Opaque *p = (Lisp_Opaque *) alloc_lcrecord(aligned_sizeof_opaque(size), &lrecord_opaque); - p->size = size; - if (data == OPAQUE_CLEAR) - memset(p->data, '\0', size); - else if (data == OPAQUE_UNINIT) - DO_NOTHING; - else - memcpy(p->data, data, size); - - { - Lisp_Object val; - XSETOPAQUE(val, p); - return val; + assert(p!=NULL); + if(p != NULL) { + p->size = size; + + if (data == OPAQUE_CLEAR) + memset(p->data, '\0', size); + else if (data == OPAQUE_UNINIT) + DO_NOTHING; + else + memcpy(p->data, data, size); + + { + Lisp_Object val; + XSETOPAQUE(val, p); + return val; + } } + return Qnil; } /* This will not work correctly for opaques with subobjects! */ -- 2.34.1