Skip to content

Commit

Permalink
[ruby/prism] check_string() should always return a valid C string
Browse files Browse the repository at this point in the history
* Otherwise it is invalid e.g. to call strlen() to the result,
  or to assume the argument was a string.
* All callers are already checking for nil before.

ruby/prism@8197be883e
  • Loading branch information
eregon authored and matzbot committed Sep 24, 2024
1 parent 5228d34 commit b8baf3f
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,11 @@ ID rb_id_source_for;
/******************************************************************************/

/**
* Check if the given VALUE is a string. If it's nil, then return NULL. If it's
* not a string, then raise a type error. Otherwise return the VALUE as a C
* string.
* Check if the given VALUE is a string. If it's not a string, then raise a
* TypeError. Otherwise return the VALUE as a C string.
*/
static const char *
check_string(VALUE value) {
// If the value is nil, then we don't need to do anything.
if (NIL_P(value)) {
return NULL;
}

// Check if the value is a string. If it's not, then raise a type error.
if (!RB_TYPE_P(value, T_STRING)) {
rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (expected String)", rb_obj_class(value));
Expand Down

0 comments on commit b8baf3f

Please sign in to comment.