1. ホーム
  2. Linux

警告について:互換性のないポインタ型からの初期化【デフォルトで有効

2022-02-07 19:53:11

デバッグしやすいようにsysファイルシステムを追加したのですが、以下のようなコンパイル警告が出て、非常に不愉快な気分になり、2時間ほど翻弄されてようやく解決しました。

/home/apuser/mywork/4.1-3.4/kernel/drivers/input/misc/headset_xxx.c:800:9: warning: initialization from incompatible pointer type [ enable by default ](警告:互換性のないポインタ型からの初期化)。

/home/apuser/mywork/4.1-3.4/kernel/drivers/input/misc/headset_xxxx.c:800:9: 警告: (「headset_suspend_attr.store」の初期化付近) [デフォルトで使用可能]。

原因は

関数の引数の型が不一致です。

Definition of the function type when a warning occurs.
static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, char *buff, size_t len)
The repaired definition.
static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buff, size_t len)

	struct attribute attr;
	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
			char *buf);
	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
			 const char *buf, size_t count);
};


#ifdef HEADSET_DBG
/***create sys fs for debug***/
static int suspend_sts = 0;
static int headset_suspend(struct platform_device *dev, pm_message_t state);
static int headset_resume(struct platform_device *dev);

static ssize_t headset_suspend_show(struct kobject *kobj, struct kobj_attribute *attr, char *buff)
{
	PRINT_INFO("headset_suspend_show\n");
	return sprintf(buff, "%d\n", suspend_sts);
}

static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buff, size_t len)
{
	pm_message_t pm_message_t_temp = {
		.event = 0,
	};
	unsigned long suspend = simple_strtoul(buff, NULL, 10);
	PRINT_INFO("headset_suspend_store. buff = %s set_val = %ld\n", buff, suspend);
	if(suspend) {
		headset_suspend(NULL, pm_message_t_temp);
	}
	else {
		headset_resume(NULL);
	}
	return len;
}

static struct kobject *headset_suspend_kobj = NULL;
static struct kobj_attribute headset_suspend_attr =
        __ATTR(suspend, 0644, headset_suspend_show, headset_suspend_store);

static int headset_suspend_sysfs_init(void)
{
        int ret = -1;

        headset_suspend_kobj = kobject_create_and_add("headset", kernel_kobj);
        if (headset_suspend_kobj == NULL) {
                ret = -ENOMEM;
                PRINT_ERR("register sysfs failed. ret = %d\n", ret);
                return ret;
        }

        ret = sysfs_create_file(headset_suspend_kobj, &headset_suspend_attr.attr);
        if (ret) {
                PRINT_ERR("create sysfs failed. ret = %d\n", ret);
                return ret;
        }

        PRINT_INFO("headset_suspend_sysfs_init success\n");
        return ret;
}
/***create sys fs for debug***/
#endif








static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buff, size_t len)

これは、引数3の型をconstで修飾しなかった結果です。

解析参照。

struct kobj_attribute { <未定義

	struct attribute attr;
	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
			char *buf);
	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
			 const char *buf, size_t count);
};


結論から言うと、まだまだ油断大敵です。


私のコードリファレンスです。

#ifdef HEADSET_DBG
/***create sys fs for debug***/
static int suspend_sts = 0;
static int headset_suspend(struct platform_device *dev, pm_message_t state);
static int headset_resume(struct platform_device *dev);

static ssize_t headset_suspend_show(struct kobject *kobj, struct kobj_attribute *attr, char *buff)
{
	PRINT_INFO("headset_suspend_show\n");
	return sprintf(buff, "%d\n", suspend_sts);
}

static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buff, size_t len)
{
	pm_message_t pm_message_t_temp = {
		.event = 0,
	};
	unsigned long suspend = simple_strtoul(buff, NULL, 10);
	PRINT_INFO("headset_suspend_store. buff = %s set_val = %ld\n", buff, suspend);
	if(suspend) {
		headset_suspend(NULL, pm_message_t_temp);
	}
	else {
		headset_resume(NULL);
	}
	return len;
}

static struct kobject *headset_suspend_kobj = NULL;
static struct kobj_attribute headset_suspend_attr =
        __ATTR(suspend, 0644, headset_suspend_show, headset_suspend_store);

static int headset_suspend_sysfs_init(void)
{
        int ret = -1;

        headset_suspend_kobj = kobject_create_and_add("headset", kernel_kobj);
        if (headset_suspend_kobj == NULL) {
                ret = -ENOMEM;
                PRINT_ERR("register sysfs failed. ret = %d\n", ret);
                return ret;
        }

        ret = sysfs_create_file(headset_suspend_kobj, &headset_suspend_attr.attr);
        if (ret) {
                PRINT_ERR("create sysfs failed. ret = %d\n", ret);
                return ret;
        }

        PRINT_INFO("headset_suspend_sysfs_init success\n");
        return ret;
}
/***create sys fs for debug***/
#endif