与getpwuid()随机崩溃

时间:2013-12-12 02:20:23

标签: linux memory malloc getpwuid

我有一个将uid转换为用户名的函数,

char *uid2name (uid_t uid)
{
    struct passwd *pwd = getpwuid (uid);
    if (pwd)
        return strdup (pwd->pw_name);

    return NULL;
}

我称之为:

name = uid2name (atoi (blabal));

有时它会因ABRTBUS错误而崩溃:

(我知道有符号整数和无符号整数之间存在转换,但我测试了带负值的getpwuid函数,它不会崩溃。)

Program received signal SIGBUS, Bus error.
[Switching to Thread 1088039264 (LWP 7572)]
0x00007f504afaff10 in malloc_consolidate () from /lib64/tls/libc.so.6
#0  0x00007f504afaff10 in malloc_consolidate () from /lib64/tls/libc.so.6
#1  0x00007f504afb0f17 in _int_malloc () from /lib64/tls/libc.so.6
#2  0x00007f504afb2c52 in malloc () from /lib64/tls/libc.so.6
#3  0x00007f504afa26ba in __fopen_internal () from /lib64/tls/libc.so.6
#4  0x00007f50499ce04a in internal_setent () from /lib64/libnss_files.so.2
#5  0x00007f50499ce5b0 in _nss_files_getpwuid_r ()
   from /lib64/libnss_files.so.2
#6  0x00007f504afd63fd in getpwuid_r@@GLIBC_2.2.5 () from /lib64/tls/libc.so.6
#7  0x00007f504afd5d5d in getpwuid () from /lib64/tls/libc.so.6
#8  0x00007f504b783579 in uid2name (uid=Variable "uid" is not available.
) at XX.c

任何想法可能会出错?

0 个答案:

没有答案