【发布时间】:2015-01-11 22:52:49
【问题描述】:
我正在尝试使用 C 函数扩展我的 Ruby 代码。 C 代码在没有警告的情况下编译。但是当我尝试运行 ruby 代码时,我遇到了分段错误:
我有这个 c 代码:
#include <ruby.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "nessie.h"
/* #define TRACE_INTERMEDIATE_VALUES */
/*
* The number of rounds of the internal dedicated block cipher.
*/
#define R 10
VALUE rb_mExample;
VALUE rb_cClass;
// ...
static char* displayHash(const unsigned char array[], int length){
int i, k;
char *str;
str = malloc(3 * length + 1);
if (str == NULL) {
return NULL;
}
k = 0;
str[0] = '\0';
for (i = 0; i < length; i++){
char hex[3];
if (i % 32 == 0){
str[k++] = ' ';
}
if (i % 8 == 0){
str[k++] = ' ';
}
snprintf(hex, sizeof(hex), "%02X", array[i]);
str[k++] = hex[0];
str[k++] = hex[1];
}
str[k] = '\0';
return str;
}
VALUE
print_string(VALUE class, VALUE *valor) {
struct NESSIEstruct w;
u8 digest[DIGESTBYTES];
int i;
for (i = 0; valor[i] != '\0'; i++);
int sizeo = i;
NESSIEinit(&w);
NESSIEadd((u8*)valor, 8*sizeo, &w);
NESSIEfinalize(&w, digest);
return (VALUE) displayHash(digest, DIGESTBYTES);
}
void
Init_example(){
rb_mExample = rb_define_module("Example");
rb_cClass = rb_define_class_under(rb_mExample, "Class", rb_cObject);
rb_define_method(rb_cClass, "print_string", print_string, 1);
}
还有这段 Ruby 代码:
require "example"
def print
e = Example::Class.new
e.print_string("ruby")
end
当我运行 ruby 代码时,我遇到了分段错误。
编辑:带有日志信息的要点
https://gist.github.com/psantos10/f07484afa26ce0e55181
我哪里失败了?我是 C 语言的新手。
编辑:
我将“print_string”更改为如下所示:
VALUE
print_string(VALUE class, VALUE *valor) {
struct NESSIEstruct w;
u8 digest[DIGESTBYTES];
int i;
for (i = 0; valor[i] != '\0'; i++);
int sizeo = i;
NESSIEinit(&w);
NESSIEadd((u8*)valor, 8*sizeo, &w);
NESSIEfinalize(&w, digest);
return rb_str_new(displayHash(digest, DIGESTBYTES), 128);
}
这样,分段错误就消失了。但是返回的字符串带有一个奇怪的字符,例如:
"ruby\x00\x00\x00\x00\x00未检测到错误。\x00\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xD80x\xC0 \x18`\x18\x18&F\xAF\x05#\x8C##\xB8\x91\xF9~\xC6?\xC6\xC6\xFB\xCDo\x13\xE8\x87\xE8\xE8\xCB\x13\xA1L \x87&\x87\x87\x11mb\xA9\xB8\xDA\xB8\xB8\t\x02\x05\b\x01\x04\x01\x01\r\x9EnBO!OO\x9Bl\xEE\xAD6\xD866\xFFQ \x04Y\xA6\xA2\xA6\xA6\f\xB9\xBD\xDE\xD2o\xD2"
当正确时必须只有:
“红宝石”
编辑 3:
进行此更改:
VALUE
print_string(VALUE class, VALUE *valor) {
struct NESSIEstruct w;
u8 digest[DIGESTBYTES];
/*
int i;
for (i = 0; valor[i] != '\0'; i++);
int sizeo = i;
*/
NESSIEinit(&w);
NESSIEadd((u8*)"ruby", 8*4, &w);
NESSIEfinalize(&w, digest);
return rb_str_new(displayHash(digest, DIGESTBYTES), 128);
}
返回正确的值。
然后我尝试这样做:
VALUE
print_string(VALUE class, VALUE *valor) {
struct NESSIEstruct w;
u8 digest[DIGESTBYTES];
/*
int i;
for (i = 0; valor[i] != '\0'; i++);
int sizeo = i;
*/
NESSIEinit(&w);
NESSIEadd((u8*)"ruby", 8*4, &w);
NESSIEfinalize(&w, digest);
return rb_str_new2(valor);
}
期望返回“ruby”字符串。但不是。它返回:“\x05”
这是什么意思?
【问题讨论】:
-
您不能只将 c 字符串转换为/从 VALUE 转换 - 您需要使用适当的转换函数 - 请参阅 docs.ruby-lang.org/en/2.2.0/README_EXT.html
-
@FrederickCheung 使用“rb_str_new”作为文件说,我现在可以返回一个字符串。但是附加了奇怪的字符。请查看我的编辑
-
您已将 128 作为最后一个参数传递给 rb_str_new,因此您将返回 128 字节长的内容
-
@FrederickCheung 请看我的编辑#3
-
仅更改函数签名不会更改您传递的数据
标签: c ruby segmentation-fault