[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v4 14/25] gdbstub: Dynamically allocate target.xml buffer
From: |
Akihiko Odaki |
Subject: |
[PATCH v4 14/25] gdbstub: Dynamically allocate target.xml buffer |
Date: |
Wed, 16 Aug 2023 23:51:35 +0900 |
There is no guarantee that target.xml fits in 1024 bytes, and the fixed
buffer length requires tedious buffer overflow check. Dynamically
allocate the target.xml buffer to resolve these problems.
Suggested-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
gdbstub/internals.h | 2 +-
gdbstub/gdbstub.c | 44 ++++++++++++++++++++++++--------------------
gdbstub/softmmu.c | 2 +-
3 files changed, 26 insertions(+), 22 deletions(-)
diff --git a/gdbstub/internals.h b/gdbstub/internals.h
index f2b46cce41..4876ebd74f 100644
--- a/gdbstub/internals.h
+++ b/gdbstub/internals.h
@@ -33,7 +33,7 @@ typedef struct GDBProcess {
uint32_t pid;
bool attached;
- char target_xml[1024];
+ char *target_xml;
} GDBProcess;
enum RSState {
diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c
index c2ce970c98..452b5bf0ef 100644
--- a/gdbstub/gdbstub.c
+++ b/gdbstub/gdbstub.c
@@ -366,33 +366,37 @@ static const char *get_feature_xml(const char *p, const
char **newp,
name = NULL;
if (strncmp(p, "target.xml", len) == 0) {
- char *buf = process->target_xml;
- const size_t buf_sz = sizeof(process->target_xml);
-
/* Generate the XML description for this CPU. */
- if (!buf[0]) {
+ if (!process->target_xml) {
+ g_autoptr(GPtrArray) a = g_ptr_array_new_with_free_func(g_free);
GDBRegisterState *r;
- pstrcat(buf, buf_sz,
- "<?xml version=\"1.0\"?>"
- "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
- "<target>");
+ g_ptr_array_add(
+ a,
+ g_strdup("<?xml version=\"1.0\"?>"
+ "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
+ "<target>"));
if (cc->gdb_arch_name) {
- pstrcat(buf, buf_sz, "<architecture>");
- pstrcat(buf, buf_sz, cc->gdb_arch_name(cpu));
- pstrcat(buf, buf_sz, "</architecture>");
+ g_ptr_array_add(
+ a,
+ g_markup_printf_escaped("<architecture>%s</architecture>",
+ cc->gdb_arch_name(cpu)));
}
- pstrcat(buf, buf_sz, "<xi:include href=\"");
- pstrcat(buf, buf_sz, cc->gdb_core_feature->xmlname);
- pstrcat(buf, buf_sz, "\"/>");
+ g_ptr_array_add(
+ a,
+ g_markup_printf_escaped("<xi:include href=\"%s\"/>",
+ cc->gdb_core_feature->xmlname));
for (r = cpu->gdb_regs; r; r = r->next) {
- pstrcat(buf, buf_sz, "<xi:include href=\"");
- pstrcat(buf, buf_sz, r->feature->xmlname);
- pstrcat(buf, buf_sz, "\"/>");
+ g_ptr_array_add(
+ a,
+ g_markup_printf_escaped("<xi:include href=\"%s\"/>",
+ r->feature->xmlname));
}
- pstrcat(buf, buf_sz, "</target>");
+ g_ptr_array_add(a, g_strdup("</target>"));
+ g_ptr_array_add(a, NULL);
+ process->target_xml = g_strjoinv(NULL, (void *)a->pdata);
}
- return buf;
+ return process->target_xml;
}
if (cc->gdb_get_dynamic_xml) {
char *xmlname = g_strndup(p, len);
@@ -2270,6 +2274,6 @@ void gdb_create_default_process(GDBState *s)
process = &s->processes[s->process_num - 1];
process->pid = pid;
process->attached = false;
- process->target_xml[0] = '\0';
+ process->target_xml = NULL;
}
diff --git a/gdbstub/softmmu.c b/gdbstub/softmmu.c
index f509b7285d..5282324764 100644
--- a/gdbstub/softmmu.c
+++ b/gdbstub/softmmu.c
@@ -293,7 +293,7 @@ static int find_cpu_clusters(Object *child, void *opaque)
assert(cluster->cluster_id != UINT32_MAX);
process->pid = cluster->cluster_id + 1;
process->attached = false;
- process->target_xml[0] = '\0';
+ process->target_xml = NULL;
return 0;
}
--
2.41.0
- [PATCH v4 04/25] gdbstub: Introduce gdb_find_static_feature(), (continued)
- [PATCH v4 04/25] gdbstub: Introduce gdb_find_static_feature(), Akihiko Odaki, 2023/08/16
- [PATCH v4 05/25] target/arm: Move the reference to arm-core.xml, Akihiko Odaki, 2023/08/16
- [PATCH v4 06/25] hw/core/cpu: Replace gdb_core_xml_file with gdb_core_feature, Akihiko Odaki, 2023/08/16
- [PATCH v4 07/25] gdbstub: Introduce GDBFeatureBuilder, Akihiko Odaki, 2023/08/16
- [PATCH v4 08/25] target/arm: Use GDBFeature for dynamic XML, Akihiko Odaki, 2023/08/16
- [PATCH v4 09/25] target/ppc: Use GDBFeature for dynamic XML, Akihiko Odaki, 2023/08/16
- [PATCH v4 10/25] target/riscv: Use GDBFeature for dynamic XML, Akihiko Odaki, 2023/08/16
- [PATCH v4 11/25] gdbstub: Use GDBFeature for gdb_register_coprocessor, Akihiko Odaki, 2023/08/16
- [PATCH v4 12/25] gdbstub: Use GDBFeature for GDBRegisterState, Akihiko Odaki, 2023/08/16
- [PATCH v4 13/25] hw/core/cpu: Return static value with gdb_arch_name(), Akihiko Odaki, 2023/08/16
- [PATCH v4 14/25] gdbstub: Dynamically allocate target.xml buffer,
Akihiko Odaki <=
- [PATCH v4 15/25] gdbstub: Simplify XML lookup, Akihiko Odaki, 2023/08/16
- [PATCH v4 16/25] hw/core/cpu: Remove gdb_get_dynamic_xml member, Akihiko Odaki, 2023/08/16
- [PATCH v4 17/25] gdbstub: Add members to identify registers to GDBFeature, Akihiko Odaki, 2023/08/16
- [PATCH v4 18/25] hw/core/cpu: Add a parameter to gdb_read_register/gdb_write_register, Akihiko Odaki, 2023/08/16
- [PATCH v4 19/25] gdbstub: Hide gdb_has_xml, Akihiko Odaki, 2023/08/16
- [PATCH v4 20/25] gdbstub: Expose functions to read registers, Akihiko Odaki, 2023/08/16
- [PATCH v4 21/25] cpu: Call plugin hooks only when ready, Akihiko Odaki, 2023/08/16
- [PATCH v4 22/25] plugins: Allow to read registers, Akihiko Odaki, 2023/08/16
- [PATCH v4 23/25] contrib/plugins: Allow to log registers, Akihiko Odaki, 2023/08/16
- [PATCH v4 24/25] plugins: Support C++, Akihiko Odaki, 2023/08/16