Commit 492d9aa0

492d9aa0a9c4Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(runtime): audit remaining sentinel-less empty-string returns

Follow-up to 92ca18c. Sweeps lib/sp_runtime.h for the same pattern: a bare "" literal landing in a context where the result may be stored in an SP_GC_ROOT-registered local and scanned by sp_gc_mark. The literal has no \xff sentinel before it, so the random byte that the linker places at obj[-1] determines whether the scan returns early (lucky, byte == 0xff/0xfe/0xfc) or falls through to write h->marked on rodata and segfault (unlucky).

Switched to sp_str_empty (= sp_str_empty_data + 1, a 2-byte [0xff, 0x00] global whose addr+1 is the canonical sentinel-prefixed empty string):

  • sp_StrArray_set: pad with sp_str_empty (was "")
  • sp_StrArray_insert: same
  • sp_str_field: return sp_str_empty when field not found (was "")
  • sp_last_exc_cls: initial value sp_str_empty (was "")
  • sp_poly_shl on StrArray: push sp_str_empty for non-STR rhs (was "")
  • sp_StringIO_string: return sp_str_empty for null buf (was "")

The sp_str_format_strarr internal "" fallback at line 666 stays — the pointer is only used for strlen / memcpy and never escapes the function or hits any GC machinery.

Validation: tests 400/0/0, bench 52/0/0, optcarrot checksum 59662.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
492d9aa0a9c4
Parent
5ddf33ec27f2

lib/sp_runtime.hmodifiedView file

