• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SRI-CSL / yices2 / 16032530443

02 Jul 2025 06:08PM UTC coverage: 60.349% (-5.0%) from 65.357%
16032530443

Pull #582

github

web-flow
Merge b7e09d316 into b3af64ab1
Pull Request #582: Update ci

63716 of 105580 relevant lines covered (60.35%)

1127640.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.62
/src/utils/memalloc.c
1
/*
2
 * This file is part of the Yices SMT Solver.
3
 * Copyright (C) 2017 SRI International.
4
 *
5
 * Yices is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * Yices is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with Yices.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
/*
20
 * WRAPPERS FOR MALLOC/REALLOC/FREE
21
 */
22

23
#include <stdio.h>
24
#include <string.h>
25
#include <assert.h>
26

27
#include "utils/memalloc.h"
28

29
#include "yices_exit_codes.h"
30

31

32
/*
33
 * Callback function: give a chance to do something when
34
 * we run out of memory.
35
 */
36
out_of_mem_callback_t __out_of_mem_callback = NULL;
37

38

39
/*
40
 * Fatal error: out of memory
41
 */
42
void out_of_memory(void) {
×
43
  if (__out_of_mem_callback != NULL) {
×
44
    __out_of_mem_callback();
×
45
  } else {
46
    fprintf(stderr, "Out of memory\n");
×
47
  }
48
  exit(YICES_EXIT_OUT_OF_MEMORY);
×
49
}
50

51

52
/*
53
 * Local malloc: abort if out of memory.
54
 *
55
 * Special case: is size = 0, malloc(size) may
56
 * return NULL on some systems, but that does not
57
 * mean we're out of memory.
58
 */
59
void *safe_malloc(size_t size) {
23,857,221✔
60
  void *tmp;
61

62
  tmp = malloc(size);
23,857,221✔
63
  if (tmp == NULL && size > 0) {
23,857,221✔
64
    out_of_memory();
×
65
  }
66

67
  return tmp;
23,857,221✔
68
}
69

70
/*
71
 * Safer realloc to support lazy allocation.
72
 * If ptr == NULL, call malloc otherwise call realloc.
73
 * Abort if out of memory.
74
 *
75
 * NOTE: C99 specifies that realloc should behave like
76
 * malloc if ptr is NULL. This is what the Linux default
77
 * malloc does, but it's not clear whether other malloc
78
 * implementations (e.g., on MacOSX) follow the standard.
79
 * It's safer to check whether ptr is NULL and
80
 * call malloc or realloc accordingly.
81
 *
82
 * size must be positive: realloc(p, 0) is the same as free(ptr).
83
 */
84
void *safe_realloc(void *ptr, size_t size) {
1,655,173✔
85
  void *tmp;
86

87
  assert(size > 0);
88

89
  if (ptr == NULL) {
1,655,173✔
90
    tmp = malloc(size);
196,401✔
91
  } else {
92
    tmp = realloc(ptr, size);
1,458,772✔
93
  }
94
  if (tmp == NULL) out_of_memory();
1,655,173✔
95

96
  return tmp;
1,655,173✔
97
}
98

99

100
/*
101
 * Wrapper for strdup
102
 */
103
char *safe_strdup(const char *s) {
×
104
  char *tmp;
105

106
  tmp = strdup(s);
×
107
  if (tmp == NULL) {
×
108
    out_of_memory();
×
109
  }
110
  return tmp;
×
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc