Skip to contentSkip to conversation

pc-bios/s390-ccw: size_t should be unsigned

PATCH Superseded Thomas Huth 7 participants 11 messages Apr 13, 2018 – Apr 18, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pc-bios/s390-ccw: size_t should be unsigned

"size_t" should be an unsigned type - the signed counterpart is called "ssize_t" in the C standard instead. Thus we should also use this convention in the s390-ccw firmware to avoid confusion. I checked the sources, and apart from one spot in libc.c (which now uses ssize_t with this patch), the code should all be fine with this change.

---
 pc-bios/s390-ccw/libc.c | 2 +-
 pc-bios/s390-ccw/libc.h | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
pc-bios/s390-ccw/libc.c +1 -1
index 38ea77d..827d204 100644
@@ -63,7 +63,7 @@ uint64_t atoui(const char *str)
  */
 char *uitoa(uint64_t num, char *str, size_t len)
 {
-    size_t num_idx = 1; /* account for NUL */
+    ssize_t num_idx = 1; /* account for NUL */
     uint64_t tmp = num;
 
     IPL_assert(str != NULL, "uitoa: no space allocated to store string");
pc-bios/s390-ccw/libc.h +2 -1
index 63ece70..57c4199 100644
@@ -12,7 +12,8 @@
 #ifndef S390_CCW_LIBC_H
 #define S390_CCW_LIBC_H
 
-typedef long               size_t;
+typedef unsigned long      size_t;
+typedef signed long        ssize_t;
 typedef int                bool;
 typedef unsigned char      uint8_t;
 typedef unsigned short     uint16_t;
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Thomas Huth

"size_t" should be an unsigned type - the signed counterpart is called "ssize_t" in the C standard instead. Thus we should also use this convention in the s390-ccw firmware to avoid confusion. I checked the sources, and apart from one spot in libc.c (which now uses ssize_t with this patch), the code should all be fine with this change.

Quoted 33 lines
---
 pc-bios/s390-ccw/libc.c | 2 +-
 pc-bios/s390-ccw/libc.h | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
pc-bios/s390-ccw/libc.c +1 -1
index 38ea77d..827d204 100644
@@ -63,7 +63,7 @@ uint64_t atoui(const char *str)
  */
 char *uitoa(uint64_t num, char *str, size_t len)
 {
-    size_t num_idx = 1; /* account for NUL */
+    ssize_t num_idx = 1; /* account for NUL */
     uint64_t tmp = num;
 
     IPL_assert(str != NULL, "uitoa: no space allocated to store string");
pc-bios/s390-ccw/libc.h +2 -1
index 63ece70..57c4199 100644
@@ -12,7 +12,8 @@
 #ifndef S390_CCW_LIBC_H
 #define S390_CCW_LIBC_H
 
-typedef long               size_t;
+typedef unsigned long      size_t;
+typedef signed long        ssize_t;
 typedef int                bool;
 typedef unsigned char      uint8_t;
 typedef unsigned short     uint16_t;
Reviewed-by on v1 1/1
#2 lore raw
In reply to Thomas Huth
Quoted 32 lines · Thomas Huth

"size_t" should be an unsigned type - the signed counterpart is called "ssize_t" in the C standard instead. Thus we should also use this convention in the s390-ccw firmware to avoid confusion. I checked the sources, and apart from one spot in libc.c (which now uses ssize_t with this patch), the code should all be fine with this change.

---
 pc-bios/s390-ccw/libc.c | 2 +-
 pc-bios/s390-ccw/libc.h | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
pc-bios/s390-ccw/libc.c +1 -1
index 38ea77d..827d204 100644
@@ -63,7 +63,7 @@ uint64_t atoui(const char *str)
  */
 char *uitoa(uint64_t num, char *str, size_t len)
 {
-    size_t num_idx = 1; /* account for NUL */
+    ssize_t num_idx = 1; /* account for NUL */
     uint64_t tmp = num;
 
     IPL_assert(str != NULL, "uitoa: no space allocated to store string");
pc-bios/s390-ccw/libc.h +0 -0
index 63ece70..57c4199 100644

pc-bios/s390-ccw/libc.h line 19

Quoted 9 lines
pc-bios/s390-ccw/libc.h +2 -1
@@ -12,7 +12,8 @@
 #ifndef S390_CCW_LIBC_H
 #define S390_CCW_LIBC_H
 
-typedef long               size_t;
+typedef unsigned long      size_t;
+typedef signed long        ssize_t;
 typedef int                bool;
 typedef unsigned char      uint8_t;
 typedef unsigned short     uint16_t;

Looks good to me as well.

If another r-b is even necessary:

Signature · 2 lines
Respectfully,
- Collin Walling
Acked-by on v1 1/1
#3 lore raw
In reply to Thomas Huth
Quoted 2 lines · Thomas Huth

"size_t" should be an unsigned type - the signed counterpart is called "ssize_t" in the C standard instead. Thus we should also use this

The first sentence sounds like ssize_t is too a type defined by some C standard. Is it or does ssize_t come form somewhere else? I find negative size a little difficult conceptually.

Quoted 7 lines

convention in the s390-ccw firmware to avoid confusion. I checked the sources, and apart from one spot in libc.c (which now uses ssize_t with this patch), the code should all be fine with this change.

---

This is certainly an improvement over the confusing signed size_t, so:

BTW The stuff behind the buglink is a bit misleading. The description states the problem as can't escape loop (IMHO) and the bug status say 'confirmed'.

What actually happened is that it turned out the problem initially reported, was not existent. Yet the bug report helped us find another problem:
confusing names.

To complicate understanding even further, the comments on the bug only contain this realization hidden behind a link.

It probably does not matter though.

#4 lore raw
Quoted 9 lines · Halil Pasic
Quoted 2 lines · Thomas Huth

"size_t" should be an unsigned type - the signed counterpart is called "ssize_t" in the C standard instead. Thus we should also use this

The first sentence sounds like ssize_t is too a type defined by some C standard. Is it or does ssize_t come form somewhere else? I find negative size a little difficult conceptually.

I think ssize_t is from POSIX:
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html
"Used for a count of bytes or an error indication".

thanks
-- PMM

#5 lore raw
In reply to Halil Pasic
Quoted 8 lines · Halil Pasic
Quoted 2 lines · Thomas Huth

"size_t" should be an unsigned type - the signed counterpart is called "ssize_t" in the C standard instead. Thus we should also use this

The first sentence sounds like ssize_t is too a type defined by some C standard. Is it or does ssize_t come form somewhere else?

Arrr, seems like ssize_t is rather coming from POSIX than from the C standard, thanks for the hint. I'll rephrase the first sentence to:

"size_t" should be an unsigned type according to the C standard, and most libc implementations provide a signed counterpart called "ssize_t".

OK?

Quoted 11 lines
Quoted 7 lines

convention in the s390-ccw firmware to avoid confusion. I checked the sources, and apart from one spot in libc.c (which now uses ssize_t with this patch), the code should all be fine with this change.

---

This is certainly an improvement over the confusing signed size_t, so:

Thanks!

Quoted 7 lines

BTW The stuff behind the buglink is a bit misleading. The description states the problem as can't escape loop (IMHO) and the bug status say 'confirmed'.

What actually happened is that it turned out the problem initially reported, was not existent. Yet the bug report helped us find another problem:
confusing names.

Ok, I've updated the bug title.

Quoted 2 lines

To complicate understanding even further, the comments on the bug only contain this realization hidden behind a link.

Oh well, yes, the bridge between the bugtracker and the mailing list really su...ffers from many problems. Normally replies should show up in the bug tracker as well, but in this case the bridge just failed.

Thomas

#6 lore raw
Quoted 7 lines · Thomas Huth
Quoted 6 lines · Halil Pasic
Quoted 2 lines · Thomas Huth

"size_t" should be an unsigned type - the signed counterpart is called "ssize_t" in the C standard instead. Thus we should also use this

The first sentence sounds like ssize_t is too a type defined by some C standard. Is it or does ssize_t come form somewhere else?

Quoted 8 lines

Arrr, seems like ssize_t is rather coming from POSIX than from the C standard, thanks for the hint. I'll rephrase the first sentence to:

"size_t" should be an unsigned type according to the C standard, and most libc implementations provide a signed counterpart called "ssize_t".

OK?

This ssize_t seems to be an rather interesting type. For instance POSIX says """
size_t
Used for sizes of objects.
ssize_t
Used for a count of bytes or an error indication.
"""
and
"""
The type ssize_t shall be capable of storing values at least in the range [-1, {SSIZE_MAX}]. """

And it does not mandate SSIZE_MIN in limits (but of course mandates SSIZE_MAX.

I don't like this 'counterpart' word here, because AFAIU these don't have to be counterparts in any sense. That is SSIZE_MAX << SIZE_MAX is possible for example. I'm not sure about the every positive has a negative thing, but that's not important here.

The code in question kind of uses both signed and unsigned size for the same (the string). We even have a signed to unsigned comparison which could result in warnings. I still think the change is OK in practice, but maybe avoiding introducing ssize_t (until we really need it) is a better course of action. I think uitoa can be easily rewritten so it does not need the ssize_t.

How about that?

Regards,
Halil

#7 lore raw
Quoted 33 lines · Halil Pasic
Quoted 5 lines
Quoted 5 lines · Thomas Huth
Quoted 2 lines

"size_t" should be an unsigned type - the signed counterpart is called "ssize_t" in the C standard instead. Thus we should also use this

The first sentence sounds like ssize_t is too a type defined by some C standard. Is it or does ssize_t come form somewhere else?

Quoted 8 lines

Arrr, seems like ssize_t is rather coming from POSIX than from the C standard, thanks for the hint. I'll rephrase the first sentence to:

"size_t" should be an unsigned type according to the C standard, and most libc implementations provide a signed counterpart called "ssize_t".

OK?

This ssize_t seems to be an rather interesting type. For instance POSIX says """
size_t
Used for sizes of objects.
ssize_t
Used for a count of bytes or an error indication.
"""
and
"""
The type ssize_t shall be capable of storing values at least in the range [-1, {SSIZE_MAX}]. """

And it does not mandate SSIZE_MIN in limits (but of course mandates SSIZE_MAX.

I don't like this 'counterpart' word here, because AFAIU these don't have to be counterparts in any sense. That is SSIZE_MAX << SIZE_MAX is possible for example. I'm not sure about the every positive has a negative thing, but that's not important here.

Quoted 8 lines

The code in question kind of uses both signed and unsigned size for the same (the string). We even have a signed to unsigned comparison which could result in warnings. I still think the change is OK in practice, but maybe avoiding introducing ssize_t (until we really need it) is a better course of action. I think uitoa can be easily rewritten so it does not need the ssize_t.

How about that?

This seems clever indeed.

#8 lore raw
Quoted 41 lines · Philippe Mathieu-Daudé
Quoted 40 lines · Halil Pasic
Quoted 5 lines
Quoted 5 lines · Thomas Huth
Quoted 2 lines

"size_t" should be an unsigned type - the signed counterpart is called "ssize_t" in the C standard instead. Thus we should also use this

The first sentence sounds like ssize_t is too a type defined by some C standard. Is it or does ssize_t come form somewhere else?

Quoted 8 lines

Arrr, seems like ssize_t is rather coming from POSIX than from the C standard, thanks for the hint. I'll rephrase the first sentence to:

"size_t" should be an unsigned type according to the C standard, and most libc implementations provide a signed counterpart called "ssize_t".

OK?

This ssize_t seems to be an rather interesting type. For instance POSIX says """
size_t
Used for sizes of objects.
ssize_t
Used for a count of bytes or an error indication.
"""
and
"""
The type ssize_t shall be capable of storing values at least in the range [-1, {SSIZE_MAX}]. """

And it does not mandate SSIZE_MIN in limits (but of course mandates SSIZE_MAX.

I don't like this 'counterpart' word here, because AFAIU these don't have to be counterparts in any sense. That is SSIZE_MAX << SIZE_MAX is possible for example. I'm not sure about the every positive has a negative thing, but that's not important here.

The code in question kind of uses both signed and unsigned size for the same (the string). We even have a signed to unsigned comparison which could result in warnings. I still think the change is OK in practice, but maybe avoiding introducing ssize_t (until we really need it) is a better course of action. I think uitoa can be easily rewritten so it does not need the ssize_t.

Quoted 4 lines
Quoted 1 line

How about that?

This seems clever indeed.

This whole issue stems from my misuse of size_t in the first place. If it makes things easier, let's just make num_idx of type "signed long".

After reading this discussion, I think it makes sense to drop ssize_t. No need to make it available for just one function unless there are strong claims to also use this type elsewhere in the pc-bios (I can't find any).

Signature · 2 lines
Respectfully,
- Collin Walling
#9 lore raw
Quoted 47 lines · On Fri, 13 Apr 2018 14:09:55 -0400 Collin Walling <[email protected]> wrote:
Quoted 41 lines · Philippe Mathieu-Daudé
Quoted 40 lines · Halil Pasic
Quoted 5 lines
Quoted 5 lines · Thomas Huth
Quoted 2 lines

"size_t" should be an unsigned type - the signed counterpart is called "ssize_t" in the C standard instead. Thus we should also use this

The first sentence sounds like ssize_t is too a type defined by some C standard. Is it or does ssize_t come form somewhere else?

Quoted 8 lines

Arrr, seems like ssize_t is rather coming from POSIX than from the C standard, thanks for the hint. I'll rephrase the first sentence to:

"size_t" should be an unsigned type according to the C standard, and most libc implementations provide a signed counterpart called "ssize_t".

OK?

This ssize_t seems to be an rather interesting type. For instance POSIX says """
size_t
Used for sizes of objects.
ssize_t
Used for a count of bytes or an error indication.
"""
and
"""
The type ssize_t shall be capable of storing values at least in the range [-1, {SSIZE_MAX}]. """

And it does not mandate SSIZE_MIN in limits (but of course mandates SSIZE_MAX.

I don't like this 'counterpart' word here, because AFAIU these don't have to be counterparts in any sense. That is SSIZE_MAX << SIZE_MAX is possible for example. I'm not sure about the every positive has a negative thing, but that's not important here.

The code in question kind of uses both signed and unsigned size for the same (the string). We even have a signed to unsigned comparison which could result in warnings. I still think the change is OK in practice, but maybe avoiding introducing ssize_t (until we really need it) is a better course of action. I think uitoa can be easily rewritten so it does not need the ssize_t.

Quoted 4 lines
Quoted 1 line

How about that?

This seems clever indeed.

Quoted 7 lines

This whole issue stems from my misuse of size_t in the first place. If it makes things easier, let's just make num_idx of type "signed long".

After reading this discussion, I think it makes sense to drop ssize_t. No need to make it available for just one function unless there are strong claims to also use this type elsewhere in the pc-bios (I can't find any).

+1 to just using signed long. Let's not make this needlessly complicated.

#10 lore raw
In reply to Halil Pasic
Quoted 12 lines · Halil Pasic

This ssize_t seems to be an rather interesting type. For instance POSIX says """
size_t
Used for sizes of objects.
ssize_t
Used for a count of bytes or an error indication.
"""
and
"""
The type ssize_t shall be capable of storing values at least in the range [-1, {SSIZE_MAX}]. """

Quoted 1 line

And it does not mandate SSIZE_MIN in limits (but of course mandates SSIZE_MAX.

I've tried to get POSIX to tighten things to require that 'size_t' and 'ssize_t' must have the same rank, so that you can sanely use printf("%zd",(ssize_t)val), but we are not there yet.

Quoted 5 lines

I don't like this 'counterpart' word here, because AFAIU these don't have to be counterparts in any sense. That is SSIZE_MAX << SIZE_MAX is possible for example. I'm not sure about the every positive has a negative thing, but that's not important here.

Indeed, until the POSIX wording is tightened, it is technically possible (but a very poor quality of implementation, and none of qemu's compilation platforms fall in that category) that ssize_t has a different rank than size_t (whether or not they also have a different width).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org