From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id BBF8A3856DE6 for ; Fri, 15 Dec 2023 14:36:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BBF8A3856DE6 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BBF8A3856DE6 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702650987; cv=none; b=dfqa1l2VbH9qpnqmL7q9/QfHj4GODNXNhtZTPaEQTBbLaYGH7TvzCW56WOdmcTvKUgkiMscqhoK2y3kYzWbwFvLH6uSwvJ283AR83LXvShZw+P+g0QfKyXIWJ1AIIkO/4jdGUC9nD6IgNKDkO/9lIRQxKsJX8PemwfSw6Yq1ZxI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702650987; c=relaxed/simple; bh=Wm7PqqWzRoD/AIwWYiq96fFHZjEGnPt/Yf5w9LYmAPk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=rcNojDpPFUNNi6FNGN2CViFzuVfddrWKgc/zsKb3pz76xB4K4CeYMu7KDr7b3e1FEhAQP3BSd8ARd4TvCrVHqQUn4CpXqTh7zuGbPS5Y8iPOY6UmctZiMbAF6ZdLnBFDlQraCuK6smpIUxN1xsFyvBlg/hrHeVWSoYqqGa/O318= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0353723.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3BFEC5LJ018225 for ; Fri, 15 Dec 2023 14:36:24 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=VnIgUWMrP7BhpaZYmhZU9AIyhLvsaNqSpdvGVI3H/rg=; b=BB849uR/RKxrgdq4+XwdyminYnOgEM6XwMOUYgDE3dMBEiFcaXLgWzdnTu12UBqePrLu x4GmcS9vbLnD6G31s2mHpT+FftmvDEQP5HxldU2p17T4WN2JcUT16ZtgPFkEPD1j+9h1 NYiy48qtqF2nkI9I6Nr7TTMbNdOqlkV/8ne+etgNUUVpYFwfJsJL/ZcPm76VEZSBZ+WY 05V1p5nhTV0MT+ewAsqBAkB1vxPh2600KmyB7gguznh0lypAZUcJzeClUQIakv84u6xI 6L0WtzzSe7x4SvzCnU/sMimbbKJ3F0OGCpoDWyHyixZjsxY1maJChP/q/KZoo3M5jScr UA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3v0p154gcd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 15 Dec 2023 14:36:23 +0000 Received: from m0353723.ppops.net (m0353723.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 3BFECkr0022093 for ; Fri, 15 Dec 2023 14:36:23 GMT Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3v0p154gc9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 15 Dec 2023 14:36:23 +0000 Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 3BFDlAAY012616; Fri, 15 Dec 2023 14:36:23 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3uw3jpgku5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 15 Dec 2023 14:36:22 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 3BFEaJAo15008434 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Dec 2023 14:36:19 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C7FBB20040; Fri, 15 Dec 2023 14:36:19 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9B9B52004B; Fri, 15 Dec 2023 14:36:19 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Fri, 15 Dec 2023 14:36:19 +0000 (GMT) From: Jens Remus To: binutils@sourceware.org Cc: Jens Remus , Andreas Krebbel , Nick Clifton 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 Message-Id: <20231215143616.820185-6-jremus@linux.ibm.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231215143616.820185-1-jremus@linux.ibm.com> References: <20231215143616.820185-1-jremus@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: 7U57dJdtgvDKl0Jin8wZTIrbAtaAmZI_ X-Proofpoint-GUID: hoYImUmrF0z2sD2OLVr1njZWRCQgaWd1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-12-15_08,2023-12-14_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 malwarescore=0 bulkscore=0 phishscore=0 impostorscore=0 suspectscore=0 adultscore=0 spamscore=0 mlxlogscore=762 lowpriorityscore=0 priorityscore=1501 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2312150099 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 Signed-off-by: Jens Remus Reviewed-by: Andreas Krebbel --- 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 #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