Skip to contentSkip to conversation

Make strtosz() return int64_t instead of ssize_t

PATCH Applied [email protected] 4 participants 9 messages Jan 5, 2011 – Jan 11, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Make strtosz() return int64_t instead of ssize_t

From: Jes Sorensen <[email protected]>

strtosz() needs to return a 64 bit type even on 32 bit architectures. Otherwise qemu-img will fail to create disk images >= 2GB

---
 cutils.c      |    8 ++++----
 monitor.c     |    2 +-
 qemu-common.h |    4 ++--
 qemu-img.c    |    2 +-
 vl.c          |    4 ++--
 5 files changed, 10 insertions(+), 10 deletions(-)
cutils.c +4 -4
index 7984bc1..4d2e27c 100644
@@ -291,9 +291,9 @@ int fcntl_setfl(int fd, int flag)
  * value must be terminated by whitespace, ',' or '\0'. Return -1 on
  * error.
  */
-ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
+int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
 {
-    ssize_t retval = -1;
+    int64_t retval = -1;
     char *endptr, c, d;
     int mul_required = 0;
     double val, mul, integral, fraction;
@@ -365,7 +365,7 @@ ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
             goto fail;
         }
     }
-    if ((val * mul >= ~(size_t)0) || val < 0) {
+    if ((val * mul >= INT64_MAX) || val < 0) {
         goto fail;
     }
     retval = val * mul;
@@ -378,7 +378,7 @@ fail:
     return retval;
 }
 
-ssize_t strtosz(const char *nptr, char **end)
+int64_t strtosz(const char *nptr, char **end)
 {
     return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
 }
