poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 4/4] ios: Remove usage of xalloc from ios.c


From: Tim Rühsen
Subject: [PATCH 4/4] ios: Remove usage of xalloc from ios.c
Date: Thu, 14 May 2020 20:13:34 +0200

2020-05-14  Tim Rühsen  <address@hidden>

        * libpoke/ios.c: Remove include xalloc.h.
        (ios_open): Use malloc with error checking.
        (realloc_string): New helper function.
        (ios_read_string): Use realloc_string instead xrealloc.
        * libpoke/ios.h: New define IOS_ENOMEM.
        * libpoke/pvm.h: New defines PVM_E_NOMEM and PVM_E_NOMEM_MSG.
        * libpoke/pvm.jitter: Raise PVM_E_NOMEM where needed.
---
 ChangeLog          | 10 ++++++++++
 libpoke/ios.c      | 42 ++++++++++++++++++++++++++++++++++--------
 libpoke/ios.h      |  4 +++-
 libpoke/pvm.h      |  3 +++
 libpoke/pvm.jitter |  4 ++++
 5 files changed, 54 insertions(+), 9 deletions(-)

diff --git a/libpoke/ios.c b/libpoke/ios.c
index 38514032..3ec3efaf 100644
--- a/libpoke/ios.c
+++ b/libpoke/ios.c
@@ -17,7 +17,6 @@
  */

 #include <config.h>
-#include <xalloc.h>
 #include <gettext.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -118,12 +117,15 @@ ios_shutdown (void)
 int
 ios_open (const char *handler, uint64_t flags, int set_cur)
 {
-  struct ios *io = NULL;
+  struct ios *io;
   struct ios_dev_if **dev_if = NULL;
   int error = IOD_ERROR, ret;

   /* Allocate and initialize the new IO space.  */
-  io = xmalloc (sizeof (struct ios));
+  io = malloc (sizeof (struct ios));
+  if (!io)
+    return IOS_ENOMEM;
+
   io->id = ios_next_id++;
   io->next = NULL;
   io->bias = 0;
@@ -884,11 +886,23 @@ ios_read_uint (ios io, ios_off offset, int flags,
   return ios_read_int_common (io, offset, flags, bits, endian, value);
 }

+static int realloc_string(char **str, size_t newsize)
+{
+  char *newstr = realloc (*str, newsize);
+
+  if (!newstr)
+    return IOS_ENOMEM;
+
+  *str = newstr;
+  return IOS_OK;
+}
+
 int
 ios_read_string (ios io, ios_off offset, int flags, char **value)
 {
   char *str = NULL;
   size_t i = 0;
+  int ret;

   /* Apply the IOS bias.  */
   offset += ios_get_bias (io);
@@ -902,11 +916,17 @@ ios_read_string (ios io, ios_off offset, int flags, char 
**value)
       do
         {
           if (i % 128 == 0)
-            str = xrealloc (str, i + 128 * sizeof (char));
+            {
+              if ((ret = realloc_string(&str, i + 128 * sizeof (char))) < 0)
+                goto error;
+            }

           if (io->dev_if->pread (io->dev, &str[i], 1,
                                  offset / 8 + i) == IOD_EOF)
-            return IOS_EIOFF;
+            {
+              ret = IOS_EIOFF;
+              goto error;
+            }
         }
       while (str[i++] != '\0');
     }
@@ -918,17 +938,19 @@ ios_read_string (ios io, ios_off offset, int flags, char 
**value)

       do
         {
-          int ret;
           uint64_t abyte;

           if (i % 128 == 0)
-            str = xrealloc (str, i + 128 * sizeof (char));
+            {
+              if ((ret = realloc_string(&str, i + 128 * sizeof (char))) < 0)
+                goto error;
+            }

           ret = ios_read_uint (io, offset, flags, 8,
                                IOS_ENDIAN_MSB, /* Arbitrary.  */
                                &abyte);
           if (ret == IOS_EIOFF)
-            return ret;
+            goto error;

           str[i] = (char) abyte;
           offset += 8;
@@ -938,6 +960,10 @@ ios_read_string (ios io, ios_off offset, int flags, char 
**value)

   *value = str;
   return IOS_OK;
+
+error:
+  free (str);
+  return ret;
 }

 static inline int
diff --git a/libpoke/ios.h b/libpoke/ios.h
index 2bdc2fa6..f19ac841 100644
--- a/libpoke/ios.h
+++ b/libpoke/ios.h
@@ -271,11 +271,13 @@ void ios_set_bias (ios io, ios_off bias)
                          when an end-of-file condition happens in the
                          underlying IO device.  */

-/* the following error code is returned when the IO backend can't
+/* The following error code is returned when the IO backend can't
    handle the specified flags in ios_open. */

 #define IOS_EFLAGS -4 /* Invalid flags specified.  */

+#define IOS_ENOMEM -5 /* Memory allocation failure.  */
+
 /* When reading and writing integers from/to IO spaces, it is needed
    to specify some details on how the integers values are encoded in
    the underlying storage.  The following enumerations provide the
diff --git a/libpoke/pvm.h b/libpoke/pvm.h
index 634dc0af..cc3c7623 100644
--- a/libpoke/pvm.h
+++ b/libpoke/pvm.h
@@ -505,6 +505,9 @@ enum pvm_exit_code
 #define PVM_E_INVAL        14
 #define PVM_E_INVAL_MSG "invalid argument"

+#define PVM_E_NOMEM        15
+#define PVM_E_NOMEM_MSG "out of memory"
+
 typedef struct pvm *pvm;

 /* Initialize a new Poke Virtual Machine and return it.  */
diff --git a/libpoke/pvm.jitter b/libpoke/pvm.jitter
index 5b00b676..b165ae2d 100644
--- a/libpoke/pvm.jitter
+++ b/libpoke/pvm.jitter
@@ -316,6 +316,8 @@ late-header-c
        {                                                                     \
          if (ret == IOS_EIOFF)                                               \
             PVM_RAISE (PVM_E_EOF, PVM_E_EOF_MSG);                            \
+         else if (ret == IOS_ENOMEM)                                         \
+            PVM_RAISE (PVM_E_NOMEM, PVM_E_NOMEM_MSG);                        \
          else                                                                \
             PVM_RAISE (PVM_E_IO, PVM_E_IO_MSG);                              \
          JITTER_TOP_STACK () = PVM_NULL;                                     \
@@ -4617,6 +4619,8 @@ instruction peeks ()
     {
       if (ret == IOS_EIOFF)
          PVM_RAISE (PVM_E_EOF, PVM_E_EOF_MSG);
+      else if (ret == IOS_ENOMEM)
+         PVM_RAISE (PVM_E_NOMEM, PVM_E_NOMEM_MSG);
       else
          PVM_RAISE (PVM_E_IO, PVM_E_IO_MSG);
       JITTER_TOP_STACK () = PVM_NULL;
--
2.26.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]