c - 从不兼容的指针类型传递参数 1( 'strcmp')

标签 c pointers strcmp incomplete-type

我知道这些问题一直被问到,但是在这种情况下,我不太确定正确的解决方案是什么。该函数是 3.4.x Linux 内核 fork 中 do_new_mount 的编辑函数,用于上下文。预期目的是检查文件系统的类型,然后在满足正确条件时发出异步标志。

行:

if (!err && ((!strcmp(type, "ext4") &&

完整功能(添加了异步部分,这导致了错误):

static int do_new_mount(struct path *path, const char *fstype, int flags,
            int mnt_flags, const char *name, void *data)
{
struct file_system_type *type;
struct user_namespace *user_ns;
struct vfsmount *mnt;
int err;

if (!fstype)
    return -EINVAL;

/* we need capabilities... */
user_ns = real_mount(path->mnt)->mnt_ns->user_ns;
if (!ns_capable(user_ns, CAP_SYS_ADMIN))
    return -EPERM;

type = get_fs_type(fstype);
if (!type)
    return -ENODEV;

if (user_ns != &init_user_ns) {
    if (!(type->fs_flags & FS_USERNS_MOUNT)) {
        put_filesystem(type);
        return -EPERM;
    }
    /* Only in special cases allow devices from mounts
     * created outside the initial user namespace.
     */
    if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
        flags |= MS_NODEV;
        mnt_flags |= MNT_NODEV;
    }
}

mnt = vfs_kern_mount(type, flags, name, data);
if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
    !mnt->mnt_sb->s_subtype)
    mnt = fs_set_subtype(mnt, fstype);

put_filesystem(type);
if (IS_ERR(mnt))
    return PTR_ERR(mnt);

err = do_add_mount(real_mount(mnt), path, mnt_flags);
if (err)
    mntput(mnt);
#ifdef CONFIG_ASYNC_FSYNC
if (!err && ((!strcmp(type, "ext4") &&
    !strcmp(path->dentry->d_name.name, "data")) ||
    (!strcmp(type, "fuse") &&
    !strcmp(path->dentry->d_name.name, "emulated"))))
            mnt->mnt_sb->fsync_flags |= FLAG_ASYNC_FSYNC;
#endif
return err;
}

最佳答案

显然 typestruct file_system_type * 类型,这不是 strcmp() 所期望的。我猜测 struct file_system_type 中的第一个字段是 char* ,因此它可以工作。

解决方案是将 type 转换为 char* 或正确取消引用它的第一个成员。 (显然第二种方法更好)

关于c - 从不兼容的指针类型传递参数 1( 'strcmp'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408836/

相关文章:

c - 宏空白

c - c 中的意外输出

c++将指针传递给函数,用于将int数组传入和传出函数

c - 为什么 strcmp 在此上下文中不返回 0?

c - 并行运行 while 循环

c - 将字符串保存为结构中的整数。 C

c - 指针大小相同吗?

c - 打印垃圾值,c

mysql if then 语句不起作用?

c - 使用 C 中的条件按字典顺序对 3 个字符串进行排序