xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/memset/memset01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (c) International Business Machines  Corp., 2002
4  *
5  *   01/02/2003	Port to LTP	[email protected]
6  *   06/30/2001	Port to Linux	[email protected]
7  */
8 
9 /*\
10  * [Description]
11  *
12  * The testcase for test setting of buffer by check boundary conditions.
13  */
14 
15 #include <stdio.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 
21 #include "tst_test.h"
22 
23 #define BSIZE 4096
24 
25 char buf[BSIZE];
26 
fill(void)27 static void fill(void)
28 {
29 	register int i;
30 
31 	for (i = 0; i < BSIZE; i++)
32 		buf[i] = 'a';
33 }
34 
checkit(char * str)35 static int checkit(char *str)
36 {
37 	register int i = 0;
38 
39 	while (!*str++)
40 		i++;
41 
42 	return i;
43 }
44 
setup(void)45 static void setup(void)
46 {
47 	fill();
48 }
49 
verify_memset(void)50 static void verify_memset(void)
51 {
52 	register int i, j;
53 	char *p = &buf[400];
54 
55 	for (i = 0; i < 200; i++) {
56 		fill();
57 		memset(p, 0, i);
58 		if ((j = checkit(p)) != i) {
59 			tst_res(TINFO, "Not enough zero bytes, wanted %d, got %d", i, j);
60 			break;
61 		}
62 		if (!p[-1] || !p[i]) {
63 			tst_res(TINFO, "Boundary error, clear of %d", i);
64 			break;
65 		}
66 	}
67 
68 	if (i == 200)
69 		tst_res(TPASS, "Test passed");
70 	else
71 		tst_res(TFAIL, "Test fails");
72 }
73 
74 static struct tst_test test = {
75 	.setup = setup,
76 	.test_all = verify_memset,
77 };
78