【问题标题】:Vala: warning and segmentation fault with to_utf32_fast methodVala:to_utf32_fast 方法的警告和分段错误
【发布时间】:2018-05-27 08:51:30
【问题描述】:

在编译和运行以下代码时,我收到了警告和分段错误,该代码在string 上使用了to_utf32_fast 方法,并且应该返回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),不确定我是否掌握了参数注释。第二个参数不应转换为 C 为 long ** 而不是 long *,但可能为空性标记 ? 或默认值 = null 导致 Vala 认为变量 items_written 是指向 @ 987654338@(或其等效的 Vala)而不是 long。如果是这样,那么可能是方法的声明有错误或 Vala 语法有歧义。

【问题讨论】:

    标签: c segmentation-fault vala


    【解决方案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

    【讨论】:

    • 谢谢!并且声明为public extern string32 to_utf32_fast_ (string s, long len = -1, out long items_written = null) 允许省略第三个参数。
    猜你喜欢
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2021-08-06
    • 1970-01-01
    相关资源
    最近更新 更多