💾 Archived View for thrig.me › blog › 2023 › 05 › 05 › cpy2.c captured on 2023-06-14 at 15:09:10.

View Raw

More Information

⬅️ Previous capture (2023-05-24)

-=-=-=-=-=-=-

// cpy2 - test strlcpy allowing for not-terminated string results
#include <stdio.h>
#include <string.h>

#define MAXLEN 3
union {
	char buf[MAXLEN];
	char overlay[MAXLEN * 2];
} bar;

int
main(void)
{
	char *toolong = "verymuchtoolongofaninputstring";
	for (int i = 0; i < MAXLEN * 2; i++) {
		bar.overlay[i] = 'x';
	}
	strlcpy(bar.buf, toolong, MAXLEN + 1);
	fprintf(stderr, "%s\n%s\n", bar.buf, bar.overlay);
}