@@ -490,7 +490,7 @@
490490 * length-clamping semantics as sp_IntArray_slice. Out-of-bounds start
491491 * returns an empty StrArray (we don't have a nullable form). */
492492 static sp_StrArray*sp_StrArray_slice(sp_StrArray*a,mrb_int start,mrb_int len){if(start<0)start+=a->len;if(start<0)start=0;sp_StrArray*b=sp_StrArray_new();if(start>=a->len||len<=0)return b;if(start+len>a->len)len=a->len-start;for(mrb_int i=0;i<len;i++)sp_StrArray_push(b,a->data[start+i]);return b;}
493-static inline void sp_StrArray_set(sp_StrArray*a,mrb_int i,const char*v){if(i<0)i+=a->len;while(i>=a->len)sp_StrArray_push(a,"");a->data[i]=v;}
493+static inline void sp_StrArray_set(sp_StrArray*a,mrb_int i,const char*v){if(i<0)i+=a->len;while(i>=a->len)sp_StrArray_push(a,sp_str_empty);a->data[i]=v;}
494494 static void sp_StrArray_reverse_bang(sp_StrArray*a){for(mrb_int i=0,j=a->len-1;i<j;i++,j--){const char*t=a->data[i];a->data[i]=a->data[j];a->data[j]=t;}}
495495 static void sp_StrArray_rotate_bang(sp_StrArray*a,mrb_int n){if(a->len<=0)return;n=((n%a->len)+a->len)%a->len;if(n==0)return;const char**tmp=(const char**)malloc(sizeof(const char*)*a->len);for(mrb_int i=0;i<a->len;i++)tmp[i]=a->data[(i+n)%a->len];for(mrb_int i=0;i<a->len;i++)a->data[i]=tmp[i];free(tmp);}
496496 static int _sp_str_cmp(const void*a,const void*b){return strcmp(*(const char*const*)a,*(const char*const*)b);}
@@ -505,7 +505,7 @@
505505 static sp_StrArray*sp_StrArray_compact(sp_StrArray*a){sp_StrArray*r=sp_StrArray_new();for(mrb_int i=0;i<a->len;i++)if(a->data[i]!=NULL)sp_StrArray_push(r,a->data[i]);return r;}
506506 static const char*sp_StrArray_delete_at(sp_StrArray*a,mrb_int i){if(i<0)i+=a->len;if(i<0||i>=a->len)return NULL;const char*v=a->data[i];for(mrb_int j=i;j<a->len-1;j++)a->data[j]=a->data[j+1];a->len--;return v;}
507507 static const char*sp_StrArray_delete(sp_StrArray*a,const char*v){mrb_int w=0;const char*found=NULL;for(mrb_int i=0;i<a->len;i++){if(strcmp(a->data[i],v)!=0){a->data[w]=a->data[i];w++;}else{found=a->data[i];}}a->len=w;return found;}
508-static void sp_StrArray_insert(sp_StrArray*a,mrb_int i,const char*v){if(i<0)i+=a->len+1;sp_StrArray_push(a,"");for(mrb_int j=a->len-1;j>i;j--)a->data[j]=a->data[j-1];a->data[i]=v;}
508+static void sp_StrArray_insert(sp_StrArray*a,mrb_int i,const char*v){if(i<0)i+=a->len+1;sp_StrArray_push(a,sp_str_empty);for(mrb_int j=a->len-1;j>i;j--)a->data[j]=a->data[j-1];a->data[i]=v;}
509509 static void sp_StrArray_shuffle_bang(sp_StrArray*a){for(mrb_int i=a->len-1;i>0;i--){mrb_int j=(mrb_int)(rand()%(i+1));const char*t=a->data[i];a->data[i]=a->data[j];a->data[j]=t;}}
510510 static sp_StrArray*sp_StrArray_shuffle(sp_StrArray*a){sp_StrArray*r=sp_StrArray_new();sp_StrArray_replace(r,a);sp_StrArray_shuffle_bang(r);return r;}
511511
@@ -566,8 +566,8 @@
566566 If the field doesn't exist, returns "". */
567567 static const char*sp_str_field(const char*s,const char*sep,mrb_int n){
568568 size_t sl=strlen(sep);mrb_int cur=0;const char*p=s;
569- if(sl==0)return("");
570- while(cur<n){const char*f=strstr(p,sep);if(!f)return("");p=f+sl;cur++;}
569+ if(sl==0)return sp_str_empty;
570+ while(cur<n){const char*f=strstr(p,sep);if(!f)return sp_str_empty;p=f+sl;cur++;}
571571 const char*end=strstr(p,sep);size_t len=end?((size_t)(end-p)):strlen(p);
572572 char*r=sp_str_alloc_raw(len+1);memcpy(r,p,len);r[len]=0;return r;}
573573 /* Count fields in s split by sep (without allocating). */
@@ -1121,7 +1121,7 @@
11211121 return a;
11221122 }
11231123 if (a.cls_id == SP_BUILTIN_STR_ARRAY) {
1124- sp_StrArray_push((sp_StrArray *)a.v.p, b.tag == SP_TAG_STR ? (const char *)b.v.p : "");
1124+ sp_StrArray_push((sp_StrArray *)a.v.p, b.tag == SP_TAG_STR ? (const char *)b.v.p : sp_str_empty);
11251125 return a;
11261126 }
11271127 }
@@ -1276,7 +1276,7 @@
12761276 static const char *sp_exc_msg[SP_EXC_STACK_MAX];
12771277 static volatile int sp_exc_top = 0;
12781278 static const char *sp_exc_cls[SP_EXC_STACK_MAX];
1279-static volatile const char *sp_last_exc_cls = "";
1279+static volatile const char *sp_last_exc_cls = sp_str_empty;
12801280 static void sp_raise_cls(const char *cls, const char *msg) { if (sp_exc_top > 0) { sp_exc_msg[sp_exc_top-1] = msg; sp_exc_cls[sp_exc_top-1] = cls; sp_last_exc_cls = cls; longjmp(sp_exc_stack[sp_exc_top-1], 1); } fprintf(stderr, "unhandled exception: %s\n", msg); exit(1); }
12811281 static void sp_raise(const char *msg) { sp_raise_cls("RuntimeError", msg); }
12821282
@@ -1412,7 +1412,7 @@
14121412 static int64_t sio_write(sp_StringIO *sio, const char *d, int64_t dl) { sio_grow(sio, dl); if (sio->pos > sio->len) memset(sio->buf + sio->len, 0, sio->pos - sio->len); memcpy(sio->buf + sio->pos, d, dl); sio->pos += dl; if (sio->pos > sio->len) sio->len = sio->pos; sio->buf[sio->len] = '\0'; return dl; }
14131413 static sp_StringIO *sp_StringIO_new(void) { sp_StringIO *s = (sp_StringIO *)calloc(1, sizeof(sp_StringIO)); s->buf = (char *)calloc(1, 64); s->cap = 63; return s; }
14141414 static sp_StringIO *sp_StringIO_new_s(const char *init) { sp_StringIO *s = (sp_StringIO *)calloc(1, sizeof(sp_StringIO)); int64_t l = (int64_t)strlen(init); int64_t c = l < 63 ? 63 : l; s->buf = (char*)malloc(c+1); memcpy(s->buf, init, l); s->buf[l]='\0'; s->len = l; s->cap = c; return s; }
1415-static const char *sp_StringIO_string(sp_StringIO *s) { return s->buf ? s->buf : ""; }
1415+static const char *sp_StringIO_string(sp_StringIO *s) { return s->buf ? s->buf : sp_str_empty; }
14161416 static int64_t sp_StringIO_pos(sp_StringIO *s) { return s->pos; }
14171417 static int64_t sp_StringIO_size(sp_StringIO *s) { return s->len; }
14181418 static int64_t sp_StringIO_write(sp_StringIO *s, const char *str) { return sio_write(s, str, (int64_t)strlen(str)); }