From 47580e0d72f53c2fff23cb8edf1487da76e87744 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Thu, 31 Dec 2015 10:59:40 +0900 Subject: [PATCH] Avoid writing to purespace * src/alloc.c (Fmake_string): Don't write to empty string contents. (allocate_vector): Don't write to empty vector size. * src/character.h (CHECK_CHARACTER_CAR, CHECK_CHARACTER_CDR): Don't call unnecessary XSETCAR or XSETCDR. * src/lisp.h (STRING_SET_UNIBYTE, STRING_SET_MULTIBYTE): Don't write to empty string size_byte. --- src/alloc.c | 13 +++++++++---- src/character.h | 2 -- src/lisp.h | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/alloc.c b/src/alloc.c index fe55cde49c9..49f5b7f18bc 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -2119,8 +2119,11 @@ INIT must be an integer that represents a character. */) { nbytes = XINT (length); val = make_uninit_string (nbytes); - memset (SDATA (val), c, nbytes); - SDATA (val)[nbytes] = 0; + if (nbytes) + { + memset (SDATA (val), c, nbytes); + SDATA (val)[nbytes] = 0; + } } else { @@ -2145,7 +2148,8 @@ INIT must be an integer that represents a character. */) memcpy (p, beg, len); } } - *p = 0; + if (nbytes) + *p = 0; } return val; @@ -3188,7 +3192,8 @@ allocate_vector (EMACS_INT len) if (min ((nbytes_max - header_size) / word_size, MOST_POSITIVE_FIXNUM) < len) memory_full (SIZE_MAX); v = allocate_vectorlike (len); - v->header.size = len; + if (len) + v->header.size = len; return v; } diff --git a/src/character.h b/src/character.h index 871c1c3de95..440e78147d1 100644 --- a/src/character.h +++ b/src/character.h @@ -135,14 +135,12 @@ enum do { \ Lisp_Object tmp = XCAR (x); \ CHECK_CHARACTER (tmp); \ - XSETCAR ((x), tmp); \ } while (false) #define CHECK_CHARACTER_CDR(x) \ do { \ Lisp_Object tmp = XCDR (x); \ CHECK_CHARACTER (tmp); \ - XSETCDR ((x), tmp); \ } while (false) /* Nonzero iff C is a character of code less than 0x100. */ diff --git a/src/lisp.h b/src/lisp.h index 995760a5019..477521bbf4e 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -1325,7 +1325,7 @@ STRING_MULTIBYTE (Lisp_Object str) /* Mark STR as a unibyte string. */ #define STRING_SET_UNIBYTE(STR) \ do { \ - if (EQ (STR, empty_multibyte_string)) \ + if (XSTRING (STR)->size == 0) \ (STR) = empty_unibyte_string; \ else \ XSTRING (STR)->size_byte = -1; \ @@ -1335,7 +1335,7 @@ STRING_MULTIBYTE (Lisp_Object str) ASCII characters in advance. */ #define STRING_SET_MULTIBYTE(STR) \ do { \ - if (EQ (STR, empty_unibyte_string)) \ + if (XSTRING (STR)->size == 0) \ (STR) = empty_multibyte_string; \ else \ XSTRING (STR)->size_byte = XSTRING (STR)->size; \ -- 2.39.2