aboutsummaryrefslogtreecommitdiffhomepage
path: root/sanity.cpp
blob: f897694149b6a67d7988e363edfc882bafaa9af7 (plain)
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
/** \file sanity.c
  Functions for performing sanity checks on the program state
*/
#include "config.h"

#include <stdlib.h>
#include <wchar.h>
#include <stdio.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>


#include "fallback.h"
#include "util.h"

#include "common.h"
#include "sanity.h"
#include "proc.h"
#include "history.h"
#include "reader.h"
#include "kill.h"
#include "wutil.h"


/**
   Status from earlier sanity checks
*/
static int insane;

void sanity_lose()
{
    debug(0, _(L"Errors detected, shutting down. Break on sanity_lose() to debug."));
    insane = 1;
}

int sanity_check()
{
    if (!insane)
        if (get_is_interactive())
            history_sanity_check();
    if (!insane)
        reader_sanity_check();
    if (!insane)
        kill_sanity_check();
    if (!insane)
        proc_sanity_check();

    return insane;
}

void validate_pointer(const void *ptr, const wchar_t *err, int null_ok)
{

    /*
       Test if the pointer data crosses a segment boundary.
    */

    if ((0x00000003l & (intptr_t)ptr) != 0)
    {
        debug(0, _(L"The pointer '%ls' is invalid"), err);
        sanity_lose();
    }

    if ((!null_ok) && (ptr==0))
    {
        debug(0, _(L"The pointer '%ls' is null"), err);
        sanity_lose();
    }
}