poke-devel
[Top][All Lists]
Advanced

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

[PATCH 3/3] ios: report IOS_EPERM whenever appropriate


From: Jose E. Marchesi
Subject: [PATCH 3/3] ios: report IOS_EPERM whenever appropriate
Date: Sat, 27 Mar 2021 12:37:11 +0100

This patch makes the read/write IOS functions to return IOS_EPERM when
the IO space doesn't support reading (like in a write-only file) and
reading is needed.

This includes reads of "completing bytes" that happens in certain
writes.

Includes a few tests.

2021-03-27  Jose E. Marchesi  <jemarch@gnu.org>

        * libpoke/ios.c (ios_read_uint): Return IOS_EPERM if the IO space
        is not readable.
        (ios_read_int): Likewise.
        (ios_read_string): Likewise.
        (IOS_GET_C_ERR_CHCK): Likewise.
        * testsuite/poke.map/maps-perm-1.pk: New test.
        * testsuite/poke.map/maps-perm-2.pk: Likewise.
        * testsuite/poke.map/maps-perm-3.pk: Likewise.
        * testsuite/poke.map/maps-perm-4.pk: Likewise.
        * testsuite/Makefile.am (EXTRA_DIST): Add new tests.
---
 ChangeLog                         | 13 +++++++++++++
 libpoke/ios.c                     | 30 +++++++++++++++++++++++-------
 testsuite/Makefile.am             |  4 ++++
 testsuite/poke.map/maps-perm-1.pk |  6 ++++++
 testsuite/poke.map/maps-perm-2.pk |  6 ++++++
 testsuite/poke.map/maps-perm-3.pk |  6 ++++++
 testsuite/poke.map/maps-perm-4.pk | 10 ++++++++++
 7 files changed, 68 insertions(+), 7 deletions(-)
 create mode 100644 testsuite/poke.map/maps-perm-1.pk
 create mode 100644 testsuite/poke.map/maps-perm-2.pk
 create mode 100644 testsuite/poke.map/maps-perm-3.pk
 create mode 100644 testsuite/poke.map/maps-perm-4.pk

diff --git a/ChangeLog b/ChangeLog
index d46d92fe..ed9f1726 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2021-03-27  Jose E. Marchesi  <jemarch@gnu.org>
+
+       * libpoke/ios.c (ios_read_uint): Return IOS_EPERM if the IO space
+       is not readable.
+       (ios_read_int): Likewise.
+       (ios_read_string): Likewise.
+       (IOS_GET_C_ERR_CHCK): Likewise.
+       * testsuite/poke.map/maps-perm-1.pk: New test.
+       * testsuite/poke.map/maps-perm-2.pk: Likewise.
+       * testsuite/poke.map/maps-perm-3.pk: Likewise.
+       * testsuite/poke.map/maps-perm-4.pk: Likewise.
+       * testsuite/Makefile.am (EXTRA_DIST): Add new tests.
+
 2021-03-27  Jose E. Marchesi  <jemarch@gnu.org>
 
        * libpoke/ios.h (IOS_EPERM): Define.
diff --git a/libpoke/ios.c b/libpoke/ios.c
index 75ea05de..68069486 100644
--- a/libpoke/ios.c
+++ b/libpoke/ios.c
@@ -32,13 +32,17 @@
 #include "ios.h"
 #include "ios-dev.h"
 
