1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Strn.h>

int main(int argc, char **argv)
{
	char a[8];
	char pad1[32];<--- Unused variable: pad1
	char *b;
	char c[37];
	char pad2[23];<--- Unused variable: pad2
	int i;
	int len1, len2;

	b = Strncpy(a, "hello", sizeof(a));
	b = Strncat(b, "world", sizeof(a));
	printf("1: result=[%s] should be=[%s]\n",
		b,
		"hellowo"
	);

	for (i=0; i<sizeof(c); i++)
		c[i] = 'X';
	b = Strncpy(c, "testing", sizeof(c) - 2);<--- b is assigned
#if (STRN_ZERO_PAD == 1)
	for (i=7; i<sizeof(c) - 2; i++) {
		if (c[i] != '\0') {
			printf("2: did not clear to end of buffer\n");
			break;
		}
	}
#endif
	for (i=sizeof(c) - 2; i<sizeof(c); i++) {
		if (c[i] != 'X') {
			printf("2: overwrote buffer\n");
			break;
		}
	}

	for (i=0; i<sizeof(c); i++)
		c[i] = 'X';
	b = Strncpy(c, "testing", sizeof(c) - 2);<--- b is overwritten
	b = Strncat(b, " still", sizeof(c) - 2);
#if (STRN_ZERO_PAD == 1)
	for (i=13; i<sizeof(c) - 2; i++) {
		if (c[i] != '\0') {
			printf("3: did not clear to end of buffer\n");
			break;
		}
	}
#endif
	for (i=sizeof(c) - 2; i<sizeof(c); i++) {
		if (c[i] != 'X') {
			printf("3: overwrote buffer\n");
			break;
		}
	}

/*--------------*/

	b = Strnpcpy(a, "hello", sizeof(a));
	len1 = (int) (b - a);
	b = Strnpcat(a, "world", sizeof(a));
	len2 = (int) (b - a);
	printf("4: result=[%s] should be=[%s] len1=%d len2=%d\n",
		a,
		"hellowo",
		len1,
		len2
	);

	for (i=0; i<sizeof(c); i++)
		c[i] = 'X';
	b = Strnpcpy(c, "testing", sizeof(c) - 2);<--- b is assigned
#if (STRNP_ZERO_PAD == 1)
	for (i=7; i<sizeof(c) - 2; i++) {
		if (c[i] != '\0') {
			printf("5: did not clear to end of buffer\n");
			break;
		}
	}
#endif
	for (i=sizeof(c) - 2; i<sizeof(c); i++) {
		if (c[i] != 'X') {
			printf("5: overwrote buffer\n");
			break;
		}
	}

	for (i=0; i<sizeof(c); i++)
		c[i] = 'X';
	b = Strnpcpy(c, "testing", sizeof(c) - 2);<--- b is overwritten<--- b is assigned
	b = Strnpcat(c, " still", sizeof(c) - 2);<--- b is overwritten
#if (STRNP_ZERO_PAD == 1)
	for (i=13; i<sizeof(c) - 2; i++) {
		if (c[i] != '\0') {
			printf("6: did not clear to end of buffer\n");
			break;
		}
	}
#endif
	for (i=sizeof(c) - 2; i<sizeof(c); i++) {
		if (c[i] != 'X') {
			printf("6: overwrote buffer\n");
			break;
		}
	}

/*--------------*/
	{
		char *str;

		str = NULL;
		if (Dynscat(&str, "this is a test", 0) == NULL) {
			printf("7a: fail\n");
		} else if (strcmp(str, "this is a test") != 0) {
			printf("7b: fail\n");
		}
		free(str);

		str = NULL;
		if (Dynscat(&str, "this is a test", 0) == NULL) {
			printf("7c: fail\n");
		} else if (strcmp(str, "this is a test") != 0) {
			printf("7d: fail\n");
		} else if (Dynscat(&str, " ", "", "and", " ", "so is this", 0) == NULL) {
			printf("7e: fail\n");
		} else if (strcmp(str, "this is a test and so is this") != 0) {
			printf("7f: fail\n");
		}
		free(str);
	}
	exit(0);
}