Vala:使用to_utf32_fast方法发出警告和分段错误

时间:2018-05-27 08:51:30

标签: c segmentation-fault vala

在编译和运行大致以下代码时出现警告和分段错误,该代码在to_utf32_fast上使用string方法,并且应返回codepoint_count中编码的UTF-32字符数{1}}变量。此方法转换为C函数g_utf8_to_ucs4_fast,不知何故out codepoint_count最终为long * *参数,而不是预期的long *

我确实有一个解决方法,所以这并不紧急。

int main (string[] args) {
    string test = "abc";
    long codepoint_count;
    string utf32_version = test.to_utf32_fast(-1, out codepoint_count).to_string();
    stdout.printf("success");
    return 0;
}

输出的相关部分:

C:/msys64/usr/src/outerror.vala.c: In function '_vala_main':
C:/msys64/usr/src/outerror.vala.c:78:50: warning: passing argument 3 of 'g_utf8_to_ucs4_fast' from incompatible pointer type [-Wincompatible-pointer-types]
  _tmp2_ = g_utf8_to_ucs4_fast (test, (glong) -1, &_tmp1_);
                                                  ^
In file included from C:/msys64/mingw64/include/glib-2.0/glib/gstring.h:33:0,
                 from C:/msys64/mingw64/include/glib-2.0/glib/giochannel.h:34,
                 from C:/msys64/mingw64/include/glib-2.0/glib.h:54,
                 from C:/msys64/usr/src/outerror.vala.c:5:
C:/msys64/mingw64/include/glib-2.0/glib/gunicode.h:798:12: note: expected 'glong * {aka long int *}' but argument is of type 'glong ** {aka long int **}'
 gunichar * g_utf8_to_ucs4_fast (const gchar      *str,
            ^~~~~~~~~~~~~~~~~~~

我查看了已编译的C源代码,g_utf8_to_ucs4_fast的第三个参数是指向long的未初始化指针的地址,后来用g_free释放。这会在程序运行时触发段错误。

我在调用此功能的方式上做错了吗?它被声明为public string32 to_utf32_fast (long len = -1, out long? items_written = null)

我是Vala的新手(更熟悉C)并且不确定我是否掌握了论证注释。第二个参数不应该被转换为C long **而不是long *,但可能是可空性标记?或默认值= null导致Vala认为变量items_written是指向long(或其等效的Vala)而不是long的指针。如果是这样,那么可能在方法声明中存在错误或Vala语法中存在歧义。

1 个答案:

答案 0 :(得分:1)

声明是错误的。这段代码很好用:

[CCode (cname = "g_utf8_to_ucs4_fast")]
public extern string32 to_utf32_fast_ (string s, long len = -1,
    out long items_written);

int main (string[] args) {
    string test = "abc";
    long codepoint_count;
    string32 utf32_version = to_utf32_fast_(test, -1, out codepoint_count);
    stdout.printf("success");
    return 0;
}

在glib-2.0.vapi的原始声明中,items_written参数在C中为glong**,但它实际上是glong*

我已将此报告为错误:

https://gitlab.gnome.org/GNOME/vala/issues/634