xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sbrk/sbrk02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 Fujitsu Ltd.
4  * Author: Zeng Linggang <[email protected]>
5  * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that sbrk() on failure sets errno to ENOMEM.
12  */
13 
14 
15 #include "tst_test.h"
16 
17 #define INC (16*1024*1024)
18 static long increment = INC;
19 
run(void)20 static void run(void)
21 {
22 	TESTPTR(sbrk(increment));
23 
24 	if (TST_RET_PTR != (void *)-1) {
25 		tst_res(TFAIL, "sbrk(%ld) unexpectedly passed and returned %p, "
26 						"expected (void *)-1 with errno=%d",
27 						increment, TST_RET_PTR, ENOMEM);
28 		return;
29 	}
30 
31 	if (TST_ERR == ENOMEM)
32 		tst_res(TPASS | TTERRNO, "sbrk(%ld) failed as expected", increment);
33 	else
34 		tst_res(TFAIL | TTERRNO, "sbrk(%ld) failed but unexpected errno, "
35 								"expected errno=%d - %s",
36 								increment, ENOMEM, strerror(ENOMEM));
37 }
38 
setup(void)39 static void setup(void)
40 {
41 	void *ret = NULL;
42 
43 	while (ret != (void *)-1 && increment > 0) {
44 		ret = sbrk(increment);
45 		increment += INC;
46 	}
47 }
48 
49 static struct tst_test test = {
50 	.test_all = run,
51 	.setup = setup
52 };
53