2016-04-29 46 views
0

我安裝了Valac 0.30。運行以下代碼,Genie Segfaults很容易typecast

[indent=4] 
init 
    str : string 
    str = "Hello World"; 
    data : array of uint8 
    data = (array of uint8) str; 
    print "%i\n", data.length; 

我收到段錯誤。 GDB告訴我這個:

 
Program received signal SIGSEGV, Segmentation fault. 
__memcpy_sse2_unaligned() 
    at ../sysdeps/x86_64/multiarch/memcpy-sse2-unaligned.S:36 
36 ../sysdeps/x86_64/multiarch/memcpy-sse2-unaligned.S: No such file or directory. 

我見過一些其他人有這個問題,但沒有人得到解決方案,已爲我工作。

+1

GDB告訴你的重要部分是關於分段錯誤的一行。然後它會告訴你它發生的功能,包括文件名和行號。這裏的問題是崩潰發生在庫函數中,並且您沒有安裝標準庫的源代碼。這不是一個你應該擔心的錯誤,而是你需要看看函數調用堆棧(使用'bt'命令)來確定崩潰發生在你的*代碼的哪個位置。如果您可以查看生成的C代碼,那麼它也會有所幫助,因爲這是您將指向的地方。 –

回答

2

您正在告訴編譯器強制將string轉換爲array of uint8,但是這些類型不是分配兼容的。

引擎蓋下簡化生成的C代碼(你可以用valac -C獲得)看起來是這樣的:

#include <glib.h> 

int main (void) { 
     gchar* str = g_strdup ("Hello World"); 
     // Ouch: A negative number is used as length for g_memdup 
     // This will produce a segfault, because the parameter is unsigned and will overflow to a very big number. 
     // The string is only 11 bytes long however 
     guint8* data = g_memdup (str, -1 * sizeof(guint8)); 
     int data_length1 = -1; 
     g_print ("%i\n\n", data_length1); 
     g_free (data); 
     g_free (str); 
     return 0; 
} 

string data type有意味着你正在做什麼(瓦拉語法)兩個屬性:

public int length { get; } 
public uint8[] data { get; } 

所以,你可以寫你這樣的代碼:

[indent=4] 
init 
    str: string = "Hello World"; 
    print "%i\n", str.length; 

或者這樣:

[indent=4] 
init 
    str: string = "Hello World"; 
    data: array of uint8 = str.data; 
    print "%i\n", data.length; 

爲了完整這裏是gdb回溯:

(gdb) run 
Starting program: /home/user/src/genie/Test 
[Thread debugging using libthread_db enabled] 
Using host libthread_db library "/lib64/libthread_db.so.1". 

Program received signal SIGSEGV, Segmentation fault. 
__memcpy_avx_unaligned() at ../sysdeps/x86_64/multiarch/memcpy-avx-unaligned.S:245 
245    vmovdqu -0x80(%rsi,%rdx), %xmm5 
(gdb) bt 
#0 __memcpy_avx_unaligned() at ../sysdeps/x86_64/multiarch/memcpy-avx-unaligned.S:245 
#1 0x00007ffff78b66c6 in memcpy (__len=4294967295, __src=0x60cdd0, __dest=<optimized out>) at /usr/include/bits/string3.h:53 
#2 g_memdup (mem=0x60cdd0, byte_size=4294967295) at /usr/src/debug/dev-libs/glib-2.46.2-r2/glib-2.46.2/glib/gstrfuncs.c:392 
#3 0x00000000004007d6 in _vala_array_dup1 (self=0x60cdd0 "Hello World", length=-1) at /home/user/src/genie/Test.gs:6 
#4 0x000000000040085e in _vala_main (args=0x7fffffffdf78, args_length1=1) at /home/user/src/genie/Test.gs:6 
#5 0x00000000004008f5 in main (argc=1, argv=0x7fffffffdf78) at /home/user/src/genie/Test.gs:2 

所以g_memdup正試圖從一個11字節的字符串複製在這裏4294967295個字節。

+0

謝謝你的回答。解釋一切。我在一個試圖使用的庫中找到了上面的代碼片段。一直很頭疼。再次感謝。 – user2784930