monitor.c +1 -1
index f258000..fcdae15 100644
@@ -4162,7 +4162,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon,
             break;
         case 'o':
             {
-                ssize_t val;
+                int64_t val;
                 char *end;
 
                 while (qemu_isspace(*p)) {
qemu-common.h +2 -2
index 63d9943..cce6e61 100644
@@ -158,8 +158,8 @@ int fcntl_setfl(int fd, int flag);
 #define STRTOSZ_DEFSUFFIX_MB	'M'
 #define STRTOSZ_DEFSUFFIX_KB	'K'
 #define STRTOSZ_DEFSUFFIX_B	'B'
-ssize_t strtosz(const char *nptr, char **end);
-ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
+int64_t strtosz(const char *nptr, char **end);
+int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
 
 /* path.c */
 void init_paths(const char *prefix);
qemu-img.c +1 -1
index afd9ed2..6af2a4c 100644
@@ -320,7 +320,7 @@ static int img_create(int argc, char **argv)
 
     /* Get image size, if specified */
     if (optind < argc) {
-        ssize_t sval;
+        int64_t sval;
         sval = strtosz_suffix(argv[optind++], NULL, STRTOSZ_DEFSUFFIX_B);
         if (sval < 0) {
             error_report("Invalid image size specified! You may use k, M, G or "
vl.c +2 -2
index 78fcef1..93425f4 100644
@@ -804,7 +804,7 @@ static void numa_add(const char *optarg)
         if (get_param_value(option, 128, "mem", optarg) == 0) {
             node_mem[nodenr] = 0;
         } else {
-            ssize_t sval;
+            int64_t sval;
             sval = strtosz(option, NULL);
             if (sval < 0) {
                 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
@@ -2245,7 +2245,7 @@ int main(int argc, char **argv, char **envp)
                 exit(0);
                 break;
             case QEMU_OPTION_m: {
-                ssize_t value;
+                int64_t value;
 
                 value = strtosz(optarg, NULL);
                 if (value < 0) {
Signature · 1 line
1.7.3.4
#1 lore raw
Quoted 7 lines · On Wed, Jan 05, 2011 at 11:41:02AM +0100, [email protected] wrote:

From: Jes Sorensen <[email protected]>

strtosz() needs to return a 64 bit type even on 32 bit architectures. Otherwise qemu-img will fail to create disk images >= 2GB

Nothing wrong with this patch, but should the function be renamed to strtos64 then?

Quoted 108 lines
---
 cutils.c      |    8 ++++----
 monitor.c     |    2 +-
 qemu-common.h |    4 ++--
 qemu-img.c    |    2 +-
 vl.c          |    4 ++--
 5 files changed, 10 insertions(+), 10 deletions(-)
cutils.c +4 -4
index 7984bc1..4d2e27c 100644
@@ -291,9 +291,9 @@ int fcntl_setfl(int fd, int flag)
  * value must be terminated by whitespace, ',' or '\0'. Return -1 on
  * error.
  */
-ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
+int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
 {
-    ssize_t retval = -1;
+    int64_t retval = -1;
     char *endptr, c, d;
     int mul_required = 0;
     double val, mul, integral, fraction;
@@ -365,7 +365,7 @@ ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
             goto fail;
         }
     }
-    if ((val * mul >= ~(size_t)0) || val < 0) {
+    if ((val * mul >= INT64_MAX) || val < 0) {
         goto fail;
     }
     retval = val * mul;
@@ -378,7 +378,7 @@ fail:
     return retval;
 }
 
-ssize_t strtosz(const char *nptr, char **end)
+int64_t strtosz(const char *nptr, char **end)
 {
     return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
 }
monitor.c +1 -1
index f258000..fcdae15 100644
@@ -4162,7 +4162,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon,
             break;
         case 'o':
             {
-                ssize_t val;
+                int64_t val;
                 char *end;
 
                 while (qemu_isspace(*p)) {
qemu-common.h +2 -2
index 63d9943..cce6e61 100644
@@ -158,8 +158,8 @@ int fcntl_setfl(int fd, int flag);
 #define STRTOSZ_DEFSUFFIX_MB	'M'
 #define STRTOSZ_DEFSUFFIX_KB	'K'
 #define STRTOSZ_DEFSUFFIX_B	'B'
-ssize_t strtosz(const char *nptr, char **end);
-ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
+int64_t strtosz(const char *nptr, char **end);
+int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
 
 /* path.c */
 void init_paths(const char *prefix);
qemu-img.c +1 -1
index afd9ed2..6af2a4c 100644
@@ -320,7 +320,7 @@ static int img_create(int argc, char **argv)
 
     /* Get image size, if specified */
     if (optind < argc) {
-        ssize_t sval;
+        int64_t sval;
         sval = strtosz_suffix(argv[optind++], NULL, STRTOSZ_DEFSUFFIX_B);
         if (sval < 0) {
             error_report("Invalid image size specified! You may use k, M, G or "
vl.c +2 -2
index 78fcef1..93425f4 100644
@@ -804,7 +804,7 @@ static void numa_add(const char *optarg)
         if (get_param_value(option, 128, "mem", optarg) == 0) {
             node_mem[nodenr] = 0;
         } else {
-            ssize_t sval;
+            int64_t sval;
             sval = strtosz(option, NULL);
             if (sval < 0) {
                 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
@@ -2245,7 +2245,7 @@ int main(int argc, char **argv, char **envp)
                 exit(0);
                 break;
             case QEMU_OPTION_m: {
-                ssize_t value;
+                int64_t value;
 
                 value = strtosz(optarg, NULL);
                 if (value < 0) {
Signature · 1 line
1.7.3.4
#2 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · On Wed, Jan 05, 2011 at 11:41:02AM +0100, [email protected] wrote:

From: Jes Sorensen <[email protected]>

strtosz() needs to return a 64 bit type even on 32 bit architectures. Otherwise qemu-img will fail to create disk images >= 2GB

Nothing wrong with this patch, but should the function be renamed to strtos64 then?

I don't think that adds any value to be honest. The problem with the old interface was that the return type differed depending on whether it was compiled on 32 vs 64 bit systems.

Cheers,
Jes

#3 lore raw
Quoted 13 lines · Jes Sorensen
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · On Wed, Jan 05, 2011 at 11:41:02AM +0100, [email protected] wrote:

From: Jes Sorensen <[email protected]>

strtosz() needs to return a 64 bit type even on 32 bit architectures. Otherwise qemu-img will fail to create disk images >= 2GB

Nothing wrong with this patch, but should the function be renamed to strtos64 then?

Quoted 6 lines

I don't think that adds any value to be honest. The problem with the old interface was that the return type differed depending on whether it was compiled on 32 vs 64 bit systems.

Cheers,
Jes

Yes, but does not the name strtosz imply 'string to size'? Further changing the name will help make sure there are no users you forgot to update.

Signature · 1 line
MST
#4 lore raw
In reply to [email protected]
Quoted 8 lines · On 01/05/2011 04:41 AM, [email protected] wrote:

From: Jes Sorensen<[email protected]>

strtosz() needs to return a 64 bit type even on 32 bit architectures. Otherwise qemu-img will fail to create disk images>= 2GB

off_t would be the proper type to use.

Regards,

Anthony Liguori

Quoted 106 lines
---
  cutils.c      |    8 ++++----
  monitor.c     |    2 +-
  qemu-common.h |    4 ++--
  qemu-img.c    |    2 +-
  vl.c          |    4 ++--
  5 files changed, 10 insertions(+), 10 deletions(-)
cutils.c +4 -4
index 7984bc1..4d2e27c 100644
@@ -291,9 +291,9 @@ int fcntl_setfl(int fd, int flag)
   * value must be terminated by whitespace, ',' or '\0'. Return -1 on
   * error.
   */
-ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
+int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
  {
-    ssize_t retval = -1;
+    int64_t retval = -1;
      char *endptr, c, d;
      int mul_required = 0;
      double val, mul, integral, fraction;
@@ -365,7 +365,7 @@ ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
              goto fail;
          }
      }
-    if ((val * mul>= ~(size_t)0) || val<  0) {
+    if ((val * mul>= INT64_MAX) || val<  0) {
          goto fail;
      }
      retval = val * mul;
@@ -378,7 +378,7 @@ fail:
      return retval;
  }
 
-ssize_t strtosz(const char *nptr, char **end)
+int64_t strtosz(const char *nptr, char **end)
  {
      return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
  }
monitor.c +1 -1
index f258000..fcdae15 100644
@@ -4162,7 +4162,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon,
              break;
          case 'o':
              {
-                ssize_t val;
+                int64_t val;
                  char *end;
 
                  while (qemu_isspace(*p)) {
qemu-common.h +2 -2
index 63d9943..cce6e61 100644
@@ -158,8 +158,8 @@ int fcntl_setfl(int fd, int flag);
  #define STRTOSZ_DEFSUFFIX_MB	'M'
  #define STRTOSZ_DEFSUFFIX_KB	'K'
  #define STRTOSZ_DEFSUFFIX_B	'B'
-ssize_t strtosz(const char *nptr, char **end);
-ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
+int64_t strtosz(const char *nptr, char **end);
+int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
 
  /* path.c */
  void init_paths(const char *prefix);
qemu-img.c +1 -1
index afd9ed2..6af2a4c 100644
@@ -320,7 +320,7 @@ static int img_create(int argc, char **argv)
 
      /* Get image size, if specified */
      if (optind<  argc) {
-        ssize_t sval;
+        int64_t sval;
          sval = strtosz_suffix(argv[optind++], NULL, STRTOSZ_DEFSUFFIX_B);
          if (sval<  0) {
              error_report("Invalid image size specified! You may use k, M, G or "
vl.c +2 -2
index 78fcef1..93425f4 100644
@@ -804,7 +804,7 @@ static void numa_add(const char *optarg)
          if (get_param_value(option, 128, "mem", optarg) == 0) {
              node_mem[nodenr] = 0;
          } else {
-            ssize_t sval;
+            int64_t sval;
              sval = strtosz(option, NULL);
              if (sval<  0) {
                  fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
@@ -2245,7 +2245,7 @@ int main(int argc, char **argv, char **envp)
                  exit(0);
                  break;
              case QEMU_OPTION_m: {
-                ssize_t value;
+                int64_t value;
 
                  value = strtosz(optarg, NULL);
                  if (value<  0) {
#5 lore raw
Quoted 11 lines · Anthony Liguori
Quoted 8 lines · On 01/05/2011 04:41 AM, [email protected] wrote:

From: Jes Sorensen<[email protected]>

strtosz() needs to return a 64 bit type even on 32 bit architectures. Otherwise qemu-img will fail to create disk images>= 2GB

off_t would be the proper type to use.

I discussed this with Markus, and we both agree that it isn't.

Two reasons, off_t is for filesystem offsets, not random sizes. Second, off_t doesn't have an unsigned type or a max to compare against.

Using int64_t is cleaner and safer.

Cheers,
Jes

#6 lore raw
Quoted 13 lines · Jes Sorensen
Quoted 11 lines · Anthony Liguori
Quoted 9 lines · On 01/05/2011 04:41 AM, [email protected] wrote:

From: Jes Sorensen<[email protected]>

strtosz() needs to return a 64 bit type even on 32 bit architectures. Otherwise qemu-img will fail to create disk images>= 2GB

Quoted 7 lines
Quoted 2 lines

off_t would be the proper type to use.

I discussed this with Markus, and we both agree that it isn't.

Two reasons, off_t is for filesystem offsets, not random sizes. Second, off_t doesn't have an unsigned type or a max to compare against.

That's because the size of off_t depends on whether it's a 32 or 64-bit platform and what FILESIZEBITS is defined to be.

Basically, if you're looking for the type to represent offsets in a file, it's off_t. That's why it exists.

That said, using this to represent memory too, I can buy that as a justification to use int64_t.

Quoted 2 lines

Using int64_t is cleaner and safer.

I wouldn't make such bold claims but I'll concede that one is not significantly better than the other and won't object to int64_t if you feel strongly.

Regards,

Anthony Liguori

Quoted 3 lines

Cheers,
Jes

#7 lore raw
Quoted 3 lines · Anthony Liguori

I wouldn't make such bold claims but I'll concede that one is not significantly better than the other and won't object to int64_t if you feel strongly.

The more I think of it, the more I come to the conclusion that int64_t is the best solution. Since we can in theory have a system where we only allow 32 bit file system offsets, but have > 4GB of memory, int64_t does it best - it is more generic.

Cheers,
Jes