Skip to content

Instantly share code, notes, and snippets.

Created February 11, 2013 20:45
Show Gist options
  • Save anonymous/4757461 to your computer and use it in GitHub Desktop.
Save anonymous/4757461 to your computer and use it in GitHub Desktop.
c extension with ruby issue
I have a C extension that uses StringValueCStr to convert a passed-in ruby argument into a c-str like so:
VALUE login(VALUE self, VALUE rb_user, VALUE rb_password) {
char *user = StringValueCStr(rb_user);
char *passsword = StringValueCStr(rb_password);
{other stuff using these}
}
VALUE cmd(VALUE self, VALUE rb_user, VALUE rb_cmd) {
char *user = StringValueCStr(rb_user);
char *cmd = StringValueCStr(rb_cmd);
}
I call this from ruby like:
login "blah", "1234"
cmd "blah, "ls"
This works fine.
However, it is preferred to have a variable already set up like:
USER = "blah"
PASS = "1234"
login USER, PASS
cmd USER, "ls"
When I try that I get:
test.rb:11:in `send_cmd': string contains null byte (ArgumentError)
So it seems that the null byte add by StringValueCStr is sticking around and causing problems when I call it a second time in cmd.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment