Created
August 1, 2019 15:30
-
-
Save parzibyte/61ea7250149c3c7564ef0f3e6f3db299 to your computer and use it in GitHub Desktop.
Prevenir desbordamiento de búfer en C (buffer overflow) - https://parzibyte.me/blog/2018/11/18/fgets-vs-scanf-en-c/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Ejemplo simple de prevención | |
de desbordamiento de búfer en C | |
al escanear variables | |
@author parzibyte | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
// Por si decidimos cambiar la longitud después | |
#define LONGITUD 4 | |
int main() { | |
char cadena[LONGITUD]; | |
int autenticado = 0; // 1 es que sí, 0 que no | |
printf("Ingresa la contraseña:\n"); | |
fgets(cadena, LONGITUD, | |
stdin); // ----- Aquí está la prevención del desbordamiento | |
cadena[strcspn(cadena, "\r\n")] = 0; | |
if (strcmp(cadena, "123") == 0) { | |
autenticado = 1; | |
} | |
if (autenticado) | |
printf("Bienvenido al programa"); | |
else | |
printf("Acceso denegado"); | |
printf("\nAl final de todo, el valor que tiene 'autenticado' es: %d", | |
autenticado); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment