Bug Summary

File:tools/polly/lib/External/isl/isl_ctx.c
Warning:line 142, column 12
Dereference of null pointer

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name isl_ctx.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/lib/External -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/pet/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/ppcg/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/ppcg/imath -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/lib/External/ppcg -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/imath -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/include -I /usr/include/jsoncpp -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -U NDEBUG -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=gnu99 -fconst-strings -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/lib/External -fdebug-prefix-map=/build/llvm-toolchain-snapshot-7~svn329677=. -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_ctx.c
1/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
9
10#include <isl_ctx_private.h>
11#include <isl/vec.h>
12#include <isl_options_private.h>
13
14#define __isl_calloc(type,size)((type *)calloc(1, size)) ((type *)calloc(1, size))
15#define __isl_calloc_type(type)((type *)calloc(1, sizeof(type))) __isl_calloc(type,sizeof(type))((type *)calloc(1, sizeof(type)))
16
17/* Return the negation of "b", where the negation of isl_bool_error
18 * is isl_bool_error again.
19 */
20isl_bool isl_bool_not(isl_bool b)
21{
22 return b < 0 ? isl_bool_error : !b;
23}
24
25/* Check that the result of an allocation ("p") is not NULL and
26 * complain if it is.
27 * The only exception is when allocation size ("size") is equal to zero.
28 */
29static void *check_non_null(isl_ctx *ctx, void *p, size_t size)
30{
31 if (p || size == 0)
32 return p;
33 isl_die(ctx, isl_error_alloc, "allocation failure", return NULL)do { isl_handle_error(ctx, isl_error_alloc, "allocation failure"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_ctx.c"
, 33); return ((void*)0); } while (0)
;
34}
35
36/* Prepare for performing the next "operation" in the context.
37 * Return 0 if we are allowed to perform this operation and
38 * return -1 if we should abort the computation.
39 *
40 * In particular, we should stop if the user has explicitly aborted
41 * the computation or if the maximal number of operations has been exceeded.
42 */
43int isl_ctx_next_operation(isl_ctx *ctx)
44{
45 if (!ctx)
46 return -1;
47 if (ctx->abort) {
48 isl_ctx_set_error(ctx, isl_error_abort);
49 return -1;
50 }
51 if (ctx->max_operations && ctx->operations >= ctx->max_operations)
52 isl_die(ctx, isl_error_quota,do { isl_handle_error(ctx, isl_error_quota, "maximal number of operations exceeded"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_ctx.c"
, 53); return -1; } while (0)
53 "maximal number of operations exceeded", return -1)do { isl_handle_error(ctx, isl_error_quota, "maximal number of operations exceeded"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_ctx.c"
, 53); return -1; } while (0)
;
54 ctx->operations++;
55 return 0;
56}
57
58/* Call malloc and complain if it fails.
59 * If ctx is NULL, then return NULL.
60 */
61void *isl_malloc_or_die(isl_ctx *ctx, size_t size)
62{
63 if (isl_ctx_next_operation(ctx) < 0)
64 return NULL((void*)0);
65 return ctx ? check_non_null(ctx, malloc(size), size) : NULL((void*)0);
66}
67
68/* Call calloc and complain if it fails.
69 * If ctx is NULL, then return NULL.
70 */
71void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size)
72{
73 if (isl_ctx_next_operation(ctx) < 0)
74 return NULL((void*)0);
75 return ctx ? check_non_null(ctx, calloc(nmemb, size), nmemb) : NULL((void*)0);
76}
77
78/* Call realloc and complain if it fails.
79 * If ctx is NULL, then return NULL.
80 */
81void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size)
82{
83 if (isl_ctx_next_operation(ctx) < 0)
84 return NULL((void*)0);
85 return ctx ? check_non_null(ctx, realloc(ptr, size), size) : NULL((void*)0);
86}
87
88/* Keep track of all information about the current error ("error", "msg",
89 * "file", "line") in "ctx".
90 */
91void isl_ctx_set_full_error(isl_ctx *ctx, enum isl_error error, const char *msg,
92 const char *file, int line)
93{
94 if (!ctx)
95 return;
96 ctx->error = error;
97 ctx->error_msg = msg;
98 ctx->error_file = file;
99 ctx->error_line = line;
100}
101
102void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
103 const char *file, int line)
104{
105 if (!ctx)
106 return;
107
108 isl_ctx_set_full_error(ctx, error, msg, file, line);
109
110 switch (ctx->opt->on_error) {
111 case ISL_ON_ERROR_WARN0:
112 fprintf(stderrstderr, "%s:%d: %s\n", file, line, msg);
113 return;
114 case ISL_ON_ERROR_CONTINUE1:
115 return;
116 case ISL_ON_ERROR_ABORT2:
117 fprintf(stderrstderr, "%s:%d: %s\n", file, line, msg);
118 abort();
119 return;
120 }
121}
122
123static struct isl_options *find_nested_options(struct isl_args *args,
124 void *opt, struct isl_args *wanted)
125{
126 int i;
127 struct isl_options *options;
128
129 if (args == wanted)
6
Assuming 'args' is not equal to 'wanted'
7
Taking false branch
130 return opt;
131
132 for (i = 0; args->args[i].type != isl_arg_end; ++i) {
8
Assuming the condition is true
9
Loop condition is true. Entering loop body
16
Assuming the condition is true
17
Loop condition is true. Entering loop body
133 struct isl_arg *arg = &args->args[i];
134 void *child;
135
136 if (arg->type != isl_arg_child)
10
Assuming the condition is false
11
Taking false branch
18
Assuming the condition is false
19
Taking false branch
137 continue;
138
139 if (arg->offset == (size_t) -1)
12
Assuming the condition is true
13
Taking true branch
20
Assuming the condition is false
21
Taking false branch
140 child = opt;
141 else
142 child = *(void **)(((char *)opt) + arg->offset);
22
Dereference of null pointer
143
144 options = find_nested_options(arg->u.child.child,
145 child, wanted);
146 if (options)
14
Assuming 'options' is null
15
Taking false branch
147 return options;
148 }
149
150 return NULL((void*)0);
151}
152
153static struct isl_options *find_nested_isl_options(struct isl_args *args,
154 void *opt)
155{
156 return find_nested_options(args, opt, &isl_options_args);
157}
158
159void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
160{
161 if (!ctx)
1
Assuming 'ctx' is non-null
2
Taking false branch
162 return NULL((void*)0);
163 if (args == &isl_options_args)
3
Taking false branch
164 return ctx->opt;
165 return find_nested_options(ctx->user_args, ctx->user_opt, args);
4
Passing value via 2nd parameter 'opt'
5
Calling 'find_nested_options'
166}
167
168isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
169{
170 struct isl_ctx *ctx = NULL((void*)0);
171 struct isl_options *opt = NULL((void*)0);
172 int opt_allocated = 0;
173
174 if (!user_opt)
175 return NULL((void*)0);
176
177 opt = find_nested_isl_options(args, user_opt);
178 if (!opt) {
179 opt = isl_options_new_with_defaults();
180 if (!opt)
181 goto error;
182 opt_allocated = 1;
183 }
184
185 ctx = __isl_calloc_type(struct isl_ctx)((struct isl_ctx *)calloc(1, sizeof(struct isl_ctx)));
186 if (!ctx)
187 goto error;
188
189 if (isl_hash_table_init(ctx, &ctx->id_table, 0))
190 goto error;
191
192 ctx->stats = isl_calloc_type(ctx, struct isl_stats)((struct isl_stats *)isl_calloc_or_die(ctx, 1, sizeof(struct isl_stats
)))
;
193 if (!ctx->stats)
194 goto error;
195
196 ctx->user_args = args;
197 ctx->user_opt = user_opt;
198 ctx->opt_allocated = opt_allocated;
199 ctx->opt = opt;
200 ctx->ref = 0;
201
202 isl_int_init(ctx->zero)isl_sioimath_init((ctx->zero));
203 isl_int_set_si(ctx->zero, 0)isl_sioimath_set_si((ctx->zero), 0);
204
205 isl_int_init(ctx->one)isl_sioimath_init((ctx->one));
206 isl_int_set_si(ctx->one, 1)isl_sioimath_set_si((ctx->one), 1);
207
208 isl_int_init(ctx->two)isl_sioimath_init((ctx->two));
209 isl_int_set_si(ctx->two, 2)isl_sioimath_set_si((ctx->two), 2);
210
211 isl_int_init(ctx->negone)isl_sioimath_init((ctx->negone));
212 isl_int_set_si(ctx->negone, -1)isl_sioimath_set_si((ctx->negone), -1);
213
214 isl_int_init(ctx->normalize_gcd)isl_sioimath_init((ctx->normalize_gcd));
215
216 ctx->n_cached = 0;
217 ctx->n_miss = 0;
218
219 isl_ctx_reset_error(ctx);
220
221 ctx->operations = 0;
222 isl_ctx_set_max_operations(ctx, ctx->opt->max_operations);
223
224 return ctx;
225error:
226 isl_args_free(args, user_opt);
227 if (opt_allocated)
228 isl_options_free(opt);
229 free(ctx);
230 return NULL((void*)0);
231}
232
233struct isl_ctx *isl_ctx_alloc()
234{
235 struct isl_options *opt;
236
237 opt = isl_options_new_with_defaults();
238
239 return isl_ctx_alloc_with_options(&isl_options_args, opt);
240}
241
242void isl_ctx_ref(struct isl_ctx *ctx)
243{
244 ctx->ref++;
245}
246
247void isl_ctx_deref(struct isl_ctx *ctx)
248{
249 isl_assert(ctx, ctx->ref > 0, return)do { if (ctx->ref > 0) break; do { isl_handle_error(ctx
, isl_error_unknown, "Assertion \"" "ctx->ref > 0" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_ctx.c"
, 249); return; } while (0); } while (0)
;
250 ctx->ref--;
251}
252
253/* Print statistics on usage.
254 */
255static void print_stats(isl_ctx *ctx)
256{
257 fprintf(stderrstderr, "operations: %lu\n", ctx->operations);
258}
259
260void isl_ctx_free(struct isl_ctx *ctx)
261{
262 if (!ctx)
263 return;
264 if (ctx->ref != 0)
265 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "isl_ctx freed, but some objects still reference it"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_ctx.c"
, 267); return; } while (0)
266 "isl_ctx freed, but some objects still reference it",do { isl_handle_error(ctx, isl_error_invalid, "isl_ctx freed, but some objects still reference it"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_ctx.c"
, 267); return; } while (0)
267 return)do { isl_handle_error(ctx, isl_error_invalid, "isl_ctx freed, but some objects still reference it"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_ctx.c"
, 267); return; } while (0)
;
268
269 if (ctx->opt->print_stats)
270 print_stats(ctx);
271
272 isl_hash_table_clear(&ctx->id_table);
273 isl_blk_clear_cache(ctx);
274 isl_int_clear(ctx->zero)isl_sioimath_clear((ctx->zero));
275 isl_int_clear(ctx->one)isl_sioimath_clear((ctx->one));
276 isl_int_clear(ctx->two)isl_sioimath_clear((ctx->two));
277 isl_int_clear(ctx->negone)isl_sioimath_clear((ctx->negone));
278 isl_int_clear(ctx->normalize_gcd)isl_sioimath_clear((ctx->normalize_gcd));
279 isl_args_free(ctx->user_args, ctx->user_opt);
280 if (ctx->opt_allocated)
281 isl_options_free(ctx->opt);
282 free(ctx->stats);
283 free(ctx);
284}
285
286struct isl_options *isl_ctx_options(isl_ctx *ctx)
287{
288 if (!ctx)
289 return NULL((void*)0);
290 return ctx->opt;
291}
292
293enum isl_error isl_ctx_last_error(isl_ctx *ctx)
294{
295 return ctx ? ctx->error : isl_error_invalid;
296}
297
298/* Return the error message of the last error in "ctx".
299 */
300const char *isl_ctx_last_error_msg(isl_ctx *ctx)
301{
302 return ctx ? ctx->error_msg : NULL((void*)0);
303}
304
305/* Return the file name where the last error in "ctx" occurred.
306 */
307const char *isl_ctx_last_error_file(isl_ctx *ctx)
308{
309 return ctx ? ctx->error_file : NULL((void*)0);
310}
311
312/* Return the line number where the last error in "ctx" occurred.
313 */
314int isl_ctx_last_error_line(isl_ctx *ctx)
315{
316 return ctx ? ctx->error_line : -1;
317}
318
319void isl_ctx_reset_error(isl_ctx *ctx)
320{
321 if (!ctx)
322 return;
323 ctx->error = isl_error_none;
324 ctx->error_msg = NULL((void*)0);
325 ctx->error_file = NULL((void*)0);
326 ctx->error_line = -1;
327}
328
329void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
330{
331 isl_ctx_set_full_error(ctx, error, NULL((void*)0), NULL((void*)0), -1);
332}
333
334void isl_ctx_abort(isl_ctx *ctx)
335{
336 if (ctx)
337 ctx->abort = 1;
338}
339
340void isl_ctx_resume(isl_ctx *ctx)
341{
342 if (ctx)
343 ctx->abort = 0;
344}
345
346int isl_ctx_aborted(isl_ctx *ctx)
347{
348 return ctx ? ctx->abort : -1;
349}
350
351int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
352{
353 if (!ctx)
354 return -1;
355 return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
356}
357
358/* Set the maximal number of iterations of "ctx" to "max_operations".
359 */
360void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations)
361{
362 if (!ctx)
363 return;
364 ctx->max_operations = max_operations;
365}
366
367/* Return the maximal number of iterations of "ctx".
368 */
369unsigned long isl_ctx_get_max_operations(isl_ctx *ctx)
370{
371 return ctx ? ctx->max_operations : 0;
372}
373
374/* Reset the number of operations performed by "ctx".
375 */
376void isl_ctx_reset_operations(isl_ctx *ctx)
377{
378 if (!ctx)
379 return;
380 ctx->operations = 0;
381}