[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] poke: Change addresses in commands to unsigned
|
From: |
Andreas Klinger |
|
Subject: |
[PATCH] poke: Change addresses in commands to unsigned |
|
Date: |
Fri, 26 Jan 2024 09:06:45 +0100 |
2024-01-25 Andreas Klinger <ak@it-klinger.de>
* poke/pk-cmd.h: New command arg type PK_CMD_ARG_UINT
* poke/pk-cmd.c (pk_atou): New function
* poke/pk-cmd-ios.c (pk_cmd_mmap, pk_cmd_sub):
Change base and size to PK_CMD_ARG_UINT
---
This patch is also available in the poke repo as branch
"anderl/unsigned-address-command".
Background:
Addresses are interpreted as integer so far. For example on a 32 bit system it
can happen that one is using the address 0x80000000 or a higher one which is
interpreted as negative value and thus a segmentation fault happens when trying
to use it.
poke/pk-cmd-ios.c | 20 ++++++++++----------
poke/pk-cmd.c | 32 ++++++++++++++++++++++++++++++++
poke/pk-cmd.h | 3 +++
3 files changed, 45 insertions(+), 10 deletions(-)
diff --git a/poke/pk-cmd-ios.c b/poke/pk-cmd-ios.c
index 034cc09c..54839fa2 100644
--- a/poke/pk-cmd-ios.c
+++ b/poke/pk-cmd-ios.c
@@ -118,11 +118,11 @@ pk_cmd_sub (int argc, struct pk_cmd_arg argv[], uint64_t
uflags)
if (ios == NULL)
return 0;
- assert (PK_CMD_ARG_TYPE (argv[2]) == PK_CMD_ARG_INT);
- base = PK_CMD_ARG_INT (argv[2]);
+ assert (PK_CMD_ARG_TYPE (argv[2]) == PK_CMD_ARG_UINT);
+ base = PK_CMD_ARG_UINT (argv[2]);
- assert (PK_CMD_ARG_TYPE (argv[3]) == PK_CMD_ARG_INT);
- size = PK_CMD_ARG_INT (argv[3]);
+ assert (PK_CMD_ARG_TYPE (argv[3]) == PK_CMD_ARG_UINT);
+ size = PK_CMD_ARG_UINT (argv[3]);
name = (PK_CMD_ARG_TYPE (argv[4]) == PK_CMD_ARG_STR
? PK_CMD_ARG_STR (argv[4])
@@ -591,11 +591,11 @@ pk_cmd_mmap (int argc, struct pk_cmd_arg argv[], uint64_t
uflags)
/* Create a new IO space. */
const char *filename = PK_CMD_ARG_STR (argv[1]);
- assert (PK_CMD_ARG_TYPE (argv[2]) == PK_CMD_ARG_INT);
- base = PK_CMD_ARG_INT (argv[2]);
+ assert (PK_CMD_ARG_TYPE (argv[2]) == PK_CMD_ARG_UINT);
+ base = PK_CMD_ARG_UINT (argv[2]);
- assert (PK_CMD_ARG_TYPE (argv[3]) == PK_CMD_ARG_INT);
- size = PK_CMD_ARG_INT (argv[3]);
+ assert (PK_CMD_ARG_TYPE (argv[3]) == PK_CMD_ARG_UINT);
+ size = PK_CMD_ARG_UINT (argv[3]);
if (access (filename, F_OK) == 0)
@@ -636,7 +636,7 @@ const struct pk_cmd proc_cmd =
{"proc", "i", PK_PROC_UFLAGS, 0, NULL, NULL, pk_cmd_proc, ".proc PID", NULL};
const struct pk_cmd sub_cmd =
- {"sub", "s,i,i,?s", "", 0, NULL, NULL, pk_cmd_sub, ".sub IOS, BASE, SIZE,
[NAME]",
+ {"sub", "s,u,u,?s", "", 0, NULL, NULL, pk_cmd_sub, ".sub IOS, BASE, SIZE,
[NAME]",
poke_completion_function};
const struct pk_cmd mem_cmd =
@@ -649,7 +649,7 @@ const struct pk_cmd nbd_cmd =
#ifdef HAVE_MMAP
const struct pk_cmd mmap_cmd =
- {"mmap", "s,i,i", "", 0, NULL, NULL, pk_cmd_mmap, ".mmap FILE-NAME, BASE,
SIZE",
+ {"mmap", "s,u,u", "", 0, NULL, NULL, pk_cmd_mmap, ".mmap FILE-NAME, BASE,
SIZE",
rl_filename_completion_function};
#endif
diff --git a/poke/pk-cmd.c b/poke/pk-cmd.c
index 76dd4cf3..f571342a 100644
--- a/poke/pk-cmd.c
+++ b/poke/pk-cmd.c
@@ -110,6 +110,23 @@ skip_blanks (const char *p)
return p;
}
+static inline int
+pk_atou (const char **p, uint64_t *number)
+{
+ unsigned long int li;
+ char *end;
+
+ errno = 0;
+ li = strtoull (*p, &end, 0);
+ if ((errno != 0 && li == 0)
+ || end == *p)
+ return 0;
+
+ *number = li;
+ *p = end;
+ return 1;
+}
+
static inline int
pk_atoi (const char **p, int64_t *number)
{
@@ -409,6 +426,21 @@ pk_cmd_exec_1 (const char *str, struct pk_trie *cmds_trie,
char *prefix)
}
}
+ break;
+ case 'u':
+ /* Parse an unsigned integer. */
+ p = skip_blanks (p);
+ if (pk_atou (&p, &(argv[argc].val.uinteger))
+ && (*a == 'i' || argv[argc].val.uinteger >= 0))
+ {
+ p = skip_blanks (p);
+ if (*p == ',' || *p == '\0')
+ {
+ argv[argc].type = PK_CMD_ARG_UINT;
+ match = 1;
+ }
+ }
+
break;
case 's':
{
diff --git a/poke/pk-cmd.h b/poke/pk-cmd.h
index f60a6c25..65d9af4c 100644
--- a/poke/pk-cmd.h
+++ b/poke/pk-cmd.h
@@ -28,11 +28,13 @@ enum pk_cmd_arg_type
{
PK_CMD_ARG_NULL,
PK_CMD_ARG_INT,
+ PK_CMD_ARG_UINT,
PK_CMD_ARG_STR
};
#define PK_CMD_ARG_TYPE(arg) ((arg).type)
#define PK_CMD_ARG_INT(arg) ((arg).val.integer)
+#define PK_CMD_ARG_UINT(arg) ((arg).val.uinteger)
#define PK_CMD_ARG_STR(arg) ((arg).val.str)
struct pk_cmd_arg
@@ -41,6 +43,7 @@ struct pk_cmd_arg
union
{
int64_t integer;
+ uint64_t uinteger;
char *str;
int64_t tag;
} val;
--
2.39.2
signature.asc
Description: PGP signature
- [PATCH] poke: Change addresses in commands to unsigned,
Andreas Klinger <=