Skip to content

Commit

Permalink
Assert for RTEST that Qnil and Qfalse differ just 1 bit
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Oct 19, 2022
1 parent 86096a9 commit 412e3c7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/ruby/internal/special_consts.h
Expand Up @@ -136,13 +136,22 @@ static inline bool
RB_TEST(VALUE obj)
{
/*
* if USE_FLONUM
* Qfalse: ....0000 0000
* Qnil: ....0000 1000
* ~Qnil: ....1111 0111
* v ....xxxx xxxx
* ----------------------------
* RTEST(v) ....xxxx 0xxx
*
* if ! USE_FLONUM
* Qfalse: ....0000 0000
* Qnil: ....0000 0100
* ~Qnil: ....1111 1011
* v ....xxxx xxxx
* ----------------------------
* RTEST(v) ....xxxx x0xx
*
* RTEST(v) can be 0 if and only if (v == Qfalse || v == Qnil).
*/
return obj & ~RUBY_Qnil;
Expand Down
3 changes: 3 additions & 0 deletions ruby.c
Expand Up @@ -62,6 +62,9 @@
#include "ruby/version.h"
#include "ruby/internal/error.h"

#define singlebit_only_p(x) !((x) & ((x)-1))
STATIC_ASSERT(Qnil_1bit_from_Qfalse, singlebit_only_p(Qnil^Qfalse));

#ifndef MAXPATHLEN
# define MAXPATHLEN 1024
#endif
Expand Down

0 comments on commit 412e3c7

Please sign in to comment.