Vala:to_utf32_fast 方法的警告和分段错误

Vala: warning and segmentation fault with to_utf32_fast method

我在编译时得到了一个警告和一个分段错误,运行宁大致下面的代码,它在 string 上使用 to_utf32_fast 方法并且应该 return codepoint_count 变量中编码的 UTF-32 字符数。此方法转换为 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),不确定我是否掌握了参数注释。第二个参数不应作为 long ** 而不是 long * 转译为 C,但也许可空性标记 ? 或默认值 = null 导致 Vala 认为变量items_written 是指向 long(或 Vala 等价物)的指针,而不是 long。如果是这样,那么可能是方法声明有误,或者 Vala 语法有歧义。

声明有误。此代码工作正常:

[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