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

@@ -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) {
488+ if (marker == 0xfe || marker == 0xfc || marker == 0xfd) {
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) {
497+ if (marker == 0xfe || marker == 0xfc || marker == 0xfd) {
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,7 +2024,30 @@
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-static void sp_String_fin(void*p){free(((sp_String*)p)->data-1);}
2027+/* A mutable String's payload buffer carries the same length-bearing header as
2028+ a 0xfe/0xfc heap string, so an escaped `const char*` (sp_String_cstr) is
2029+ binary-safe: sp_str_byte_len reads the header rather than strlen, which would
2030+ 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`, so
2032+ 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 pointer
2045+ (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));}
20282051 static sp_String*sp_String_new(const char*s){
20292052 /* Copy s's payload into a raw-malloc'd buffer BEFORE sp_gc_alloc.
20302053 If s is a heap string (sp_str_alloc) whose only liveness anchor
@@ -2035,27 +2058,26 @@
20352058 allocator reuses freed regions faster). The malloc'd buffer is
20362059 not on the GC heap and not on the string heap, so it survives
20372060 any GC inside sp_gc_alloc. */
2038- int64_t len=(int64_t)strlen(s);
2061+ int64_t len=(int64_t)sp_str_byte_len(s);
20392062 int64_t cap=len*2+16;
2040- char*raw=(char*)malloc(cap+2);
2041- raw[0]=(char)0xfd;
2042- char*data=raw+1;
2043- memcpy(data,s,len+1);
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;
20442066 sp_String*r=(sp_String*)sp_gc_alloc(sizeof(sp_String),sp_String_fin,NULL);
20452067 r->len=len;r->cap=cap;r->data=data;
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;}
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;}
20472069 return r;
20482070 }
20492071 /* Issue #757: realloc on growth used to overwrite s->data unconditionally,
20502072 leaking the old buffer + null-dereferencing if realloc fails. Now we
20512073 check the result and bail without mutating on failure. */
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;}
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);}
20542076 /* Issue #741: String#insert(idx, str) -- insert str at idx. Negative
20552077 idx is relative to len+1 (insert before tail). */
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;}
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);}
20572079 /* Issue #740/#741 sibling: String#replace(s) -- replace entire content. */
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;}
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);}
20592081 static inline const char*sp_String_cstr(sp_String*s){return s->data;}
20602082 static inline int64_t sp_String_length(sp_String*s){return s->len;}
20612083 static sp_String*sp_String_dup(sp_String*s){return sp_String_new(s->data);}