Commit ec5b82d6
ec5b82d62e47Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~1 modified · -2 deleted
Description
Revert "Make mutable String buffers binary-safe (preserve embedded NUL)"
This reverts commit b2e92ea8005c2aae65086e28c0414373ea5c5d87.
Details
- Commit
- ec5b82d62e47
- Parent
- 951d8bb8a048
lib/sp_runtime.hmodifiedView file
@@ -485,7 +485,7 @@
485485
static inline size_t sp_str_byte_len(const char *s) {486486
if (!s) return 0;487487
unsigned char marker = ((const unsigned char *)s)[-1];488-
if (marker == 0xfe || marker == 0xfc || marker == 0xfd) {488+
if (marker == 0xfe || marker == 0xfc) {489489
return (((const sp_str_hdr *)(s - 1)) - 1)->len;490490
}491491
return strlen(s);@@ -494,7 +494,7 @@
494494
static inline void sp_str_set_len(char *s, size_t len) {495495
if (!s) return;496496
unsigned char marker = ((unsigned char *)s)[-1];497-
if (marker == 0xfe || marker == 0xfc || marker == 0xfd) {497+
if (marker == 0xfe || marker == 0xfc) {498498
sp_str_hdr *hd = ((sp_str_hdr *)(s - 1)) - 1;499499
hd->len = (uint32_t)len;500500
hd->hash = 0; /* length change implies content change: invalidate cached hash */@@ -2024,30 +2024,7 @@
20242024
strings stay frozen via the 0xff marker byte (see sp_str_setbyte). */20252025
static inline mrb_bool sp_String_is_frozen(sp_String*s){if(!s)return TRUE;sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));return h->frozen;}20262026
static inline sp_String*sp_String_freeze(sp_String*s){if(s){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));h->frozen=1;}return s;}2027-
/* A mutable String's payload buffer carries the same length-bearing header as2028-
a 0xfe/0xfc heap string, so an escaped `const char*` (sp_String_cstr) is2029-
binary-safe: sp_str_byte_len reads the header rather than strlen, which would2030-
truncate at an embedded NUL (matz/spinel#1479). Raw malloc'd block layout:2031-
[sp_str_hdr][0xfd marker][data ...][NUL]. s->data points at `data`, so2032-
s->data[-1] == 0xfd and the header sits just below the marker. */2033-
#define SP_FD_HDR (sizeof(sp_str_hdr) + 1) /* header + marker byte */2034-
#define SP_FD_OVH (sizeof(sp_str_hdr) + 2) /* header + marker + NUL terminator */2035-
static inline char *sp_fd_base(const char *data){return (char*)data - SP_FD_HDR;}2036-
/* Lay out header + marker in a freshly (re)allocated raw block; return data. */2037-
static inline char *sp_fd_setup(char *raw, int64_t len){2038-
sp_str_hdr *hd = (sp_str_hdr *)raw;2039-
hd->next = NULL; hd->size = 0; hd->len = (uint32_t)len; hd->hash = 0;2040-
char *body = (char *)(hd + 1);2041-
body[0] = (char)0xfd;2042-
return body + 1;2043-
}2044-
/* Publish s->len into the buffer header so readers of the escaped pointer2045-
(sp_str_byte_len) see the true byte length; invalidate the cached hash. */2046-
static inline void sp_fd_publish(sp_String *s){2047-
sp_str_hdr *hd = (sp_str_hdr *)sp_fd_base(s->data);2048-
hd->len = (uint32_t)s->len; hd->hash = 0;2049-
}2050-
static void sp_String_fin(void*p){free(sp_fd_base(((sp_String*)p)->data));}2027+
static void sp_String_fin(void*p){free(((sp_String*)p)->data-1);}20512028
static sp_String*sp_String_new(const char*s){20522029
/* Copy s's payload into a raw-malloc'd buffer BEFORE sp_gc_alloc.20532030
If s is a heap string (sp_str_alloc) whose only liveness anchor@@ -2058,26 +2035,27 @@
20582035
allocator reuses freed regions faster). The malloc'd buffer is20592036
not on the GC heap and not on the string heap, so it survives20602037
any GC inside sp_gc_alloc. */2061-
int64_t len=(int64_t)sp_str_byte_len(s);2038+
int64_t len=(int64_t)strlen(s);20622039
int64_t cap=len*2+16;2063-
char*raw=(char*)malloc(SP_FD_OVH+cap);2064-
char*data=sp_fd_setup(raw,len);2065-
memcpy(data,s,len);data[len]=0;2040+
char*raw=(char*)malloc(cap+2);2041+
raw[0]=(char)0xfd;2042+
char*data=raw+1;2043+
memcpy(data,s,len+1);20662044
sp_String*r=(sp_String*)sp_gc_alloc(sizeof(sp_String),sp_String_fin,NULL);20672045
r->len=len;r->cap=cap;r->data=data;2068-
{sp_gc_hdr*h=(sp_gc_hdr*)((char*)r-sizeof(sp_gc_hdr));h->size+=r->cap+SP_FD_OVH;sp_gc_bytes+=r->cap+SP_FD_OVH;}2046+
{sp_gc_hdr*h=(sp_gc_hdr*)((char*)r-sizeof(sp_gc_hdr));h->size+=r->cap+2;sp_gc_bytes+=r->cap+2;}20692047
return r;20702048
}20712049
/* Issue #757: realloc on growth used to overwrite s->data unconditionally,20722050
leaking the old buffer + null-dereferencing if realloc fails. Now we20732051
check the result and bail without mutating on failure. */2074-
static inline void sp_String_append(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)sp_str_byte_len(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(sp_fd_base(s->data),SP_FD_OVH+new_cap);if(!raw)return;sp_gc_bytes-=s->cap+SP_FD_OVH;h->size-=s->cap+SP_FD_OVH;s->cap=new_cap;s->data=sp_fd_setup(raw,0);h->size+=s->cap+SP_FD_OVH;sp_gc_bytes+=s->cap+SP_FD_OVH;}memcpy(s->data+s->len,t,tl);s->len+=tl;s->data[s->len]=0;sp_fd_publish(s);}2075-
static inline void sp_String_prepend(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)sp_str_byte_len(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(sp_fd_base(s->data),SP_FD_OVH+new_cap);if(!raw)return;sp_gc_bytes-=s->cap+SP_FD_OVH;h->size-=s->cap+SP_FD_OVH;s->cap=new_cap;s->data=sp_fd_setup(raw,0);h->size+=s->cap+SP_FD_OVH;sp_gc_bytes+=s->cap+SP_FD_OVH;}memmove(s->data+tl,s->data,s->len+1);memcpy(s->data,t,tl);s->len+=tl;sp_fd_publish(s);}2052+
static inline void sp_String_append(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memcpy(s->data+s->len,t,tl+1);s->len+=tl;}2053+
static inline void sp_String_prepend(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memmove(s->data+tl,s->data,s->len+1);memcpy(s->data,t,tl);s->len+=tl;}20762054
/* Issue #741: String#insert(idx, str) -- insert str at idx. Negative20772055
idx is relative to len+1 (insert before tail). */2078-
static inline void sp_String_insert(sp_String*s,int64_t idx,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)sp_str_byte_len(t);if(tl==0)return;if(idx<0)idx+=s->len+1;if(idx<0)idx=0;if(idx>s->len)idx=s->len;if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(sp_fd_base(s->data),SP_FD_OVH+new_cap);if(!raw)return;sp_gc_bytes-=s->cap+SP_FD_OVH;h->size-=s->cap+SP_FD_OVH;s->cap=new_cap;s->data=sp_fd_setup(raw,0);h->size+=s->cap+SP_FD_OVH;sp_gc_bytes+=s->cap+SP_FD_OVH;}memmove(s->data+idx+tl,s->data+idx,s->len-idx+1);memcpy(s->data+idx,t,tl);s->len+=tl;sp_fd_publish(s);}2056+
static inline void sp_String_insert(sp_String*s,int64_t idx,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(tl==0)return;if(idx<0)idx+=s->len+1;if(idx<0)idx=0;if(idx>s->len)idx=s->len;if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memmove(s->data+idx+tl,s->data+idx,s->len-idx+1);memcpy(s->data+idx,t,tl);s->len+=tl;}20792057
/* Issue #740/#741 sibling: String#replace(s) -- replace entire content. */2080-
static inline void sp_String_replace(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)sp_str_byte_len(t);if(tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=tl*2+16;char*raw=(char*)realloc(sp_fd_base(s->data),SP_FD_OVH+new_cap);if(!raw)return;sp_gc_bytes-=s->cap+SP_FD_OVH;h->size-=s->cap+SP_FD_OVH;s->cap=new_cap;s->data=sp_fd_setup(raw,0);h->size+=s->cap+SP_FD_OVH;sp_gc_bytes+=s->cap+SP_FD_OVH;}memcpy(s->data,t,tl);s->data[tl]='\0';s->len=tl;sp_fd_publish(s);}2058+
static inline void sp_String_replace(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=tl*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memcpy(s->data,t,tl);s->data[tl]='\0';s->len=tl;}20812059
static inline const char*sp_String_cstr(sp_String*s){return s->data;}20822060
static inline int64_t sp_String_length(sp_String*s){return s->len;}20832061
static sp_String*sp_String_dup(sp_String*s){return sp_String_new(s->data);}test/string_append_binary_safe.rbdeleted
Loading diff…
test/string_append_binary_safe.rb.expecteddeleted
Loading diff…