Skip to content

Commit 0761394

Browse files
Add parameter validation for selected API functions
1 parent e3139d4 commit 0761394

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/api/api_ast.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,15 @@ extern "C" {
225225
Z3_TRY;
226226
LOG_Z3_mk_fresh_func_decl(c, prefix, domain_size, domain, range);
227227
RESET_ERROR_CODE();
228+
CHECK_IS_SORT(range, nullptr);
229+
CHECK_SORTS(domain_size, domain, nullptr);
228230
if (prefix == nullptr) {
229231
prefix = "";
230232
}
231233

232234
func_decl* d = mk_c(c)->m().mk_fresh_func_decl(prefix,
233235
domain_size,
234-
reinterpret_cast<sort*const*>(domain),
236+
to_sorts(domain),
235237
to_sort(range), false);
236238

237239
mk_c(c)->save_ast_trail(d);
@@ -243,9 +245,11 @@ extern "C" {
243245
Z3_TRY;
244246
LOG_Z3_mk_fresh_const(c, prefix, ty);
245247
RESET_ERROR_CODE();
248+
CHECK_IS_SORT(ty, nullptr);
246249
if (prefix == nullptr) {
247250
prefix = "";
248251
}
252+
249253
app* a = mk_c(c)->m().mk_fresh_const(prefix, to_sort(ty), false);
250254
mk_c(c)->save_ast_trail(a);
251255
RETURN_Z3(of_ast(a));
@@ -654,6 +658,7 @@ extern "C" {
654658
Z3_TRY;
655659
LOG_Z3_get_sort_name(c, t);
656660
RESET_ERROR_CODE();
661+
CHECK_IS_SORT(t, of_symbol(symbol::null));
657662
CHECK_VALID_AST(t, of_symbol(symbol::null));
658663
return of_symbol(to_sort(t)->get_name());
659664
Z3_CATCH_RETURN(of_symbol(symbol::null));

src/api/api_context.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,13 @@ namespace api {
286286
inline api::context * mk_c(Z3_context c) { return reinterpret_cast<api::context*>(c); }
287287
#define RESET_ERROR_CODE() { mk_c(c)->reset_error_code(); }
288288
#define SET_ERROR_CODE(ERR, MSG) { mk_c(c)->set_error_code(ERR, MSG); }
289-
#define CHECK_NON_NULL(_p_,_ret_) { if (_p_ == 0) { SET_ERROR_CODE(Z3_INVALID_ARG, "ast is null"); return _ret_; } }
290-
#define CHECK_VALID_AST(_a_, _ret_) { if (_a_ == 0 || !CHECK_REF_COUNT(_a_)) { SET_ERROR_CODE(Z3_INVALID_ARG, "not a valid ast"); return _ret_; } }
289+
#define CHECK_NON_NULL(_p_,_ret_) { if (_p_ == nullptr) { SET_ERROR_CODE(Z3_INVALID_ARG, "ast is null"); return _ret_; } }
290+
#define CHECK_VALID_AST(_a_, _ret_) { if (_a_ == nullptr || !CHECK_REF_COUNT(_a_)) { SET_ERROR_CODE(Z3_INVALID_ARG, "not a valid ast"); return _ret_; } }
291291
inline bool is_expr(Z3_ast a) { return is_expr(to_ast(a)); }
292-
#define CHECK_IS_EXPR(_p_, _ret_) { if (_p_ == 0 || !is_expr(_p_)) { SET_ERROR_CODE(Z3_INVALID_ARG, "ast is not an expression"); return _ret_; } }
292+
#define CHECK_IS_EXPR(_p_, _ret_) { if (_p_ == nullptr || !is_expr(_p_)) { SET_ERROR_CODE(Z3_INVALID_ARG, "ast is not an expression"); return _ret_; } }
293+
#define CHECK_IS_SORT(_p_, _ret_) { if (_p_ == nullptr || !is_sort(_p_)) { SET_ERROR_CODE(Z3_INVALID_ARG, "ast is not a sort"); return _ret_; } }
294+
#define CHECK_SORTS(_n_, _ps_, _ret_) { for (unsigned i = 0; i < _n_; ++i) if (!is_sort(_ps_[i])) { SET_ERROR_CODE(Z3_INVALID_ARG, "ast is not a sort"); return _ret_; } }
295+
293296
inline bool is_bool_expr(Z3_context c, Z3_ast a) { return is_expr(a) && mk_c(c)->m().is_bool(to_expr(a)); }
294-
#define CHECK_FORMULA(_a_, _ret_) { if (_a_ == 0 || !CHECK_REF_COUNT(_a_) || !is_bool_expr(c, _a_)) { SET_ERROR_CODE(Z3_INVALID_ARG, nullptr); return _ret_; } }
297+
#define CHECK_FORMULA(_a_, _ret_) { if (_a_ == nullptr || !CHECK_REF_COUNT(_a_) || !is_bool_expr(c, _a_)) { SET_ERROR_CODE(Z3_INVALID_ARG, nullptr); return _ret_; } }
295298
inline void check_sorts(Z3_context c, ast * n) { mk_c(c)->check_sorts(n); }

src/api/api_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ inline ast * const * to_asts(Z3_ast const* a) { return reinterpret_cast<ast* con
6767

6868
inline sort * to_sort(Z3_sort a) { return reinterpret_cast<sort*>(a); }
6969
inline Z3_sort of_sort(sort* s) { return reinterpret_cast<Z3_sort>(s); }
70+
inline bool is_sort(Z3_sort a) { return is_sort(to_sort(a)); }
7071

7172
inline sort * const * to_sorts(Z3_sort const* a) { return reinterpret_cast<sort* const*>(a); }
7273
inline Z3_sort const * of_sorts(sort* const* s) { return reinterpret_cast<Z3_sort const*>(s); }

0 commit comments

Comments
 (0)