Link Search Menu Expand Document

Below is our example code.

strsep example

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    char *s = strdup("This is a test");
    char *deleteme = s;
    char *d = " ";
    while(s) {
        char *token = strsep(&s, d);
        puts(token);
    }
    free(deleteme);
    return 0;
}

buffer overflow

#include <string.h>
#include <stdio.h>

int main() {
    char buffer[100];
    //const char *s1 = "This is a test";
    //const char *s2 = " of string.h";
    const char *s1 = "This is a test AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    const char *s2 = " of string.h AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    buffer[0] = '\0';
    strcat(buffer, s1);
    strcat(buffer, s2);
    puts(buffer);
    return 0;
}

To include the stack canary, compile with clang -fstack-protector.


Copyright © 2022 John Hott, portions Luther Tychonievich.
Released under the CC-BY-NC-SA 4.0 license.
Creative Commons License