我编写函数来创建目录并强制该目录归用户所有。

int mkdir_and_force_owner( const char *path, uid_t uid, gid_t gid )
{
    /* deref_ptr_in_call: Dereferencing pointer "path" */
    if( mkdir( path, S_IRWXU ) < 0 ) {
        if( errno != EEXIST ) {
            lfprintf( stderr, _("Cannot create %s: %s\n"), path, strerror( errno ) );
        } else {
            DIR *temp_dir = opendir( path );
            /* check_after_deref: Null-checking "path" suggests that it may be null, but it has already been dereferenced on all paths leading to the check */
            if( !path ) {
                lfprintf( stderr, _("Cannot open %s: %s\n"), path, strerror( errno ) );
            } else {
                closedir( temp_dir );
                return 1;
            }
            closedir( temp_dir );
        }
    } else {
        if( chown( path, uid, gid ) < 0 ) {
            lfprintf( stderr, _("Cannot change owner of %s: %s.\n"), path, strerror( errno ) );
        } else {
            return 1;
        }
    }

    return 0;
}

但这段代码得到了这个警告:
空检查“path”表明它可能为空,但它已经在所有通向检查的路径上被取消引用。
我想要同样的帮助来解决这个问题
谢谢

最佳答案

在向stderr打印“Cannot open”消息的行上,编译器已经很友好地注意到,根据定义,您正试图向lfprintf传递一个空字符串指针因为它包含在一个if (!path) {}块中。你是想说if (!temp_dir)

关于c - 空检查前取消引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19821408/

10-11 23:12
查看更多