C chain overflow

1

Is there one?

It's a question of someone who is starting to program, but for example, if I declare this:

char cadena[11];

Why would you let me assign you a string that has 30 elements? Should not there be a memory overflow and show me weird things when pulling out the string with a printf?

To give an example:

void solicitarCadena(){
    char cadena[11];

    printf("\nIntroduce una cadena de 10 elementos como maximo:\n");

    gets(cadena);

    printf("%s",cadena);
}

In this example, even if you enter a chain of 50 elements, it shows it to me in an integrated way without giving any kind of problem.

Is the compiler I'm using (dev c / c ++) doing magic below to make it work?

    
asked by Oscar 17.02.2018 в 21:01
source

1 answer

2

Consider the following modification:

int main(void)
 {
char cadena[11];
char cadena2[11];
printf("\nIntroduce una cadena de 10 elementos como maximo:\n");
gets(cadena);
printf("%s\n",cadena);
printf("%s\n", cadena2);
return 0;
 }

You get:

  

Enter a chain of 10 elements at most:   a longer chain than it should   arga de lo due

Effectively overflowing the buffer and writing in other memory spaces.

In the following documentation url they warn about this: link

  

Notes   The gets () function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks.

    
answered by 17.02.2018 / 22:38
source