mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Avoid use of snprintf() when DISABLE_STDIO is set; fix #3632
ref #3492 #3515 #3517
This commit is contained in:
@@ -1024,9 +1024,9 @@ gen_assignment(codegen_scope *s, node *tree, int sp, int val)
|
||||
|
||||
default:
|
||||
#ifndef MRB_DISABLE_STDIO
|
||||
printf("unknown lhs %d\n", type);
|
||||
fprintf(stderr, "unknown lhs %d\n", type);
|
||||
#endif
|
||||
break;
|
||||
return;
|
||||
}
|
||||
if (val) push();
|
||||
}
|
||||
@@ -2195,11 +2195,11 @@ codegen(codegen_scope *s, node *tree, int val)
|
||||
case NODE_NTH_REF:
|
||||
if (val) {
|
||||
mrb_state *mrb = s->mrb;
|
||||
char buf[32];
|
||||
mrb_value str;
|
||||
int sym;
|
||||
|
||||
snprintf(buf, sizeof(buf), "$%" MRB_PRId, (mrb_int)(intptr_t)tree);
|
||||
sym = new_sym(s, mrb_intern_cstr(mrb, buf));
|
||||
str = mrb_format(mrb, "$%S", mrb_fixnum_value((mrb_int)(intptr_t)tree));
|
||||
sym = new_sym(s, mrb_intern_str(mrb, str));
|
||||
genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));
|
||||
push();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <mruby/irep.h>
|
||||
#include <mruby/proc.h>
|
||||
#include <mruby/opcode.h>
|
||||
#include <mruby/error.h>
|
||||
|
||||
mrb_value mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p);
|
||||
mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self);
|
||||
@@ -175,12 +176,14 @@ create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, con
|
||||
|
||||
if (0 < p->nerr) {
|
||||
/* parse error */
|
||||
char buf[256];
|
||||
int n;
|
||||
n = snprintf(buf, sizeof(buf), "line %d: %s\n", p->error_buffer[0].lineno, p->error_buffer[0].message);
|
||||
mrb_value str;
|
||||
|
||||
str = mrb_format(mrb, "line %S: %S",
|
||||
mrb_fixnum_value(p->error_buffer[0].lineno),
|
||||
mrb_str_new_cstr(mrb, p->error_buffer[0].message));
|
||||
mrb_parser_free(p);
|
||||
mrbc_context_free(mrb, cxt);
|
||||
mrb_exc_raise(mrb, mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, n));
|
||||
mrb_exc_raise(mrb, mrb_exc_new_str(mrb, E_SYNTAX_ERROR, str));
|
||||
}
|
||||
|
||||
proc = mrb_generate_code(mrb, p);
|
||||
|
||||
@@ -52,10 +52,7 @@ mrb_proc_inspect(mrb_state *mrb, mrb_value self)
|
||||
|
||||
line = mrb_debug_get_line(irep, 0);
|
||||
if (line != -1) {
|
||||
char buf[32];
|
||||
|
||||
snprintf(buf, sizeof(buf), "%" PRId32, line);
|
||||
mrb_str_cat_cstr(mrb, str, buf);
|
||||
str = mrb_format(mrb, "%S:%S", str, mrb_fixnum_value(line));
|
||||
}
|
||||
else {
|
||||
mrb_str_cat_lit(mrb, str, "-");
|
||||
|
||||
@@ -5,12 +5,17 @@
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <mruby.h>
|
||||
#include <mruby/class.h>
|
||||
#include <mruby/data.h>
|
||||
|
||||
#ifndef DISABLE_STDIO
|
||||
#include <stdio.h>
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#define NDIV(x,y) (-(-((x)+1)/(y))-1)
|
||||
|
||||
#if _MSC_VER < 1800
|
||||
@@ -55,6 +60,13 @@ double round(double x) {
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* asctime(3) */
|
||||
/* mruby usually use its own implementation of struct tm to string conversion */
|
||||
/* except when DISABLE_STDIO is set. In that case, it uses asctime() or asctime_r(). */
|
||||
/* By default mruby tries to use asctime_r() which is reentrant. */
|
||||
/* Undef following macro on platforms that does not have asctime_r(). */
|
||||
/* #define NO_ASCTIME_R */
|
||||
|
||||
/* timegm(3) */
|
||||
/* mktime() creates tm structure for localtime; timegm() is for UTC time */
|
||||
/* define following macro to use probably faster timegm() on the platform */
|
||||
@@ -166,6 +178,7 @@ static const mrb_timezone_name timezone_names[] = {
|
||||
{ "LOCAL", sizeof("LOCAL") - 1 },
|
||||
};
|
||||
|
||||
#ifndef DISABLE_STDIO
|
||||
static const char mon_names[12][4] = {
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
||||
};
|
||||
@@ -173,6 +186,7 @@ static const char mon_names[12][4] = {
|
||||
static const char wday_names[7][4] = {
|
||||
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
|
||||
};
|
||||
#endif
|
||||
|
||||
struct mrb_time {
|
||||
time_t sec;
|
||||
@@ -528,18 +542,28 @@ mrb_time_zone(mrb_state *mrb, mrb_value self)
|
||||
static mrb_value
|
||||
mrb_time_asctime(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct mrb_time *tm;
|
||||
struct tm *d;
|
||||
char buf[256];
|
||||
struct mrb_time *tm = time_get_ptr(mrb, self);
|
||||
struct tm *d = &tm->datetime;
|
||||
int len;
|
||||
|
||||
tm = time_get_ptr(mrb, self);
|
||||
d = &tm->datetime;
|
||||
#if defined(DISABLE_STDIO)
|
||||
char *s;
|
||||
# ifdef NO_ASCTIME_R
|
||||
s = asctime(d);
|
||||
# else
|
||||
char buf[32];
|
||||
s = asctime_r(d, buf);
|
||||
# endif
|
||||
len = strlen(s)-1; /* truncate the last newline */
|
||||
#else
|
||||
char buf[256];
|
||||
|
||||
len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d",
|
||||
wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
|
||||
d->tm_hour, d->tm_min, d->tm_sec,
|
||||
tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "",
|
||||
d->tm_year + 1900);
|
||||
#endif
|
||||
return mrb_str_new(mrb, buf, len);
|
||||
}
|
||||
|
||||
|
||||
+4
-7
@@ -74,14 +74,12 @@ static void
|
||||
get_backtrace_i(mrb_state *mrb, struct backtrace_location *loc, void *data)
|
||||
{
|
||||
mrb_value ary, str;
|
||||
char buf[32];
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
|
||||
ary = mrb_obj_value((struct RArray*)data);
|
||||
|
||||
str = mrb_str_new_cstr(mrb, loc->filename);
|
||||
snprintf(buf, sizeof(buf), ":%d", loc->lineno);
|
||||
mrb_str_cat_cstr(mrb, str, buf);
|
||||
str = mrb_format(mrb, "%S:%S", str, mrb_fixnum_value(loc->lineno));
|
||||
|
||||
if (loc->method) {
|
||||
mrb_str_cat_lit(mrb, str, ":in ");
|
||||
@@ -400,14 +398,13 @@ mrb_restore_backtrace(mrb_state *mrb)
|
||||
int ai;
|
||||
mrb_backtrace_entry *entry;
|
||||
mrb_value mrb_entry;
|
||||
char buf[32];
|
||||
|
||||
ai = mrb_gc_arena_save(mrb);
|
||||
entry = &(mrb->backtrace.entries[i]);
|
||||
|
||||
mrb_entry = mrb_str_new_cstr(mrb, entry->filename);
|
||||
snprintf(buf, sizeof(buf), ":%d", entry->lineno);
|
||||
mrb_str_cat_cstr(mrb, mrb_entry, buf);
|
||||
mrb_entry = mrb_format(mrb, "%S:%S",
|
||||
mrb_str_new_cstr(mrb, entry->filename),
|
||||
mrb_fixnum_value(entry->lineno));
|
||||
if (entry->method_id != 0) {
|
||||
mrb_str_cat_lit(mrb, mrb_entry, ":in ");
|
||||
|
||||
|
||||
+8
-17
@@ -137,6 +137,7 @@ exc_inspect(mrb_state *mrb, mrb_value exc)
|
||||
{
|
||||
mrb_value str, mesg, file, line;
|
||||
mrb_bool append_mesg;
|
||||
const char *cname;
|
||||
|
||||
mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
|
||||
file = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "file"));
|
||||
@@ -148,28 +149,18 @@ exc_inspect(mrb_state *mrb, mrb_value exc)
|
||||
append_mesg = RSTRING_LEN(mesg) > 0;
|
||||
}
|
||||
|
||||
cname = mrb_obj_classname(mrb, exc);
|
||||
str = mrb_str_new_cstr(mrb, cname);
|
||||
if (mrb_string_p(file) && mrb_fixnum_p(line)) {
|
||||
char buf[32];
|
||||
|
||||
str = mrb_str_dup(mrb, file);
|
||||
snprintf(buf, sizeof(buf), ":%" MRB_PRId ": ", mrb_fixnum(line));
|
||||
mrb_str_cat_cstr(mrb, str, buf);
|
||||
if (append_mesg) {
|
||||
mrb_str_cat_str(mrb, str, mesg);
|
||||
mrb_str_cat_lit(mrb, str, " (");
|
||||
str = mrb_format(mrb, "%S:%S:%S (%S)", file, line, mesg, str);
|
||||
}
|
||||
mrb_str_cat_cstr(mrb, str, mrb_obj_classname(mrb, exc));
|
||||
if (append_mesg) {
|
||||
mrb_str_cat_lit(mrb, str, ")");
|
||||
else {
|
||||
str = mrb_format(mrb, "%S:%S:%S", file, line, str);
|
||||
}
|
||||
}
|
||||
else {
|
||||
const char *cname = mrb_obj_classname(mrb, exc);
|
||||
str = mrb_str_new_cstr(mrb, cname);
|
||||
if (append_mesg) {
|
||||
mrb_str_cat_lit(mrb, str, ": ");
|
||||
mrb_str_cat_str(mrb, str, mesg);
|
||||
}
|
||||
else if (append_mesg) {
|
||||
str = mrb_format(mrb, "%S:%S", str, mesg);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user