public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick McGehearty <patrick.mcgehearty@oracle.com>
To: gcc@gcc.gnu.org
Subject: Re: strcpy and strcat seem to lead to a stack overflow
Date: Tue, 22 Feb 2022 18:47:21 -0600	[thread overview]
Message-ID: <a6e89185-6d93-bb81-416b-07f19d82ef81@oracle.com> (raw)
In-Reply-To: <AM6P194MB0263B192623F4BBFD53A4C10AE3B9@AM6P194MB0263.EURP194.PROD.OUTLOOK.COM>

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. -
>


  reply	other threads:[~2022-02-23  0:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-22 21:01 Emile Michel Hobo
2022-02-23  0:47 ` Patrick McGehearty [this message]
2022-02-23  8:58 ` lego12239
2022-02-23  9:47 ` Jonathan Wakely

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=a6e89185-6d93-bb81-416b-07f19d82ef81@oracle.com \
    --to=patrick.mcgehearty@oracle.com \
    --cc=gcc@gcc.gnu.org \
    /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).