File: recursion_guard.c.in

package info (click to toggle)
drgn 0.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,852 kB
  • sloc: python: 74,992; ansic: 54,589; awk: 423; makefile: 351; sh: 99
file content (43 lines) | stat: -rw-r--r-- 1,152 bytes parent folder | download | duplicates (2)
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
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "test_util.h"
#include "../error.h"

static struct drgn_error *no_recursion_allowed(int *counter)
{
	drgn_recursion_guard(0, "recursive call detected");
	(*counter)++;
	return no_recursion_allowed(counter);
}

static struct drgn_error *limited_recursion_allowed(int *counter)
{
	drgn_recursion_guard(10, "maximum recursion depth exceeded");
	(*counter)++;
	return limited_recursion_allowed(counter);
}

#suite recursion_guard

#test no_recursion
{
	int counter = 0;
	struct drgn_error *err = no_recursion_allowed(&counter);
	ck_assert_ptr_nonnull(err);
	ck_assert_int_eq(err->code, DRGN_ERROR_RECURSION);
	ck_assert_str_eq(err->message, "recursive call detected");
	drgn_error_destroy(err);
	ck_assert_int_eq(counter, 1);
}

#test limited_recursion
{
	int counter = 0;
	struct drgn_error *err = limited_recursion_allowed(&counter);
	ck_assert_ptr_nonnull(err);
	ck_assert_int_eq(err->code, DRGN_ERROR_RECURSION);
	ck_assert_str_eq(err->message, "maximum recursion depth exceeded");
	drgn_error_destroy(err);
	ck_assert_int_eq(counter, 11);
}