Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby C extension rb_str_new2 seems to return false
    text
    copied!<p>I cannot post my actual code due to work copyright, so I will try to show my problem with simple example code.</p> <p>I have a C extension whose simplified version looks like:</p> <pre><code>#include &lt;ruby.h&gt; #include &lt;termios.h&gt; #include &lt;stdio.h&gt; #include &lt;fcntl.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include &lt;string.h&gt; VALUE test(VALUE self, VALUE string); void Init_module_name() { module_name = rb_define_module("Modulename"); c_modulename = rb_define_class_under(modulename, "Class", rb_cObject); rb_define_method(c_modulename, "test", test, 1); e_ModuleNameError = rb_define_class_under(modulename, "Error", rb_eStandardError); } VALUE test(VALUE self, VALUE string) { char *c_string = StringValueCStr(string); int fd = open(c_string, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd == -1) { rb_raise(e_ModuleNameError, "Failed to open file"); } if (!isatty(fd)) { rb_raise(e_ModuleNameError, "File is not a tty"); } struct termios config; int termios_ret = init_termios(config, fd) if (termios_ret != OK) { // OK defined by enum in modulename's header close(fd); rb_raise(e_ModuleNameError, "Termios init failed."); } int success = write(fd, "I'm a string", str_length); if (success &lt; str_length) { close(fd); rb_raise(e_ModuleNameError, "Failed to write to file."); } close(fd); return rb_str_new2("Success"); } </code></pre> <p>Then, the ruby code that requires this looks like:</p> <pre><code>require 'modulename' class ModuleName attr_acessor :file def initialize(file) @file = file @object = Modulename::Class.new end def test @object.test @file end end </code></pre> <p>Which is then called in my production project like:</p> <pre><code>require "modulename_ruby_file" x = ModuleName "/dev/pts/1" x.test </code></pre> <p>Here is the interesting thing. When I run this code in production, the return value from x.test above is false (As in literally the value false, not a string). Also, the write to the file never happens. However, if I do it in some simplified test code it returns the string "Success" just like expected and the write is indeed completed.</p> <p>Does anyone know of any situation that would cause this function not to execute the write, and return false? I already tried putting a rescue around it in case it was throwing one of the rb_raises, but it doesn't appear to be.</p> <p>Me and 3 other members of my team have looked at this all afternoon and have not found an answer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload