Skip to content

Instantly share code, notes, and snippets.

@pavly-gerges
Last active December 1, 2024 09:03
Show Gist options
  • Save pavly-gerges/069e14c16ab4f1c5d643f11ebe18094d to your computer and use it in GitHub Desktop.
Save pavly-gerges/069e14c16ab4f1c5d643f11ebe18094d to your computer and use it in GitHub Desktop.
// The following code examines multiple routines of definining dynamic buffers in C.
// This file fires a runtime malloc() error due to a HEAP corruption error; because of buffer overflow.
// Two maneuvers are introduced to fix this
#include <stdio.h>
#include <stdlib.h>
int main() {
// buffers
const char *value1 = "Hi";
const char *value2 = "Hello\n Dynamic Buffers!";
// pointer to buffers
const char **inputs = malloc(sizeof(char *));
// automatic allocation of address buffer ???
// appends new addresses to the buffer automatically!
*inputs = value1;
*(inputs + 1) = value2;
*(inputs + 2) = value1;
*(inputs + 3) = value1;
*(inputs + 4) = value1;
// printf("%s\n", ((const char*) inputs[2]));
// printf("%s\n", ((const char*) inputs[2]));
free((void *) inputs);
return 0;
}
@pavly-gerges
Copy link
Author

The following is a board summary to the 3 solutions presented here (incl. the defective solution that introduces memory access violations):

Memory-acces-vio

For more, follow the GNU Libc documentation for heap manipulating algorithms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment