From: Jens Remus <jremus@linux.ibm.com>
To: binutils@sourceware.org
Cc: Jens Remus <jremus@linux.ibm.com>,
Andreas Krebbel <krebbel@linux.ibm.com>,
Nick Clifton <nickc@redhat.com>
Subject: [PATCH v2 5/7] s390: Use safe string functions and length macros in s390-mkopc
Date: Fri, 15 Dec 2023 15:36:14 +0100 [thread overview]
Message-ID: <20231215143616.820185-6-jremus@linux.ibm.com> (raw)
In-Reply-To: <20231215143616.820185-1-jremus@linux.ibm.com>
Use strncpy() and snprintf() instead of strcpy() and strcat(). Define
and use macros for string lengths, such as mnemonic, instruction
format, and instruction description.
This is a mechanical change, although some buffers have increased in
length by one character. This has been confirmed by verifying that the
generated opcode/s390-opc.tab is unchanged.
opcodes/
* s390-mkopc.c: Use strncpy() and strncat().
Suggested-by: Nick Clifton <nickc@redhat.com>
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
Reviewed-by: Andreas Krebbel <krebbel@linux.ibm.com>
---
opcodes/s390-mkopc.c | 77 ++++++++++++++++++++++++++++++--------------
1 file changed, 52 insertions(+), 25 deletions(-)
diff --git a/opcodes/s390-mkopc.c b/opcodes/s390-mkopc.c
index aad093f18ff..eae0397d497 100644
--- a/opcodes/s390-mkopc.c
+++ b/opcodes/s390-mkopc.c
@@ -25,6 +25,19 @@
#include <string.h>
#include "opcode/s390.h"
+#define STRINGIFY(x) _STRINGIFY(x)
+#define _STRINGIFY(x) #x
+
+/* Length of strings without terminating '\0' character. */
+#define MAX_OPCODE_LEN 15
+#define MAX_MNEMONIC_LEN 15
+#define MAX_FORMAT_LEN 15
+#define MAX_DESCRIPTION_LEN 79
+
+#define MAX_CPU_LEN 15
+#define MAX_MODES_LEN 15
+#define MAX_FLAGS_LEN 79
+
/* Return code. */
int return_code = EXIT_SUCCESS;
@@ -44,9 +57,9 @@ print_error (const char *fmt, ...)
struct op_struct
{
- char opcode[16];
- char mnemonic[16];
- char format[16];
+ char opcode[MAX_OPCODE_LEN + 1];
+ char mnemonic[MAX_MNEMONIC_LEN + 1];
+ char format[MAX_FORMAT_LEN + 1];
int mode_bits;
int min_cpu;
int flags;
@@ -108,9 +121,12 @@ insertOpcode (char *opcode, char *mnemonic, char *format,
break;
for (k = no_ops; k > ix; k--)
op_array[k] = op_array[k-1];
- strcpy(op_array[ix].opcode, opcode);
- strcpy(op_array[ix].mnemonic, mnemonic);
- strcpy(op_array[ix].format, format);
+ strncpy (op_array[ix].opcode, opcode, MAX_OPCODE_LEN);
+ op_array[ix].opcode[MAX_OPCODE_LEN] = '\0';
+ strncpy (op_array[ix].mnemonic, mnemonic, MAX_MNEMONIC_LEN);
+ op_array[ix].mnemonic[MAX_MNEMONIC_LEN] = '\0';
+ strncpy (op_array[ix].format, format, MAX_FORMAT_LEN);
+ op_array[ix].format[MAX_FORMAT_LEN] = '\0';
op_array[ix].sort_value = sort_value;
op_array[ix].no_nibbles = no_nibbles;
op_array[ix].min_cpu = min_cpu;
@@ -180,9 +196,9 @@ insertExpandedMnemonic (char *opcode, char *mnemonic, char *format,
int min_cpu, int mode_bits, int flags)
{
char *tag;
- char prefix[15];
- char suffix[15];
- char number[15];
+ char prefix[MAX_MNEMONIC_LEN + 1];
+ char suffix[MAX_MNEMONIC_LEN + 1];
+ char number[MAX_MNEMONIC_LEN + 1];
int mask_start, i = 0, tag_found = 0, reading_number = 0;
int number_p = 0, suffix_p = 0, prefix_p = 0;
const struct s390_cond_ext_format *ext_table;
@@ -263,12 +279,17 @@ insertExpandedMnemonic (char *opcode, char *mnemonic, char *format,
for (i = 0; i < ext_table_length; i++)
{
- char new_mnemonic[15];
+ char new_mnemonic[MAX_MNEMONIC_LEN + 1];
- strcpy (new_mnemonic, prefix);
opcode[mask_start] = ext_table[i].nibble;
- strcat (new_mnemonic, ext_table[i].extension);
- strcat (new_mnemonic, suffix);
+
+ if (snprintf (new_mnemonic, sizeof (new_mnemonic), "%s%s%s", prefix,
+ ext_table[i].extension, suffix) >= sizeof (new_mnemonic))
+ {
+ print_error ("Mnemonic: \"%s\": Concatenated mnemonic exceeds max. length\n", mnemonic);
+ return;
+ }
+
insertOpcode (opcode, new_mnemonic, format, min_cpu, mode_bits, flags);
}
return;
@@ -338,13 +359,13 @@ main (void)
make an entry into the opcode table. */
while (fgets (currentLine, sizeof (currentLine), stdin) != NULL)
{
- char opcode[16];
- char mnemonic[16];
- char format[16];
- char description[80];
- char cpu_string[16];
- char modes_string[16];
- char flags_string[80];
+ char opcode[MAX_OPCODE_LEN + 1];
+ char mnemonic[MAX_MNEMONIC_LEN + 1];
+ char format[MAX_FORMAT_LEN + 1];
+ char description[MAX_DESCRIPTION_LEN + 1];
+ char cpu_string[MAX_CPU_LEN + 1];
+ char modes_string[MAX_MODES_LEN + 1];
+ char flags_string[MAX_FLAGS_LEN + 1];
int min_cpu;
int mode_bits;
int flag_bits;
@@ -353,11 +374,17 @@ main (void)
if (currentLine[0] == '#' || currentLine[0] == '\n')
continue;
- memset (opcode, 0, 8);
- num_matched =
- sscanf (currentLine, "%15s %15s %15s \"%79[^\"]\" %15s %15s %79[^\n]",
- opcode, mnemonic, format, description,
- cpu_string, modes_string, flags_string);
+ memset (opcode, '\0', sizeof(opcode));
+ num_matched = sscanf (currentLine,
+ "%" STRINGIFY (MAX_OPCODE_LEN) "s "
+ "%" STRINGIFY (MAX_MNEMONIC_LEN) "s "
+ "%" STRINGIFY (MAX_FORMAT_LEN) "s "
+ "\"%" STRINGIFY (MAX_DESCRIPTION_LEN) "[^\"]\" "
+ "%" STRINGIFY (MAX_CPU_LEN) "s "
+ "%" STRINGIFY (MAX_MODES_LEN) "s "
+ "%" STRINGIFY (MAX_FLAGS_LEN) "[^\n]",
+ opcode, mnemonic, format, description,
+ cpu_string, modes_string, flags_string);
if (num_matched != 6 && num_matched != 7)
{
print_error ("Couldn't scan line %s\n", currentLine);
--
2.40.1
next prev parent reply other threads:[~2023-12-15 14:36 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-15 14:36 [PATCH v2 0/7] s390: Optionally print instruction description in disassembly Jens Remus
2023-12-15 14:36 ` [PATCH v2 1/7] s390: Fix build when using EXEEXT_FOR_BUILD Jens Remus
2023-12-15 14:36 ` [PATCH v2 2/7] s390: Align letter case of instruction descriptions Jens Remus
2023-12-15 14:36 ` [PATCH v2 3/7] s390: Provide IBM z16 (arch14) " Jens Remus
2023-12-15 14:36 ` [PATCH v2 4/7] s390: Enhance error handling in s390-mkopc Jens Remus
2023-12-15 14:36 ` Jens Remus [this message]
2023-12-15 14:36 ` [PATCH v2 6/7] s390: Optionally print instruction description in disassembly Jens Remus
2023-12-15 14:36 ` [PATCH v2 7/7] s390: Add suffix to conditional branch instruction descriptions Jens Remus
2023-12-18 12:14 ` [PATCH v2 0/7] s390: Optionally print instruction description in disassembly Nick Clifton
2023-12-20 10:56 ` Andreas Krebbel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231215143616.820185-6-jremus@linux.ibm.com \
--to=jremus@linux.ibm.com \
--cc=binutils@sourceware.org \
--cc=krebbel@linux.ibm.com \
--cc=nickc@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).