Commit 12c892bd
12c892bd494aMerged
ydah <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(runtime): make File.write binary-safe
Details
- Commit
- 12c892bd494a
- Parent
- 8e604f0a7ef9
lib/sp_runtime.hmodifiedView file
@@ -185,7 +185,7 @@
185185
static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 256*1024;186186
187187
/* ---- String GC ---- */188-
typedef struct sp_str_hdr { struct sp_str_hdr *next; size_t size; } sp_str_hdr;188+
typedef struct sp_str_hdr { struct sp_str_hdr *next; size_t size; size_t len; } sp_str_hdr;189189
static sp_str_hdr *sp_str_heap = NULL;190190
#define SPL(s) (&("\xff" s)[1])191191
static const char sp_str_empty_data[] = "\xff";@@ -196,6 +196,7 @@
196196
sp_str_hdr *h = (sp_str_hdr *)malloc(total);197197
h->next = sp_str_heap;198198
h->size = total;199+
h->len = len;199200
sp_str_heap = h;200201
/* Don't fold string-heap pressure into sp_gc_bytes : the201202
threshold heuristic in sp_gc_alloc is keyed on heap survivors, and@@ -222,6 +223,38 @@
222223
return sp_str_alloc(total_with_null > 0 ? total_with_null - 1 : 0);223224
}224225
226+
static inline size_t sp_str_byte_len(const char *s) {227+
if (!s) return 0;228+
unsigned char marker = ((const unsigned char *)s)[-1];229+
if (marker == 0xfe || marker == 0xfc) {230+
const char *body = s - 1;231+
const sp_str_hdr *h = sp_str_heap;232+
while (h) {233+
if ((const char *)(h + 1) == body) {234+
return h->len;235+
}236+
h = h->next;237+
}238+
}239+
return strlen(s);240+
}241+
242+
static inline void sp_str_set_len(char *s, size_t len) {243+
if (!s) return;244+
unsigned char marker = ((unsigned char *)s)[-1];245+
if (marker == 0xfe || marker == 0xfc) {246+
char *body = s - 1;247+
sp_str_hdr *h = sp_str_heap;248+
while (h) {249+
if ((char *)(h + 1) == body) {250+
h->len = len;251+
return;252+
}253+
h = h->next;254+
}255+
}256+
}257+
225258
static const char *sp_str_dup_external(const char *s) {226259
if (!s) return NULL;227260
size_t n = strlen(s);@@ -685,13 +718,13 @@
685718
static mrb_int sp_str_field_count(const char*s,const char*sep){686719
if(*s==0)return 0;size_t sl=strlen(sep);if(sl==0)return(mrb_int)strlen(s);687720
mrb_int c=1;const char*p=s;while((p=strstr(p,sep))!=NULL){c++;p+=sl;}return c;}688-
static const char*sp_str_concat(const char*a,const char*b){size_t la=strlen(a),lb=strlen(b);char*r=sp_str_alloc(la+lb);memcpy(r,a,la);memcpy(r+la,b,lb);return r;}689-
static const char*sp_str_concat3(const char*a,const char*b,const char*c){size_t la=strlen(a),lb=strlen(b),lc=strlen(c);char*r=sp_str_alloc(la+lb+lc);memcpy(r,a,la);memcpy(r+la,b,lb);memcpy(r+la+lb,c,lc);return r;}690-
static const char*sp_str_concat4(const char*a,const char*b,const char*c,const char*d){size_t la=strlen(a),lb=strlen(b),lc=strlen(c),ld=strlen(d);char*r=sp_str_alloc(la+lb+lc+ld);memcpy(r,a,la);memcpy(r+la,b,lb);memcpy(r+la+lb,c,lc);memcpy(r+la+lb+lc,d,ld);return r;}721+
static const char*sp_str_concat(const char*a,const char*b){size_t la=sp_str_byte_len(a),lb=sp_str_byte_len(b);char*r=sp_str_alloc(la+lb);memcpy(r,a,la);memcpy(r+la,b,lb);return r;}722+
static const char*sp_str_concat3(const char*a,const char*b,const char*c){size_t la=sp_str_byte_len(a),lb=sp_str_byte_len(b),lc=sp_str_byte_len(c);char*r=sp_str_alloc(la+lb+lc);memcpy(r,a,la);memcpy(r+la,b,lb);memcpy(r+la+lb,c,lc);return r;}723+
static const char*sp_str_concat4(const char*a,const char*b,const char*c,const char*d){size_t la=sp_str_byte_len(a),lb=sp_str_byte_len(b),lc=sp_str_byte_len(c),ld=sp_str_byte_len(d);char*r=sp_str_alloc(la+lb+lc+ld);memcpy(r,a,la);memcpy(r+la,b,lb);memcpy(r+la+lb,c,lc);memcpy(r+la+lb+lc,d,ld);return r;}691724
/* Concatenate N strings into a single GC-managed buffer. */692-
static const char*sp_str_concat_arr(const char *const *parts,int n){size_t total=0;for(int i=0;i<n;i++)total+=strlen(parts[i]);char*r=sp_str_alloc(total);char*p=r;for(int i=0;i<n;i++){size_t sl=strlen(parts[i]);memcpy(p,parts[i],sl);p+=sl;}return r;}693-
static const char*sp_int_to_s(mrb_int n){char*b=sp_str_alloc_raw(32);snprintf(b,32,"%lld",(long long)n);return b;}694-
static const char*sp_int_to_s_base(mrb_int n,mrb_int base){if(base<2||base>36)base=10;char*b=sp_str_alloc_raw(72);char tmp[72];int i=0;int neg=0;uint64_t u;if(n<0){neg=1;u=(uint64_t)(-(n+1))+1;}else{u=(uint64_t)n;}if(u==0){tmp[i++]='0';}else{while(u>0){mrb_int d=u%base;tmp[i++]=d<10?'0'+d:'a'+d-10;u/=base;}}int j=0;if(neg)b[j++]='-';while(i>0)b[j++]=tmp[--i];b[j]=0;return b;}725+
static const char*sp_str_concat_arr(const char *const *parts,int n){size_t total=0;for(int i=0;i<n;i++)total+=sp_str_byte_len(parts[i]);char*r=sp_str_alloc(total);char*p=r;for(int i=0;i<n;i++){size_t sl=sp_str_byte_len(parts[i]);memcpy(p,parts[i],sl);p+=sl;}return r;}726+
static const char*sp_int_to_s(mrb_int n){char*b=sp_str_alloc_raw(32);int len=snprintf(b,32,"%lld",(long long)n);if(len<0)len=0;sp_str_set_len(b,(size_t)len);return b;}727+
static const char*sp_int_to_s_base(mrb_int n,mrb_int base){if(base<2||base>36)base=10;char*b=sp_str_alloc_raw(72);char tmp[72];int i=0;int neg=0;uint64_t u;if(n<0){neg=1;u=(uint64_t)(-(n+1))+1;}else{u=(uint64_t)n;}if(u==0){tmp[i++]='0';}else{while(u>0){mrb_int d=u%base;tmp[i++]=d<10?'0'+d:'a'+d-10;u/=base;}}int j=0;if(neg)b[j++]='-';while(i>0)b[j++]=tmp[--i];b[j]=0;sp_str_set_len(b,(size_t)j);return b;}695728
/* Float#to_s (Ruby semantics): produce the shortest decimal that696729
round-trips back to the same double, formatted per CRuby — fixed697730
point when the decimal exponent is in [-4, 15], scientific@@ -722,12 +755,12 @@
722755
if(e>=0)out[o++]='+';else{out[o++]='-';e=-e;}723756
if(e<10){out[o++]='0';out[o++]=(char)('0'+e);}else o+=snprintf(out+o,16,"%d",e);724757
}725-
out[o]=0;return out;758+
out[o]=0;sp_str_set_len(out,(size_t)o);return out;726759
}727760
#define sp_float_inspect sp_float_to_s728761
/* String#inspect: wrap in double quotes and escape \, ", \n, \t, \r,729762
plus any non-printable byte as \xNN. Output is always ASCII-safe. */730-
static const char*sp_str_inspect(const char*s){if(!s){char*r=sp_str_alloc_raw(4);r[0]='n';r[1]='i';r[2]='l';r[3]=0;return r;}size_t sl=strlen(s);size_t cap=sl*4+3;char*r=sp_str_alloc_raw(cap);size_t o=0;r[o++]='"';for(size_t i=0;i<sl;i++){unsigned char c=(unsigned char)s[i];if(c=='\\'||c=='"'){r[o++]='\\';r[o++]=c;}else if(c=='\n'){r[o++]='\\';r[o++]='n';}else if(c=='\t'){r[o++]='\\';r[o++]='t';}else if(c=='\r'){r[o++]='\\';r[o++]='r';}else if(c<0x20||c==0x7f){snprintf(r+o,5,"\\x%02X",c);o+=4;}else{r[o++]=(char)c;}}r[o++]='"';r[o]=0;return r;}763+
static const char*sp_str_inspect(const char*s){if(!s){char*r=sp_str_alloc_raw(4);r[0]='n';r[1]='i';r[2]='l';r[3]=0;return r;}size_t sl=strlen(s);size_t cap=sl*4+3;char*r=sp_str_alloc_raw(cap);size_t o=0;r[o++]='"';for(size_t i=0;i<sl;i++){unsigned char c=(unsigned char)s[i];if(c=='\\'||c=='"'){r[o++]='\\';r[o++]=c;}else if(c=='\n'){r[o++]='\\';r[o++]='n';}else if(c=='\t'){r[o++]='\\';r[o++]='t';}else if(c=='\r'){r[o++]='\\';r[o++]='r';}else if(c<0x20||c==0x7f){snprintf(r+o,5,"\\x%02X",c);o+=4;}else{r[o++]=(char)c;}}r[o++]='"';r[o]=0;sp_str_set_len(r,o);return r;}731764
static const char*sp_str_upcase(const char*s){size_t l=strlen(s);char*r=sp_str_alloc_raw(l+1);for(size_t i=0;i<=l;i++)r[i]=toupper((unsigned char)s[i]);return r;}732765
static const char*sp_str_downcase(const char*s){size_t l=strlen(s);char*r=sp_str_alloc_raw(l+1);for(size_t i=0;i<=l;i++)r[i]=tolower((unsigned char)s[i]);return r;}733766
static const char*sp_str_swapcase(const char*s){size_t l=strlen(s);char*r=sp_str_alloc_raw(l+1);for(size_t i=0;i<=l;i++){unsigned char c=(unsigned char)s[i];if(isupper(c))r[i]=tolower(c);else if(islower(c))r[i]=toupper(c);else r[i]=s[i];}return r;}@@ -1461,7 +1494,7 @@
14611494
null-termination because text mode shrinks the byte count below14621495
ftell's raw-file size. */14631496
static const char *sp_file_read(const char *path) { FILE *f = fopen(path, "r"); if (!f) return &("\xff" "")[1]; fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET); char *buf = sp_str_alloc(sz); size_t n = 0; if (sz > 0) { n = fread(buf, 1, sz, f); } buf[n] = 0; fclose(f); return buf; }1464-
static void sp_file_write(const char *path, const char *data) { FILE *f = fopen(path, "w"); if (f) { fputs(data, f); fclose(f); } }1497+
static void sp_file_write(const char *path, const char *data) { FILE *f = fopen(path, "wb"); if (f) { fwrite(data, 1, sp_str_byte_len(data), f); fclose(f); } }14651498
static mrb_bool sp_file_exist(const char *path) { FILE *f = fopen(path, "r"); if (f) { fclose(f); return TRUE; } return FALSE; }14661499
static void sp_file_delete(const char *path) { remove(path); }14671500
static const char *sp_backtick(const char *cmd) { FILE *p = popen(cmd, "r"); if (!p) return sp_str_empty; char *buf = sp_str_alloc_raw(4096); size_t n = fread(buf, 1, 4095, p); buf[n] = 0; pclose(p); return buf; }test/file_write_binary_string.rbaddedView file
Loading diff…
test/file_write_binary_string.rb.expectedaddedView file
Loading diff…