Ubuntu 上的 strdup() 崩溃

标签 c ubuntu crash strdup

当我尝试在 Ubuntu 上运行时,我的 C 程序出现段错误。这是堆栈跟踪。感谢任何帮助

0  0x015383f1 in ?? () from /lib/tls/i686/cmov/libc.so.6
#1  0x01538075 in strdup () from /lib/tls/i686/cmov/libc.so.6
#2  0x00c0a4af in cvtToString (ign=0xc10b80, target=0x83ba0a8, off=56, 
    source=0x0, setter=0xc044c3 <UxString_SetValue>) at xmlSerialPrimitives.c:28
#3  0x00c09adb in _setValue (xs=0x83b3520, inst=0x83ba0a8 "\020", prop=0xc103dc, 
    value=0x0) at xmlDeserializer.c:214
#4  0x00c09b71 in ctSetValue (xs=0x83b3520, property=0xc103dc, parent=0x83ba0a8, 
    val=0x0) at xmlDeserializer.c:237
#5  0x00c0a1aa in _closeElement (xs=0x83b3520, element=0x83ba1a0)
    at xmlDeserializer.c:419
#6  0x00c0a3da in xmlDeserialize (xs=0x83b3520, xtr=0x83b6218)
    at xmlDeserializer.c:533
#7  0x00c0ab14 in xmlDeserializeFromFile (xs=0x83b3520, 
    file=0x83b34f0 "/usr/v/xlayout.xml")
    at xmlSerializer.c:60
#8  0x00c07280 in load_uxUserModel (
    file=0x83b34f0 "/usr/v/xlayout.xml", data=0xbffff4dc)
    at UxUserModel.c:718
#9  0x00c073d0 in UxUserModel_LoadUserModel (name=0x8148e60 "x.xml")
    at UxUserModel.c:771
#10 0x00bf7f5d in UxModelLoad (umif=0x8397c58, filename=0x8148e60 "x.xml")

这是崩溃的函数

static int cvtToString( PCType * ign, void * target, int off, char * source, 
                        void_f    setter)
   {
        /* do we want to copy the string? */
        if (source && strncmp( source, "usermodel://", 12) == 0)
                return 0;
        source = strdup( source);
        if (setter)
               (*setter)( target, source);
        else
               (void) memcpy( target+off, &source, sizeof(char*));

        return 1;
   }

最佳答案

if (!source || !strncmp( source, "usermodel://", 12))
            return 0;

顺便说一句:这一行:

(void) memcpy( target+off, &source, sizeof(char*));

尝试对空指针执行算术运算。编译器应该(至少)提示。

关于Ubuntu 上的 strdup() 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10390012/

相关文章:

c - execl参数混淆

c - 在 C 中的客户端/服务器确认中使用 select()

c++ - 了解函数是否在运行时获取引用或值

c - 为什么我的阅读类型是空白的?

c# - MonoTouch 应用程序在模拟器中运行,但不在设备上运行

ubuntu - 在成功构建时通过 Jenkins 发送邮件?

java - OpenJDK 和 update-alternatives 命令

python - 无法终止使用 Python 子进程启动的 Jar 文件

objective-c - 地址簿崩溃不合逻辑

iOS:有时应用程序会终止