From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id AF40D3858406 for ; Wed, 23 Feb 2022 00:47:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AF40D3858406 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21MN838B021799 for ; Wed, 23 Feb 2022 00:47:30 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=message-id : date : subject : to : references : from : in-reply-to : content-type : content-transfer-encoding : mime-version; s=corp-2021-07-09; bh=97TFSCWJmuNI7s4v/rXzTzCAnbFtyZfV/sPgpgjgsh4=; b=dQaRLHP4a5J+JTrdjiERHBhgvJvfcv0I7CZEV8mlQvVz08w82AN0gmfw9rjQUmdl3NuQ N+16dX0YvL2Z2rjknycRe2cEoUTVMH82LIRzPZ8oXS9oK1G/In4omCBA9LyYHbOVLqVr DYAcJDjZCqr2TENnkWsn30bskIWQv4VI3/ELQUpzBWxSosN/+vaqyDzwgk1v4fZmZ3LT 3f4CRj5pOm/I1l7D4Eq6sE9kVRXait3pgxd1GG9d4fV8QUgboIVNGGKwwhLsfNZGHcng GS1kv5B4nEqRKinQmuNBJ991Bcyxg57SfQ+PLzER7Y6HFx1RwB5Yk/tZxvxtKjdUU7Q5 +w== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3ecvar2e71-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 23 Feb 2022 00:47:30 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 21N0KrX0058799 for ; Wed, 23 Feb 2022 00:47:29 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2169.outbound.protection.outlook.com [104.47.58.169]) by aserp3030.oracle.com with ESMTP id 3eapkgt49u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 23 Feb 2022 00:47:29 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IBoAwwQ2AsnTnL+93tjI+/UqHAaYYz9gAAt6AWQ2AYTstz/3IO+YJEQhXYL2lrIIXzHdHWZSbG8IXoeZLEta3n9wLmi2BinY4RtPiulHmV19oDpG9+JE2t1eGkKOAnE+DD67eubT0z6Ai8M6TQb4fif4RtZRJIxiOpjdaGgvFXSqNDmP4jT/FpB+vuK9xs3k+S1fiUW4v8LPXeKah3xElUd1iNUbsEiSxv3YoADll0l9woAdifQZFr7z8pR9a4/CVHa7gPBJ6L+wRvgZUC6vj8d4eQNSi9fvdIC6diDeX34x5GZBxDyKiQeUtDxh8Nze3GWCnbcyktVZHMmyIF4RRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=97TFSCWJmuNI7s4v/rXzTzCAnbFtyZfV/sPgpgjgsh4=; b=XkwKeIhchwk5wnj1zdjEusVbZmXDdg9ihYjjwnRgVOSSgCVda3xsrB0iuk+FA/VeIfn/W3tF+/9nsKtX5qUdZXqcE2oyix5xPxaIAVW3Kl95nSlF0eFjocSED5ecXuDSiKYx4VLgQfE58jdUxUo6/skZH6pm4jBi1duq/ir116/r3TsEc6i5SksaVNrILboiD7RMT6cxSybvh6WiS+Gq4/U7uvjwbIsZNISrftLWrRX4TtOHZjWrAF502izh5xZ830gN8mxzWEJrTaJA5aCdEn/73dBfeZT7iLKnH2k8m2y4VrfZd7v6Xpsuwi+B8xHRyOt7/3tlz85dKgO5khexLw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=97TFSCWJmuNI7s4v/rXzTzCAnbFtyZfV/sPgpgjgsh4=; b=uN1WnjyxYKnSmq+QebRF9rRYRpX2+YLHVuz/81p6cHtp4m8QKTwFLf8pUbrVALbHElBwrW8h7g3KCYP/N1O1GckA3kQFpofyvYForlKZpP2TbOswVDP7KgzBydp2ZWXgrw4lH/P+WZIiFSqzuBWSKhXc6FGyqeaLmZCMS6VPCs8= Received: from BYAPR10MB3208.namprd10.prod.outlook.com (2603:10b6:a03:159::10) by CY4PR1001MB2328.namprd10.prod.outlook.com (2603:10b6:910:4a::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4995.24; Wed, 23 Feb 2022 00:47:27 +0000 Received: from BYAPR10MB3208.namprd10.prod.outlook.com ([fe80::3d07:ffdd:63e2:a731]) by BYAPR10MB3208.namprd10.prod.outlook.com ([fe80::3d07:ffdd:63e2:a731%4]) with mapi id 15.20.4995.027; Wed, 23 Feb 2022 00:47:26 +0000 Message-ID: Date: Tue, 22 Feb 2022 18:47:21 -0600 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.6.0 Subject: Re: strcpy and strcat seem to lead to a stack overflow Content-Language: en-US To: gcc@gcc.gnu.org References: From: Patrick McGehearty In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-ClientProxiedBy: SA9PR13CA0049.namprd13.prod.outlook.com (2603:10b6:806:22::24) To BYAPR10MB3208.namprd10.prod.outlook.com (2603:10b6:a03:159::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 968d5172-ebf1-479d-6b48-08d9f6660fcc X-MS-TrafficTypeDiagnostic: CY4PR1001MB2328:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GrtcaWhfQRN7C1X1GIFEsCzNR1EQ2i43DTsjySOMfH/IQtr1xOwhwzL/8EFDmgrd+1cSrVYz5zVjt2O2t/ozsmMsh3XcKsKKgJ2EL+dmGqwD3E9Mqrvluebkqhi6zZreHNlySKhLrbHQB3gtgo/j7r+SC85OeNfsLDgDwDohS18GF5xUV9nxpMSf49NERZDCJtcXUivX2ERSU1gyBLqUq9xDjJJP8j8hxw87AW65HGzCbImSsqyJpADuuI87ymNrioujcEII7XLfCAPw1vrqhfuq4YLw/ez/w3JYAneHVsKoirIhGTJz/8x8Ye2bqEd+pyVVBgY4jgpIvxU6XlhHRK6Xhx0z/k2a6Tb66B/XogfDa4korFlflsdor2J1yx2VVznfWn7/91dDAv9a/uvtLkdGs5pgqVcuktVk0hMCDa0RE8eTRD/BWoJLHTlW569DB1+h1hcIFy4WKO3Vv5cTi76G1HTz0mLiBMdaisvhPxY2YIU5f0Wvc/hvwgDYUIFJ0LQ21orqPs23SpCZHvmHAsn7KWAby1GRXVuavfRRRcdrhrQgYM/m7unxPxmuxQ9gif447ozce5NjTBMP2zbMXZRtq5Dzx8hl9J9p6E6TCyLOgiME2gfFBH4CN6W43hG0TLKnvhXZAZ3L9MUR/N34xLLZG08GgkJZfcU9K7ySm4+CT4QCv5HxJPySrvtz8IadTcGZjIQK+PcHuY2fU+IS+Ccqso++TcfvclGO48+2y2YCj4SnsgRq5g+1EarsLKb8S0ZbmJikF7ECGFRxX0HcLA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BYAPR10MB3208.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(316002)(8676002)(6916009)(186003)(26005)(5660300002)(66946007)(31686004)(66476007)(66556008)(508600001)(6666004)(53546011)(52116002)(6506007)(6512007)(2906002)(2616005)(6486002)(38350700002)(38100700002)(31696002)(86362001)(83380400001)(8936002)(36756003)(45980500001)(43740500002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?WDFwdDgxekRETHBhbEF0dThFMEVPay9kU0x1c0trdXBycXZraEtJNndEdm41?= =?utf-8?B?aFFvV0dLZ3ZwYVFwbzV0TW1UMnczWnlXL0tZQ1VucUxGcGRyTGkrSUhGbDR5?= =?utf-8?B?djhFV2dXSzlYMzhwQ1VQQzJOVGJtVnVXdGVESGpUT29GdHdRK2pUWHI4RmhB?= =?utf-8?B?WUhHMlJvamdGbTg1Z3VNbDN3MXlndmdsSjYvdldTWjEzT2E4MFE4NkJ3bEQw?= =?utf-8?B?T2NZemVuMHF4NWJleVZsamRqdUlRN0h4bnNjZTBGTHRmRHlCNWUwc2JMQmhS?= =?utf-8?B?cjBuS0ZGSEdTU3B1ZGFSemJlV05EOWpXWG5MWDJrNld5VWI1b2VqaFJPWUFT?= =?utf-8?B?aExJSWVlQ1pUU2xvUk03OTBJaXoxZGZWcndtdjFNYjlGZVVmWEpiUTNtMnQ3?= =?utf-8?B?SjdxeXduWDF5TUhKYm1VUlhIbHVGSkxBRytDR3Zwa21sL05PWHhKZW1NUFNw?= =?utf-8?B?TXA4L293aFNPVFZXb1ByNmNTSUFQQnlJSjBnc2dESFg3Q0xVVGlseUNEMU4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-479d-6b48-08d9f6660fcc X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3208.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Feb 2022 00:47:26.3662 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: k1/NzBG5J6enoip+ehsZ+obkLUyJ82/1qx3qXJfcVcukIkNPC12DFaNVjkJG++TrWhY0nc9jiMiakRLM7SDcYwO4PB4nwqixbzHpMtac4D4= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR1001MB2328 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10266 signatures=677939 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 adultscore=0 phishscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202230000 X-Proofpoint-GUID: Ts8b9rC175Hcf_kbOkJCK3mOiqOcJj0w X-Proofpoint-ORIG-GUID: Ts8b9rC175Hcf_kbOkJCK3mOiqOcJj0w X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Feb 2022 00:47:32 -0000 You may be thinking of string capabilities in some other language. Selected from the Linux man pages for these glibc functions: strcpy:        char *strcpy(char *dest, const char *src);        The  strcpy()  function  copies the string pointed to by src, including        the terminating null byte ('\0'), to the buffer  pointed to  by  dest.        The  strings  may  not overlap, and the destination string dest must be        large enough to receive the copy.  Beware  of  buffer overruns! strcat:        char *strcat(char *dest, const char *src);        The  strcat() function appends the src string to the dest string, over‐        writing the terminating null byte ('\0') at the end of dest,  and  then        adds  a  terminating  null  byte.  The strings may not overlap, and the        dest string must have enough space for the  result.   If dest  is  not        large  enough, program behavior is unpredictable; buffer overruns are a        favorite avenue for attacking secure programs. Neither strcpy nor strcat allocate or release buffers. The programmer is expected to have previously allocated the dest buffer of sufficient size. It seems likely from the behavior you describe, in your case, the const src string is allocated on the stack and your use of strcat with an unallocated dest is overwriting the end of the src string on each iteration. Ultimately you either run out of stack space or wipe out some other stack data which causes unpredictable behavior. Strings in C are not particularly user friendly. As to whether they are intuitive, it all depends on what language we first learn. Many languages invented in the 70s and 80s did not have strong string handling capabilities. If you learn string handling on one of those languages first, you come to not expect much and are pleasantly surprised when encountering a language that does the support work for you. Many recommend using strncpy and strncat which require explicit string lengths in order to remind the programmer to be careful about buffer sizes and to avoid the risks of unterminated strings. - patrick On 2/22/2022 3:01 PM, Emile Michel Hobo via Gcc wrote: > Dear developers: > > I find it counterintuitive that if I repeatedly reset a variable by using strcpy with an empty string "" to that variable and then us strcat to add characters to that variable that that seems to lead to a stack overflow. > > I would expect strcpy to first free the variable, then malloc, then copy the string value into the variable. I think that would be a better interpretation, because it can keep running for quite some time before it overflows and doesn’t really call it. > > Instead, I got "Illegal instruction: 4". > > I ended up reimplementing the reset function, implementing it with free and malloc myself, but the way strings have been implemented in C is highly counter-intuitive. In general pointers tend to be bug-prone, but here you would expect this not to happen. > > I hope you can fix this. Personally, I’m looking into switching to Ada. > > All the best, > > > Emile M. Hobo > > > - Au fin! Et encore en plus. - >