Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions sound/soc/sof/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
/* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
if (data->size > be->max - sizeof(*data)) {
dev_err_ratelimited(scomp->dev,
"error: %u bytes of control data is invalid, max is %lu\n",
data->size, be->max - sizeof(*data));
"error: %u bytes of control data is invalid, max is %zu\n",
data->size, (size_t)be->max - sizeof(*data));
return -EINVAL;
}

Expand Down Expand Up @@ -266,8 +266,8 @@ int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
/* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
if (data->size > be->max - sizeof(*data)) {
dev_err_ratelimited(scomp->dev,
"error: data size too big %u bytes max is %lu\n",
data->size, be->max - sizeof(*data));
"error: data size too big %u bytes max is %zu\n",
data->size, (size_t)be->max - sizeof(*data));
return -EINVAL;
}

Expand Down Expand Up @@ -388,8 +388,9 @@ int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int _

/* check data size doesn't exceed max coming from topology */
if (cdata->data->size > be->max - sizeof(const struct sof_abi_hdr)) {
dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %lu.\n",
cdata->data->size, be->max - sizeof(const struct sof_abi_hdr));
dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %zu.\n",
cdata->data->size,
(size_t)be->max - sizeof(const struct sof_abi_hdr));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmh, type-casts are evil(TM). Is there any specific reason for this one? It's only to fix the printk() format, right? The calculation, the value would be exactly the same if we just used %d? Maybe just do that then... Or is this because size_t is a different type on 32-bit builds, ARM?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be->max is an int, but after arithmetic with sizeof(), it's a "unsigned int" right. so would "%u" as printf syntax solve ths issue without a cast?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lyakh yes size_t is different.
I tried earlier with a %d but it wouldn't work for x86_64. the %lu fails on 32-bit.
So I settled on a typecast to force the use of size_t, and let the compiler adjust.
If you have a better solution I am all ears, just trying to avoid an error reported upstream.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be->max is an int, but after arithmetic with sizeof(), it's a "unsigned int" right. so would "%u" as printf syntax solve ths issue without a cast?

@kv2019i no, sizeof() returns a size_t, which can be 32 or 64 bits, so that impacts the results.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cast seems unnecessary, %zd should be enough. Have you tried with %zd only and no cast?

I think we can safely assume size_t has a rank always greater or equal to int.

In user space the following code compiles with both -m32 and -m64 without any warning:

  int i = -1;
  size_t s = 42;
  printf ("%zd", i - s);

https://wiki.sei.cmu.edu/confluence/display/c/INT02-C.+Understand+integer+conversion+rules

Usual arithmetic conversions
3. If the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type is converted to the type of the operand with unsigned integer type.

Copy link
Collaborator

@marc-hb marc-hb Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because you switch to %lu (by accident?). I wrote no cast and %zu.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see e1cc344, didn't work.

Copy link
Collaborator

@marc-hb marc-hb Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that e1cc344 has %lu and not the %zu that I suggested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I was misled by the error message in topology.c which you hadn't changed yet.

ret = -EINVAL;
goto out;
}
Expand Down Expand Up @@ -440,8 +441,9 @@ int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,

/* check data size doesn't exceed max coming from topology */
if (cdata->data->size > be->max - sizeof(const struct sof_abi_hdr)) {
dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %lu.\n",
cdata->data->size, be->max - sizeof(const struct sof_abi_hdr));
dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %zu.\n",
cdata->data->size,
(size_t)be->max - sizeof(const struct sof_abi_hdr));
return -EINVAL;
}

Expand Down
4 changes: 2 additions & 2 deletions sound/soc/sof/topology.c
Original file line number Diff line number Diff line change
Expand Up @@ -1162,8 +1162,8 @@ static int sof_control_load_bytes(struct snd_soc_component *scomp,

/* init the get/put bytes data */
if (priv_size > max_size - sizeof(struct sof_ipc_ctrl_data)) {
dev_err(scomp->dev, "err: bytes data size %lu exceeds max %lu.\n",
priv_size, max_size - sizeof(struct sof_ipc_ctrl_data));
dev_err(scomp->dev, "err: bytes data size %zu exceeds max %zu.\n",
priv_size, (size_t)max_size - sizeof(struct sof_ipc_ctrl_data));
ret = -EINVAL;
goto out;
}
Expand Down