mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
add mrb_strlen_lit which makes _lit macros safer
strlen(3) + string literal is usually optimized but strlen(3) doesn't check if its argument is really a string literal. This is important for mruby's _static functions to which some _lit macros are expanded (string literals have static storage). See comment for some additional info. remove unnecessary parentheses
This commit is contained in:
+10
-2
@@ -231,13 +231,21 @@ struct RClass * mrb_define_module_under(mrb_state *mrb, struct RClass *outer, co
|
||||
|
||||
int mrb_get_args(mrb_state *mrb, const char *format, ...);
|
||||
|
||||
/* `strlen` for character string literals (use with caution or `strlen` instead)
|
||||
Adjacent string literals are concatenated in C/C++ in translation phase 6.
|
||||
If `lit` is not one, the compiler will report a syntax error:
|
||||
MSVC: "error C2143: syntax error : missing ')' before 'string'"
|
||||
GCC: "error: expected ')' before string constant"
|
||||
*/
|
||||
#define mrb_strlen_lit(lit) (sizeof(lit "") - 1)
|
||||
|
||||
mrb_value mrb_funcall(mrb_state*, mrb_value, const char*, int,...);
|
||||
mrb_value mrb_funcall_argv(mrb_state*, mrb_value, mrb_sym, int, mrb_value*);
|
||||
mrb_value mrb_funcall_with_block(mrb_state*, mrb_value, mrb_sym, int, mrb_value*, mrb_value);
|
||||
mrb_sym mrb_intern_cstr(mrb_state*,const char*);
|
||||
mrb_sym mrb_intern(mrb_state*,const char*,size_t);
|
||||
mrb_sym mrb_intern_static(mrb_state*,const char*,size_t);
|
||||
#define mrb_intern_lit(mrb, lit) mrb_intern_static(mrb, (lit), sizeof(lit) - 1)
|
||||
#define mrb_intern_lit(mrb, lit) mrb_intern_static(mrb, lit, mrb_strlen_lit(lit))
|
||||
mrb_sym mrb_intern_str(mrb_state*,mrb_value);
|
||||
mrb_value mrb_check_intern_cstr(mrb_state*,const char*);
|
||||
mrb_value mrb_check_intern(mrb_state*,const char*,size_t);
|
||||
@@ -257,7 +265,7 @@ void mrb_free(mrb_state*, void*);
|
||||
mrb_value mrb_str_new(mrb_state *mrb, const char *p, size_t len);
|
||||
mrb_value mrb_str_new_cstr(mrb_state*, const char*);
|
||||
mrb_value mrb_str_new_static(mrb_state *mrb, const char *p, size_t len);
|
||||
#define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), sizeof(lit) - 1)
|
||||
#define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit))
|
||||
|
||||
mrb_state* mrb_open(void);
|
||||
mrb_state* mrb_open_allocf(mrb_allocf, void *ud);
|
||||
|
||||
@@ -13,7 +13,7 @@ extern "C" {
|
||||
|
||||
void mrb_sys_fail(mrb_state *mrb, const char *mesg);
|
||||
mrb_value mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value str);
|
||||
#define mrb_exc_new_str_lit(mrb, c, lit) mrb_exc_new_str(mrb, c, mrb_str_new_lit(mrb, (lit)))
|
||||
#define mrb_exc_new_str_lit(mrb, c, lit) mrb_exc_new_str(mrb, c, mrb_str_new_lit(mrb, lit))
|
||||
mrb_value mrb_make_exception(mrb_state *mrb, int argc, mrb_value *argv);
|
||||
mrb_value mrb_format(mrb_state *mrb, const char *format, ...);
|
||||
void mrb_exc_print(mrb_state *mrb, struct RObject *exc);
|
||||
|
||||
@@ -83,7 +83,7 @@ mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2);
|
||||
mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str);
|
||||
mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
|
||||
mrb_value mrb_str_cat_cstr(mrb_state *mrb, mrb_value str, const char *ptr);
|
||||
#define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, (lit), sizeof(lit) - 1)
|
||||
#define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, lit, mrb_strlen_lit(lit))
|
||||
mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2);
|
||||
|
||||
int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
|
||||
|
||||
Reference in New Issue
Block a user