-#define IOS_GET_C_ERR_CHCK(c, io, off)                                \
-  {                                                                \
-    uint8_t ch;                                                        \
-    int ret = (io)->dev_if->pread ((io)->dev, &ch, 1, off);         \
-    if (ret == IOD_EOF)                                                \
-      return IOS_EIOFF;                                                \
-    (c) = ch;                                                        \
+#define IOS_GET_C_ERR_CHCK(c, io, off)                                  \
+  {                                                                     \
+    uint8_t ch;                                                         \
+    int ret;                                                            \
+    /* The IOS should be readable to get the completing byte.  */       \
+    if (!(io->dev_if->get_flags (io->dev) & IOS_F_READ))                \
+      return IOS_EPERM;                                                 \
+    ret = (io)->dev_if->pread ((io)->dev, &ch, 1, off);                 \
+    if (ret == IOD_EOF)                                                 \
+      return IOS_EIOFF;                                                 \
+    (c) = ch;                                                           \
   }
 
 #define IOS_PUT_C_ERR_CHCK(c, io, len, off)                \
@@ -696,6 +700,10 @@ ios_read_int (ios io, ios_off offset, int flags,
               enum ios_nenc nenc,
               int64_t *value)
 {
+  /* The IOS should be readable.  */
+  if (!(io->dev_if->get_flags (io->dev) & IOS_F_READ))
+    return IOS_EPERM;
+
   /* Apply the IOS bias.  */
   offset += ios_get_bias (io);
 
@@ -820,6 +828,10 @@ ios_read_uint (ios io, ios_off offset, int flags,
                enum ios_endian endian,
                uint64_t *value)
 {
+  /* The IOS should be readable.  */
+  if (!(io->dev_if->get_flags (io->dev) & IOS_F_READ))
+    return IOS_EPERM;
+
   /* Apply the IOS bias.  */
   offset += ios_get_bias (io);
 
@@ -924,6 +936,10 @@ ios_read_string (ios io, ios_off offset, int flags, char 
**value)
   size_t i = 0;
   int ret;
 
+  /* The IOS should be readable.  */
+  if (!(io->dev_if->get_flags (io->dev) & IOS_F_READ))
+    return IOS_EPERM;
+
   /* Apply the IOS bias.  */
   offset += ios_get_bias (io);
 
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 28968938..87f4aa7f 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -264,6 +264,10 @@ EXTRA_DIST = \
   poke.map/maps-offsets-1.pk \
   poke.map/maps-offsets-2.pk \
   poke.map/maps-offsets.pk \
+  poke.map/maps-perm-1.pk \
+  poke.map/maps-perm-2.pk \
+  poke.map/maps-perm-3.pk \
+  poke.map/maps-perm-4.pk \
   poke.map/maps-simple-1.pk \
   poke.map/maps-simple-2.pk \
   poke.map/maps-simple-3.pk \
diff --git a/testsuite/poke.map/maps-perm-1.pk 
b/testsuite/poke.map/maps-perm-1.pk
new file mode 100644
index 00000000..d8c5fccf
--- /dev/null
+++ b/testsuite/poke.map/maps-perm-1.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x51 0x23 0x45 0x67 0x89 0xab 0xcd 0xef} foo } */
+
+/* { dg-command { var fd = open ("foo", IOS_M_WRONLY) } } */
+/* { dg-command { try byte @ fd: 1#B; catch if E_perm { print "caught\n"; } } 
} */
+/* { dg-output "caught" } */
diff --git a/testsuite/poke.map/maps-perm-2.pk 
b/testsuite/poke.map/maps-perm-2.pk
new file mode 100644
index 00000000..a8ba8c3b
--- /dev/null
+++ b/testsuite/poke.map/maps-perm-2.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x51 0x23 0x45 0x00 0x89 0xab 0xcd 0xef} foo } */
+
+/* { dg-command { var fd = open ("foo", IOS_M_WRONLY) } } */
+/* { dg-command { try string @ fd: 0#B; catch if E_perm { print "caught\n"; } 
} } */
+/* { dg-output "caught" } */
diff --git a/testsuite/poke.map/maps-perm-3.pk 
b/testsuite/poke.map/maps-perm-3.pk
new file mode 100644
index 00000000..5618a40d
--- /dev/null
+++ b/testsuite/poke.map/maps-perm-3.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x51 0x23 0x45 0x67 0x89 0xab 0xcd 0xef} foo } */
+
+/* { dg-command { var fd = open ("foo", IOS_M_WRONLY) } } */
+/* { dg-command { try int<32> @ fd: 1#B; catch if E_perm { print "caught\n"; } 
} } */
+/* { dg-output "caught" } */
diff --git a/testsuite/poke.map/maps-perm-4.pk 
b/testsuite/poke.map/maps-perm-4.pk
new file mode 100644
index 00000000..fcfa53d4
--- /dev/null
+++ b/testsuite/poke.map/maps-perm-4.pk
@@ -0,0 +1,10 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x51 0x23 0x45 0x67 0x89 0xab 0xcd 0xef} foo } */
+
+/* This tests that trying to write partial bytes in non-readable IO
+   spaces triggers E_perm.  This is because the operation requires
+   reading "completing bytes".  */
+
+/* { dg-command { var fd = open ("foo", IOS_M_WRONLY) } } */
+/* { dg-command { try uint<3> @ fd : 1#B = 2; catch if E_perm { print 
"caught\n"; } } } */
+/* { dg-output "caught" } */
-- 
2.25.0.2.g232378479e




reply via email to

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