Skip to content

Commit e7e11d9

Browse files
authored
Merge pull request #3202 from marcstern/v2/pr/assert
Fixed assert() usage
2 parents 277e7e2 + 60d07a5 commit e7e11d9

13 files changed

+925
-156
lines changed

apache2/apache2_config.c

Lines changed: 594 additions & 122 deletions
Large diffs are not rendered by default.

apache2/modsecurity.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ static apr_status_t modsecurity_process_phase_response_headers(modsec_rec *msr)
670670
*
671671
*/
672672
static apr_status_t modsecurity_process_phase_response_body(modsec_rec *msr) {
673+
assert(msr != NULL);
673674
apr_time_t time_before;
674675
apr_status_t rc = 0;
675676

@@ -701,6 +702,7 @@ static apr_status_t modsecurity_process_phase_response_body(modsec_rec *msr) {
701702
*
702703
*/
703704
static apr_status_t modsecurity_process_phase_logging(modsec_rec *msr) {
705+
assert(msr != NULL);
704706
apr_time_t time_before, time_after;
705707

706708
if (msr->txcfg->debuglog_level >= 4) {

apache2/msc_geo.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* directly using the email address security@modsecurity.org.
1313
*/
1414

15+
#include <assert.h>
1516
#include "msc_geo.h"
1617

1718

@@ -244,6 +245,7 @@ static int field_length(const char *field, int maxlen)
244245
*/
245246
int geo_init(directory_config *dcfg, const char *dbfn, char **error_msg)
246247
{
248+
assert(dcfg != NULL);
247249
*error_msg = NULL;
248250

249251
if ((dcfg->geo == NULL) || (dcfg->geo == NOT_SET_P)) {

apache2/msc_json.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const char *base_offset=NULL;
2121
int json_add_argument(modsec_rec *msr, const char *value, unsigned length)
2222
{
2323
assert(msr != NULL);
24+
assert(msr->json != NULL);
2425
msc_arg *arg = (msc_arg *) NULL;
2526

2627
/**
@@ -89,6 +90,7 @@ static int yajl_map_key(void *ctx, const unsigned char *key, size_t length)
8990
{
9091
modsec_rec *msr = (modsec_rec *) ctx;
9192
assert(msr != NULL);
93+
assert(msr->json != NULL);
9294
unsigned char *safe_key = (unsigned char *) NULL;
9395

9496
/**
@@ -168,6 +170,7 @@ static int yajl_number(void *ctx, const char *value, size_t length)
168170
static int yajl_start_array(void *ctx) {
169171
modsec_rec *msr = (modsec_rec *) ctx;
170172
assert(msr != NULL);
173+
assert(msr->json != NULL);
171174

172175
if (!msr->json->current_key && !msr->json->prefix) {
173176
msr->json->prefix = apr_pstrdup(msr->mp, "array");
@@ -198,6 +201,7 @@ static int yajl_start_array(void *ctx) {
198201
static int yajl_end_array(void *ctx) {
199202
modsec_rec *msr = (modsec_rec *) ctx;
200203
assert(msr != NULL);
204+
assert(msr->json != NULL);
201205
unsigned char *separator = (unsigned char *) NULL;
202206

203207
/**
@@ -235,6 +239,7 @@ static int yajl_start_map(void *ctx)
235239
{
236240
modsec_rec *msr = (modsec_rec *) ctx;
237241
assert(msr != NULL);
242+
assert(msr->json != NULL);
238243

239244
/**
240245
* If we do not have a current_key, this is a top-level hash, so we do not
@@ -274,6 +279,7 @@ static int yajl_end_map(void *ctx)
274279
{
275280
modsec_rec *msr = (modsec_rec *) ctx;
276281
assert(msr != NULL);
282+
assert(msr->json != NULL);
277283
unsigned char *separator = (unsigned char *) NULL;
278284

279285
/**
@@ -365,6 +371,7 @@ int json_init(modsec_rec *msr, char **error_msg) {
365371
*/
366372
int json_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char **error_msg) {
367373
assert(msr != NULL);
374+
assert(msr->json != NULL);
368375
assert(error_msg != NULL);
369376
*error_msg = NULL;
370377
base_offset=buf;
@@ -393,6 +400,7 @@ int json_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char
393400
*/
394401
int json_complete(modsec_rec *msr, char **error_msg) {
395402
assert(msr != NULL);
403+
assert(msr->json != NULL);
396404
assert(error_msg != NULL);
397405
char *json_data = (char *) NULL;
398406

@@ -419,6 +427,8 @@ int json_complete(modsec_rec *msr, char **error_msg) {
419427
* Frees the resources used for JSON parsing.
420428
*/
421429
apr_status_t json_cleanup(modsec_rec *msr) {
430+
assert(msr != NULL);
431+
assert(msr->json != NULL);
422432
msr_log(msr, 4, "JSON: Cleaning up JSON results");
423433
if (msr->json->handle != NULL) {
424434
yajl_free(msr->json->handle);

apache2/msc_logging.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ static void write_rule_json(modsec_rec *msr, const msre_rule *rule, yajl_gen g)
654654
* Produce an audit log entry in JSON format.
655655
*/
656656
void sec_audit_logger_json(modsec_rec *msr) {
657+
assert(msr != NULL);
657658
const apr_array_header_t *arr = NULL;
658659
apr_table_entry_t *te = NULL;
659660
const apr_array_header_t *tarr_pattern = NULL;
@@ -1547,6 +1548,7 @@ void sec_audit_logger_json(modsec_rec *msr) {
15471548
* Produce an audit log entry in native format.
15481549
*/
15491550
void sec_audit_logger_native(modsec_rec *msr) {
1551+
assert(msr != NULL);
15501552
const apr_array_header_t *arr = NULL;
15511553
apr_table_entry_t *te = NULL;
15521554
const apr_array_header_t *tarr_pattern = NULL;
@@ -2235,7 +2237,7 @@ void sec_audit_logger_native(modsec_rec *msr) {
22352237
sec_auditlog_write(msr, text, strlen(text));
22362238
} else {
22372239
if ((rule != NULL) && (rule->actionset != NULL) && !rule->actionset->is_chained && (rule->chain_starter == NULL)) {
2238-
text = apr_psprintf(msr->mp, "%s\n\n", rule->unparsed);
2240+
text = apr_psprintf(msr->mp, "%s\n", rule->unparsed);
22392241
sec_auditlog_write(msr, text, strlen(text));
22402242
}
22412243
}
@@ -2327,6 +2329,7 @@ void sec_audit_logger_native(modsec_rec *msr) {
23272329
*/
23282330
void sec_audit_logger(modsec_rec *msr) {
23292331
#ifdef WITH_YAJL
2332+
assert(msr != NULL);
23302333
if (msr->txcfg->auditlog_format == AUDITLOGFORMAT_JSON) {
23312334
sec_audit_logger_json(msr);
23322335
} else {

apache2/msc_multipart.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,7 @@ int multipart_process_chunk(modsec_rec *msr, const char *buf,
13271327
*
13281328
*/
13291329
apr_status_t multipart_cleanup(modsec_rec *msr) {
1330+
assert(msr != NULL);
13301331
int keep_files = 0;
13311332

13321333
if (msr->mpd == NULL) return -1;

apache2/msc_parsers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ int parse_arguments(modsec_rec *msr, const char *s, apr_size_t inputlength,
245245
apr_table_t *arguments, int *invalid_count)
246246
{
247247
assert(msr != NULL);
248+
assert(invalid_count != NULL);
248249
msc_arg *arg;
249250
apr_size_t i, j;
250251
char *value = NULL;

apache2/msc_reqbody.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
void msre_engine_reqbody_processor_register(msre_engine *engine,
2626
const char *name, void *fn_init, void *fn_process, void *fn_complete)
2727
{
28+
assert(engine != NULL);
2829
msre_reqbody_processor_metadata *metadata =
2930
(msre_reqbody_processor_metadata *)apr_pcalloc(engine->mp,
3031
sizeof(msre_reqbody_processor_metadata));
@@ -440,6 +441,7 @@ apr_status_t modsecurity_request_body_store(modsec_rec *msr,
440441
apr_status_t modsecurity_request_body_to_stream(modsec_rec *msr, const char *buffer, int buflen, char **error_msg) {
441442
assert(msr != NULL);
442443
assert(error_msg != NULL);
444+
assert(buffer != NULL || buflen == 0);
443445
#ifndef MSC_LARGE_STREAM_INPUT
444446
char *stream_input_body = NULL;
445447
char *data = NULL;
@@ -812,6 +814,7 @@ apr_status_t modsecurity_request_body_retrieve_start(modsec_rec *msr, char **err
812814
*
813815
*/
814816
apr_status_t modsecurity_request_body_retrieve_end(modsec_rec *msr) {
817+
assert(msr != NULL);
815818
if (msr->msc_reqbody_storage == MSC_REQBODY_DISK) {
816819
if (msr->msc_reqbody_fd > 0) {
817820
close(msr->msc_reqbody_fd);

apache2/msc_xml.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ int xml_complete(modsec_rec *msr, char **error_msg) {
140140
* Frees the resources used for XML parsing.
141141
*/
142142
apr_status_t xml_cleanup(modsec_rec *msr) {
143+
assert(msr != NULL);
144+
assert(msr->xml != NULL);
143145
if (msr->xml->parsing_ctx != NULL) {
144146
if (msr->xml->parsing_ctx->myDoc) {
145147
xmlFreeDoc(msr->xml->parsing_ctx->myDoc);

apache2/re.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ char *msre_ruleset_phase_rule_update_target_matching_exception(modsec_rec *msr,
203203
const char *p3)
204204
{
205205
assert(ruleset != NULL);
206+
assert(phase_arr != NULL);
206207
msre_rule **rules;
207208
int i, j, mode;
208209
char *err;
@@ -212,7 +213,10 @@ char *msre_ruleset_phase_rule_update_target_matching_exception(modsec_rec *msr,
212213
rules = (msre_rule **)phase_arr->elts;
213214
for (i = 0; i < phase_arr->nelts; i++) {
214215
msre_rule *rule = (msre_rule *)rules[i];
216+
assert(rule != NULL);
217+
215218
if (mode == 0) { /* Looking for next rule. */
219+
assert(rule->actionset != NULL);
216220
if (msre_ruleset_rule_matches_exception(rule, re)) {
217221
err = update_rule_target_ex(msr, ruleset, rule, p2, p3);
218222
if (err) return err;
@@ -527,10 +531,12 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
527531
}
528532

529533
int msre_ruleset_rule_matches_exception(msre_rule *rule, rule_exception *re) {
534+
assert(rule != NULL);
530535
int match = 0;
531536

532537
/* Only remove non-placeholder rules */
533538
if (rule->placeholder == RULE_PH_NONE) {
539+
assert(re != NULL);
534540
switch(re->type) {
535541
case RULE_EXCEPTION_REMOVE_ID :
536542
if ((rule->actionset != NULL)&&(rule->actionset->id != NULL)) {
@@ -1468,6 +1474,7 @@ apr_status_t msre_ruleset_process_phase(msre_ruleset *ruleset, modsec_rec *msr)
14681474
rules = (msre_rule **)arr->elts;
14691475
for (i = 0; i < arr->nelts; i++) {
14701476
msre_rule *rule = rules[i];
1477+
assert(rule != NULL);
14711478
rule->execution_time = 0;
14721479
}
14731480

@@ -1480,6 +1487,7 @@ apr_status_t msre_ruleset_process_phase(msre_ruleset *ruleset, modsec_rec *msr)
14801487
rules = (msre_rule **)arr->elts;
14811488
for (i = 0; i < arr->nelts; i++) {
14821489
msre_rule *rule = rules[i];
1490+
assert(rule != NULL);
14831491

14841492
/* Ignore markers, which are never processed. */
14851493
if (rule->placeholder == RULE_PH_MARKER) continue;
@@ -1498,6 +1506,8 @@ static apr_status_t msre_ruleset_process_phase_(msre_ruleset *ruleset, modsec_re
14981506
#else
14991507
apr_status_t msre_ruleset_process_phase(msre_ruleset *ruleset, modsec_rec *msr) {
15001508
#endif
1509+
assert(ruleset != NULL);
1510+
assert(msr != NULL);
15011511
apr_array_header_t *arr = NULL;
15021512
msre_rule **rules;
15031513
apr_status_t rc;
@@ -1542,10 +1552,11 @@ static apr_status_t msre_ruleset_process_phase_(msre_ruleset *ruleset, modsec_re
15421552
rules = (msre_rule **)arr->elts;
15431553
for (i = 0; i < arr->nelts; i++) {
15441554
msre_rule *rule = rules[i];
1555+
assert(rule != NULL);
1556+
assert(rule->actionset != NULL);
15451557
#if defined(PERFORMANCE_MEASUREMENT)
15461558
apr_time_t time1 = 0;
15471559
#endif
1548-
assert(rule->actionset != NULL);
15491560

15501561
/* Reset the rule interception flag */
15511562
msr->rule_was_intercepted = 0;
@@ -1974,6 +1985,9 @@ msre_ruleset *msre_ruleset_create(msre_engine *engine, apr_pool_t *mp) {
19741985
* Adds one rule to the given phase of the ruleset.
19751986
*/
19761987
int msre_ruleset_rule_add(msre_ruleset *ruleset, msre_rule *rule, int phase) {
1988+
assert(ruleset != NULL);
1989+
assert(rule != NULL);
1990+
assert(rule->actionset != NULL);
19771991
apr_array_header_t *arr = NULL;
19781992

19791993
switch (phase) {
@@ -2011,6 +2025,8 @@ int msre_ruleset_rule_add(msre_ruleset *ruleset, msre_rule *rule, int phase) {
20112025
static msre_rule * msre_ruleset_fetch_phase_rule(const msre_ruleset *ruleset, const char *id,
20122026
const apr_array_header_t *phase_arr, int offset)
20132027
{
2028+
assert(id != NULL);
2029+
assert(phase_arr != NULL);
20142030
msre_rule **rules = (msre_rule **)phase_arr->elts;
20152031
int i;
20162032

@@ -2067,6 +2083,7 @@ msre_rule * msre_ruleset_fetch_rule(msre_ruleset *ruleset, const char *id, int o
20672083
static int msre_ruleset_phase_rule_remove_with_exception(msre_ruleset *ruleset, rule_exception *re,
20682084
apr_array_header_t *phase_arr)
20692085
{
2086+
assert(phase_arr != NULL);
20702087
msre_rule **rules;
20712088
int i, j, mode, removed_count;
20722089

@@ -2084,6 +2101,7 @@ static int msre_ruleset_phase_rule_remove_with_exception(msre_ruleset *ruleset,
20842101

20852102
/* Only remove non-placeholder rules */
20862103
if (rule->placeholder == RULE_PH_NONE) {
2104+
assert(re != NULL);
20872105
switch(re->type) {
20882106
case RULE_EXCEPTION_REMOVE_ID :
20892107
if (rule->actionset->id != NULL) {
@@ -2304,6 +2322,7 @@ char *msre_format_metadata(modsec_rec *msr, msre_actionset *actionset) {
23042322
char * msre_rule_generate_unparsed(apr_pool_t *pool, const msre_rule *rule, const char *targets,
23052323
const char *args, const char *actions)
23062324
{
2325+
assert(rule != NULL);
23072326
char *unparsed = NULL;
23082327
const char *r_targets = targets;
23092328
const char *r_args = args;
@@ -2363,12 +2382,19 @@ msre_rule *msre_rule_create(msre_ruleset *ruleset, int type,
23632382
const char *fn, int line, const char *targets,
23642383
const char *args, const char *actions, char **error_msg)
23652384
{
2385+
assert(ruleset != NULL);
2386+
assert(args != NULL);
2387+
assert(error_msg != NULL);
2388+
// Normally useless code, left to be safe for the moment
2389+
if (error_msg == NULL) {
2390+
ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, ruleset->mp, NULL, "msre_rule_create: error_msg is NULL");
2391+
return NULL;
2392+
}
23662393
msre_rule *rule;
23672394
char *my_error_msg;
23682395
const char *argsp;
23692396
int rc;
23702397

2371-
if (error_msg == NULL) return NULL;
23722398
*error_msg = NULL;
23732399

23742400
rule = (msre_rule *)apr_pcalloc(ruleset->mp, sizeof(msre_rule));
@@ -2521,6 +2547,8 @@ static void msre_perform_disruptive_actions(modsec_rec *msr, msre_rule *rule,
25212547
{
25222548
assert(msr != NULL);
25232549
assert(actionset != NULL);
2550+
assert(actionset->intercept_action_rec != NULL);
2551+
assert(actionset->intercept_action_rec->metadata != NULL);
25242552
const apr_array_header_t *tarr;
25252553
const apr_table_entry_t *telts;
25262554
int i;
@@ -2534,6 +2562,7 @@ static void msre_perform_disruptive_actions(modsec_rec *msr, msre_rule *rule,
25342562
telts = (const apr_table_entry_t*)tarr->elts;
25352563
for (i = 0; i < tarr->nelts; i++) {
25362564
msre_action *action = (msre_action *)telts[i].val;
2565+
assert(action->metadata != NULL);
25372566
if (action->metadata->type == ACTION_DISRUPTIVE) {
25382567
if (action->metadata->execute != NULL) {
25392568
action->metadata->execute(msr, mptmp, rule, action);
@@ -2797,6 +2826,11 @@ static int execute_operator(msre_var *var, msre_rule *rule, modsec_rec *msr,
27972826
* Executes rule against the given transaction.
27982827
*/
27992828
static apr_status_t msre_rule_process_normal(msre_rule *rule, modsec_rec *msr) {
2829+
assert(rule != NULL);
2830+
assert(rule->actionset != NULL);
2831+
assert(rule->targets != NULL);
2832+
assert(msr != NULL);
2833+
assert(msr->txcfg != NULL);
28002834
const apr_array_header_t *arr = NULL;
28012835
const apr_table_entry_t *te = NULL;
28022836
msre_actionset *acting_actionset = NULL;
@@ -3343,6 +3377,8 @@ static apr_status_t msre_rule_process_normal(msre_rule *rule, modsec_rec *msr) {
33433377
*
33443378
*/
33453379
static apr_status_t msre_rule_process_lua(msre_rule *rule, modsec_rec *msr) {
3380+
assert(rule != NULL);
3381+
assert(msr != NULL);
33463382
msre_actionset *acting_actionset = NULL;
33473383
char *my_error_msg = NULL;
33483384
int rc;
@@ -3380,6 +3416,7 @@ static apr_status_t msre_rule_process_lua(msre_rule *rule, modsec_rec *msr) {
33803416
*
33813417
*/
33823418
static apr_status_t msre_rule_process(msre_rule *rule, modsec_rec *msr) {
3419+
assert(msr != NULL);
33833420
/* Use a fresh memory sub-pool for processing each rule */
33843421
if (msr->msc_rule_mptmp == NULL) {
33853422
if (apr_pool_create(&msr->msc_rule_mptmp, msr->mp) != APR_SUCCESS) {

0 commit comments

Comments
 (0)