Commit 951d8bb8
951d8bb8a048Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Make mutable String buffers binary-safe (preserve embedded NUL)
String#<< (and prepend/insert/replace) dropped embedded NUL bytes, so a mutable string built in place was not binary-safe -- breaking WebSocket frame emission whose extended 2-byte length header carries a 0x00 high byte for 126..255-byte payloads (Action Cable confirm_subscription, Turbo broadcasts). Two compounding sites:
- Write side: the in-place mutators sized the appended operand with strlen(t), appending nothing when t began with 0x00.
- Read side: a mutable buffer (marker 0xfd) carried its length only in the sp_String struct, so once it escaped as a const char* (concat, FFI byte-writes, inspect, length) sp_str_byte_len fell back to strlen and truncated at the first embedded NUL.
Give the 0xfd buffer the same length-bearing sp_str_hdr header that 0xfe/0xfc heap strings use ([hdr][0xfd][data][NUL]); sp_str_byte_len now reads it, so an escaped pointer is self-describing for every reader. The mutators size operands with sp_str_byte_len and publish s->len into the header on each mutation. sp_String_new also copies its source with sp_str_byte_len, so String.new(binstr) is binary-safe too.
955 test / 57 bench / optcarrot 59662 (721 fps) green.
Closes #1479
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 951d8bb8a048
- Parent
- 274bec5cc630
lib/sp_runtime.hmodifiedView file
static inline size_t sp_str_byte_len(const char *s) { if (!s) return 0; unsigned char marker = ((const unsigned char *)s)[-1]; if (marker == 0xfe || marker == 0xfc) { if (marker == 0xfe || marker == 0xfc || marker == 0xfd) { return (((const sp_str_hdr *)(s - 1)) - 1)->len; } return strlen(s);static inline void sp_str_set_len(char *s, size_t len) { if (!s) return; unsigned char marker = ((unsigned char *)s)[-1]; if (marker == 0xfe || marker == 0xfc) { if (marker == 0xfe || marker == 0xfc || marker == 0xfd) { sp_str_hdr *hd = ((sp_str_hdr *)(s - 1)) - 1; hd->len = (uint32_t)len; hd->hash = 0; /* length change implies content change: invalidate cached hash */ strings stay frozen via the 0xff marker byte (see sp_str_setbyte). */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;}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;}static void sp_String_fin(void*p){free(((sp_String*)p)->data-1);}/* A mutable String's payload buffer carries the same length-bearing header as a 0xfe/0xfc heap string, so an escaped `const char*` (sp_String_cstr) is binary-safe: sp_str_byte_len reads the header rather than strlen, which would truncate at an embedded NUL (matz/spinel#1479). Raw malloc'd block layout: [sp_str_hdr][0xfd marker][data ...][NUL]. s->data points at `data`, so s->data[-1] == 0xfd and the header sits just below the marker. */#define SP_FD_HDR (sizeof(sp_str_hdr) + 1) /* header + marker byte */#define SP_FD_OVH (sizeof(sp_str_hdr) + 2) /* header + marker + NUL terminator */static inline char *sp_fd_base(const char *data){return (char*)data - SP_FD_HDR;}/* Lay out header + marker in a freshly (re)allocated raw block; return data. */static inline char *sp_fd_setup(char *raw, int64_t len){ sp_str_hdr *hd = (sp_str_hdr *)raw; hd->next = NULL; hd->size = 0; hd->len = (uint32_t)len; hd->hash = 0; char *body = (char *)(hd + 1); body[0] = (char)0xfd; return body + 1;}/* Publish s->len into the buffer header so readers of the escaped pointer (sp_str_byte_len) see the true byte length; invalidate the cached hash. */static inline void sp_fd_publish(sp_String *s){ sp_str_hdr *hd = (sp_str_hdr *)sp_fd_base(s->data); hd->len = (uint32_t)s->len; hd->hash = 0;}static void sp_String_fin(void*p){free(sp_fd_base(((sp_String*)p)->data));}static sp_String*sp_String_new(const char*s){ /* Copy s's payload into a raw-malloc'd buffer BEFORE sp_gc_alloc. If s is a heap string (sp_str_alloc) whose only liveness anchor allocator reuses freed regions faster). The malloc'd buffer is not on the GC heap and not on the string heap, so it survives any GC inside sp_gc_alloc. */ int64_t len=(int64_t)strlen(s); int64_t len=(int64_t)sp_str_byte_len(s); int64_t cap=len*2+16; char*raw=(char*)malloc(cap+2); raw[0]=(char)0xfd; char*data=raw+1; memcpy(data,s,len+1); char*raw=(char*)malloc(SP_FD_OVH+cap); char*data=sp_fd_setup(raw,len); memcpy(data,s,len);data[len]=0; sp_String*r=(sp_String*)sp_gc_alloc(sizeof(sp_String),sp_String_fin,NULL); r->len=len;r->cap=cap;r->data=data; {sp_gc_hdr*h=(sp_gc_hdr*)((char*)r-sizeof(sp_gc_hdr));h->size+=r->cap+2;sp_gc_bytes+=r->cap+2;} {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;} return r;}/* Issue #757: realloc on growth used to overwrite s->data unconditionally, leaking the old buffer + null-dereferencing if realloc fails. Now we check the result and bail without mutating on failure. */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;}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;}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);}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);}/* Issue #741: String#insert(idx, str) -- insert str at idx. Negative idx is relative to len+1 (insert before tail). */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;}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);}/* Issue #740/#741 sibling: String#replace(s) -- replace entire content. */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;}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);}static inline const char*sp_String_cstr(sp_String*s){return s->data;}static inline int64_t sp_String_length(sp_String*s){return s->len;}static sp_String*sp_String_dup(sp_String*s){return sp_String_new(s->data);}test/string_append_binary_safe.rbaddedView file
Loading diff…
test/string_append_binary_safe.rb.expectedaddedView file
Loading diff…