Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 1, 2019 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/61ea7250149c3c7564ef0f3e6f3db299 to your computer and use it in GitHub Desktop.
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/
/*
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