Skip to content

Commit 277e7e2

Browse files
authored
Merge pull request #3193 from marcstern/v2/pr/useless
Removed useless code
2 parents 8cfb911 + 692710c commit 277e7e2

File tree

8 files changed

+63
-105
lines changed

8 files changed

+63
-105
lines changed

apache2/apache2_io.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ static int flatten_response_body(modsec_rec *msr) {
629629
return -1;
630630
}
631631

632-
memset(msr->stream_output_data, 0, msr->stream_output_length+1);
633632
memcpy(msr->stream_output_data, msr->resbody_data, msr->stream_output_length);
634633
msr->stream_output_data[msr->stream_output_length] = '\0';
635634
} else if (msr->txcfg->stream_outbody_inspection && msr->txcfg->hash_is_enabled == HASH_ENABLED) {
@@ -662,7 +661,6 @@ static int flatten_response_body(modsec_rec *msr) {
662661
return -1;
663662
}
664663

665-
memset(msr->stream_output_data, 0, msr->stream_output_length+1);
666664
memcpy(msr->stream_output_data, msr->resbody_data, msr->stream_output_length);
667665
msr->stream_output_data[msr->stream_output_length] = '\0';
668666
}

apache2/msc_crypt.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,8 +1156,8 @@ int inject_hashed_response_body(modsec_rec *msr, int elts) {
11561156
return -1;
11571157
}
11581158

1159-
memset(msr->stream_output_data, 0x0, msr->stream_output_length+1);
11601159
memcpy(msr->stream_output_data, xmlOutputBufferGetContent(output_buf), msr->stream_output_length);
1160+
msr->stream_output_data[msr->stream_output_length] = '\0';
11611161

11621162
if (msr->txcfg->debuglog_level >= 4)
11631163
msr_log(msr, 4, "inject_hashed_response_body: Copying XML tree from CONTENT to stream buffer [%zu] bytes.", xmlOutputBufferGetSize(output_buf));
@@ -1187,8 +1187,8 @@ int inject_hashed_response_body(modsec_rec *msr, int elts) {
11871187
return -1;
11881188
}
11891189

1190-
memset(msr->stream_output_data, 0x0, msr->stream_output_length+1);
11911190
memcpy(msr->stream_output_data, xmlOutputBufferGetContent(output_buf), msr->stream_output_length);
1191+
msr->stream_output_data[msr->stream_output_length] = '\0';
11921192

11931193
if (msr->txcfg->debuglog_level >= 4)
11941194
msr_log(msr, 4, "inject_hashed_response_body: Copying XML tree from CONV to stream buffer [%zu] bytes.", xmlOutputBufferGetSize(output_buf));
@@ -1222,9 +1222,9 @@ int inject_hashed_response_body(modsec_rec *msr, int elts) {
12221222
return -1;
12231223
}
12241224

1225-
memset(msr->stream_output_data, 0x0, msr->stream_output_length+1);
12261225
memcpy(msr->stream_output_data, (char *)xmlBufferContent(output_buf->buffer), msr->stream_output_length);
12271226
//memcpy(msr->stream_output_data, output_buf->buffer->content, msr->stream_output_length);
1227+
msr->stream_output_data[msr->stream_output_length] = '\0';
12281228

12291229
if (msr->txcfg->debuglog_level >= 4)
12301230
msr_log(msr, 4, "inject_hashed_response_body: Copying XML tree from CONTENT to stream buffer [%d] bytes.", msr->stream_output_length);
@@ -1254,9 +1254,9 @@ int inject_hashed_response_body(modsec_rec *msr, int elts) {
12541254
return -1;
12551255
}
12561256

1257-
memset(msr->stream_output_data, 0x0, msr->stream_output_length+1);
12581257
memcpy(msr->stream_output_data, (char *)xmlBufferContent(output_buf->conv), msr->stream_output_length);
12591258
//memcpy(msr->stream_output_data, output_buf->conv->content, msr->stream_output_length);
1259+
msr->stream_output_data[msr->stream_output_length] = '\0';
12601260

12611261
if (msr->txcfg->debuglog_level >= 4)
12621262
msr_log(msr, 4, "inject_hashed_response_body: Copying XML tree from CONV to stream buffer [%d] bytes.", msr->stream_output_length);

apache2/msc_reqbody.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -461,37 +461,30 @@ apr_status_t modsecurity_request_body_to_stream(modsec_rec *msr, const char *buf
461461
if(data == NULL)
462462
return -1;
463463

464-
memset(data, 0, msr->stream_input_length + 1 - buflen);
465464
memcpy(data, msr->stream_input_data, msr->stream_input_length - buflen);
465+
data[msr->stream_input_length - buflen] = '\0';
466466

467467
stream_input_body = (char *)realloc(msr->stream_input_data, msr->stream_input_length + 1);
468468

469469
msr->stream_input_data = (char *)stream_input_body;
470470
}
471471

472472
if (msr->stream_input_data == NULL) {
473-
if(data) {
474-
free(data);
475-
data = NULL;
476-
}
473+
if (data) free(data);
477474
*error_msg = apr_psprintf(msr->mp, "Unable to allocate memory to hold request body on stream. Asked for %" APR_SIZE_T_FMT " bytes.",
478475
msr->stream_input_length + 1);
479476
return -1;
480477
}
481478

482-
memset(msr->stream_input_data, 0, msr->stream_input_length+1);
483-
484479
if(first_pkt) {
485480
memcpy(msr->stream_input_data, buffer, msr->stream_input_length);
486481
} else {
487482
memcpy(msr->stream_input_data, data, msr->stream_input_length - buflen);
488483
memcpy(msr->stream_input_data+(msr->stream_input_length - buflen), buffer, buflen);
489484
}
485+
msr->stream_input_data[msr->stream_input_length] = '\0';
490486

491-
if(data) {
492-
free(data);
493-
data = NULL;
494-
}
487+
if (data) free(data);
495488
#else
496489
if (msr->stream_input_data == NULL) {
497490
// Is the request body length known beforehand? (requests that are not Transfer-Encoding: chunked)

apache2/msc_util.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,28 +2473,16 @@ int msc_headers_to_buffer(const apr_array_header_t *arr, char *buffer,
24732473

24742474
int read_line(char *buf, int len, FILE *fp)
24752475
{
2476-
char *tmp;
2476+
if (buf == NULL) return -1;
24772477

2478-
if (buf == NULL)
2479-
{
2480-
return -1;
2481-
}
2482-
2483-
memset(buf, '\0', len*sizeof(char));
2484-
2485-
if (fgets(buf, len, fp) == NULL)
2486-
{
2478+
if (fgets(buf, len, fp) == NULL) {
24872479
*buf = '\0';
24882480
return 0;
24892481
}
2490-
else
2491-
{
2492-
if ((tmp = strrchr(buf, '\n')) != NULL)
2493-
{
2494-
*tmp = '\0';
2495-
}
2496-
}
2497-
2482+
2483+
char* tmp;
2484+
if ((tmp = strrchr(buf, '\n')) != NULL) *tmp = '\0';
2485+
24982486
return 1;
24992487
}
25002488

apache2/re.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,14 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
326326
if(value != NULL && targets[i]->param != NULL) {
327327
if((strlen(targets[i]->param) == strlen(value)) &&
328328
strncasecmp(targets[i]->param,value,strlen(targets[i]->param)) == 0) {
329-
memset(targets[i]->name,0,strlen(targets[i]->name));
330-
memset(targets[i]->param,0,strlen(targets[i]->param));
329+
targets[i]->name[0] = '\0';
330+
targets[i]->param[0] = '\0';
331331
targets[i]->is_counting = 0;
332332
targets[i]->is_negated = 1;
333333
match = 1;
334334
}
335335
} else if (value == NULL && targets[i]->param == NULL){
336-
memset(targets[i]->name,0,strlen(targets[i]->name));
336+
targets[i]->name[0] = '\0';
337337
targets[i]->is_counting = 0;
338338
targets[i]->is_negated = 1;
339339
match = 1;

apache2/re_actions.c

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,27 +1251,26 @@ static apr_status_t msre_action_ctl_execute(modsec_rec *msr, apr_pool_t *mptmp,
12511251
return -1;
12521252
}
12531253

1254-
re = apr_pcalloc(msr->mp, sizeof(rule_exception));
1255-
if (re == NULL) {
1256-
msr_log(msr, 1, "Ctl: Memory allocation error");
1257-
return -1;
1258-
}
1259-
re->type = RULE_EXCEPTION_REMOVE_ID;
1260-
re->param = (const char *)apr_pstrdup(msr->mp, p1);
1261-
if (re->param == NULL) {
1262-
msr_log(msr, 1, "Ctl: Memory allocation error");
1263-
return -1;
1264-
}
1265-
apr_table_addn(msr->removed_targets, apr_pstrdup(msr->mp, p2), (void *)re);
1266-
return 1;
1254+
re = apr_pcalloc(msr->mp, sizeof(rule_exception));
1255+
if (re == NULL) {
1256+
msr_log(msr, 1, "Ctl: Memory allocation error");
1257+
return -1;
1258+
}
1259+
re->type = RULE_EXCEPTION_REMOVE_ID;
1260+
re->param = (const char *)apr_pstrdup(msr->mp, p1);
1261+
if (re->param == NULL) {
1262+
msr_log(msr, 1, "Ctl: Memory allocation error");
1263+
return -1;
1264+
}
1265+
apr_table_addn(msr->removed_targets, apr_pstrdup(msr->mp, p2), (void *)re);
1266+
return 1;
12671267
} else
12681268
if (strcasecmp(name, "ruleRemoveTargetByTag") == 0) {
12691269
rule_exception *re = NULL;
12701270
char *p1 = NULL, *p2 = NULL;
12711271
char *savedptr = NULL;
12721272

12731273
p1 = apr_strtok(value,";",&savedptr);
1274-
12751274
p2 = apr_strtok(NULL,";",&savedptr);
12761275

12771276
if (msr->txcfg->debuglog_level >= 4) {
@@ -1282,24 +1281,23 @@ static apr_status_t msre_action_ctl_execute(modsec_rec *msr, apr_pool_t *mptmp,
12821281
return -1;
12831282
}
12841283

1285-
re = apr_pcalloc(msr->mp, sizeof(rule_exception));
1286-
re->type = RULE_EXCEPTION_REMOVE_TAG;
1287-
re->param = (const char *)apr_pstrdup(msr->mp, p1);
1288-
re->param_data = msc_pregcomp(msr->mp, p1, 0, NULL, NULL);
1289-
if (re->param_data == NULL) {
1290-
msr_log(msr, 1, "ModSecurity: Invalid regular expression \"%s\"", p1);
1291-
return -1;
1292-
}
1293-
apr_table_addn(msr->removed_targets, apr_pstrdup(msr->mp, p2), (void *)re);
1294-
return 1;
1284+
re = apr_pcalloc(msr->mp, sizeof(rule_exception));
1285+
re->type = RULE_EXCEPTION_REMOVE_TAG;
1286+
re->param = (const char *)apr_pstrdup(msr->mp, p1);
1287+
re->param_data = msc_pregcomp(msr->mp, p1, 0, NULL, NULL);
1288+
if (re->param_data == NULL) {
1289+
msr_log(msr, 1, "ModSecurity: Invalid regular expression \"%s\"", p1);
1290+
return -1;
1291+
}
1292+
apr_table_addn(msr->removed_targets, apr_pstrdup(msr->mp, p2), (void *)re);
1293+
return 1;
12951294
} else
12961295
if (strcasecmp(name, "ruleRemoveTargetByMsg") == 0) {
12971296
rule_exception *re = NULL;
12981297
char *p1 = NULL, *p2 = NULL;
12991298
char *savedptr = NULL;
13001299

13011300
p1 = apr_strtok(value,";",&savedptr);
1302-
13031301
p2 = apr_strtok(NULL,";",&savedptr);
13041302

13051303
if (msr->txcfg->debuglog_level >= 4) {
@@ -1310,23 +1308,20 @@ static apr_status_t msre_action_ctl_execute(modsec_rec *msr, apr_pool_t *mptmp,
13101308
return -1;
13111309
}
13121310

1313-
re = apr_pcalloc(msr->mp, sizeof(rule_exception));
1314-
re->type = RULE_EXCEPTION_REMOVE_MSG;
1315-
re->param = apr_pstrdup(msr->mp, p1);
1316-
re->param_data = msc_pregcomp(msr->mp, p1, 0, NULL, NULL);
1317-
if (re->param_data == NULL) {
1318-
msr_log(msr, 1, "ModSecurity: Invalid regular expression \"%s\"", p1);
1319-
return -1;
1320-
}
1321-
apr_table_addn(msr->removed_targets, apr_pstrdup(msr->mp, p2), (void *)re);
1322-
return 1;
1323-
}
1324-
else {
1325-
/* Should never happen, but log if it does. */
1326-
msr_log(msr, 1, "Internal Error: Unknown ctl action \"%s\".", name);
1327-
return -1;
1311+
re = apr_pcalloc(msr->mp, sizeof(rule_exception));
1312+
re->type = RULE_EXCEPTION_REMOVE_MSG;
1313+
re->param = apr_pstrdup(msr->mp, p1);
1314+
re->param_data = msc_pregcomp(msr->mp, p1, 0, NULL, NULL);
1315+
if (re->param_data == NULL) {
1316+
msr_log(msr, 1, "ModSecurity: Invalid regular expression \"%s\"", p1);
1317+
return -1;
1318+
}
1319+
apr_table_addn(msr->removed_targets, apr_pstrdup(msr->mp, p2), (void *)re);
1320+
return 1;
13281321
}
13291322

1323+
/* Should never happen, but log if it does. */
1324+
msr_log(msr, 1, "Internal Error: Unknown ctl action \"%s\".", name);
13301325
return -1;
13311326
}
13321327

@@ -1764,7 +1759,7 @@ static apr_status_t msre_action_setvar_parse(modsec_rec *msr, apr_pool_t *mptmp,
17641759
var_value = s + 1;
17651760
*s = '\0';
17661761

1767-
while ((*var_value != '\0')&&(isspace(*var_value))) var_value++;
1762+
while (isspace(*var_value)) var_value++;
17681763
}
17691764

17701765
return msre_action_setvar_execute(msr,mptmp,rule,var_name,var_value);

apache2/re_operators.c

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -617,43 +617,31 @@ static int msre_op_rsub_execute(modsec_rec *msr, msre_rule *rule, msre_var *var,
617617
size+=sl;
618618
*data_out=0;
619619

620-
if(msr->stream_output_data != NULL && output_body == 1) {
621-
622-
memset(msr->stream_output_data, 0x0, msr->stream_output_length);
620+
if (msr->stream_output_data != NULL && output_body == 1) {
623621
free(msr->stream_output_data);
624622
msr->stream_output_data = NULL;
625623
msr->stream_output_length = 0;
626-
627624
msr->stream_output_data = (char *)malloc(size+1);
628-
629-
if(msr->stream_output_data == NULL) {
630-
return -1;
631-
}
625+
if (msr->stream_output_data == NULL) return -1;
632626

633627
msr->stream_output_length = size;
634-
memset(msr->stream_output_data, 0x0, size+1);
635-
636628
msr->of_stream_changed = 1;
637-
638629
memcpy(msr->stream_output_data, data, size);
639630
msr->stream_output_data[size] = '\0';
640631

641632
var->value_len = size;
642633
var->value = msr->stream_output_data;
643634
}
644635

645-
if(msr->stream_input_data != NULL && input_body == 1) {
646-
memset(msr->stream_input_data, 0x0, msr->stream_input_length);
636+
if (msr->stream_input_data != NULL && input_body == 1) {
647637
free(msr->stream_input_data);
648638
msr->stream_input_data = NULL;
649639
msr->stream_input_length = 0;
650640
#ifdef MSC_LARGE_STREAM_INPUT
651641
msr->stream_input_allocated_length = 0;
652642
#endif
653643
msr->stream_input_data = (char *)malloc(size+1);
654-
if(msr->stream_input_data == NULL) {
655-
return -1;
656-
}
644+
if(msr->stream_input_data == NULL) return -1;
657645

658646
msr->stream_input_length = size;
659647
#ifdef MSC_LARGE_STREAM_INPUT
@@ -1573,12 +1561,11 @@ static const char *gsb_replace_tpath(apr_pool_t *pool, const char *domain, int l
15731561
int match = 0;
15741562

15751563
url = apr_palloc(pool, len + 1);
1564+
if (!url) return NULL;
15761565
data = apr_palloc(pool, len + 1);
1566+
if (!data) return NULL;
15771567

1578-
memset(data, 0, len+1);
1579-
memset(url, 0, len+1);
1580-
1581-
memcpy(url, domain, len);
1568+
url[len] = '\0';
15821569

15831570
while(( pos = strstr(url , "/./" )) != NULL) {
15841571
match = 1;
@@ -1589,8 +1576,7 @@ static const char *gsb_replace_tpath(apr_pool_t *pool, const char *domain, int l
15891576
strncpy(url , data, len);
15901577
}
15911578

1592-
if(match == 0)
1593-
return domain;
1579+
if (match == 0) return domain;
15941580

15951581
return url;
15961582
}
@@ -1681,16 +1667,14 @@ static int verify_gsb(gsb_db *gsb, modsec_rec *msr, const char *match, unsigned
16811667
const char *hash = NULL;
16821668
const char *search = NULL;
16831669

1684-
memset(digest, 0, sizeof(digest));
1685-
16861670
apr_md5_init(&ctx);
16871671

16881672
if ((rc = apr_md5_update(&ctx, match, match_length)) != APR_SUCCESS)
16891673
return -1;
16901674

16911675
apr_md5_final(digest, &ctx);
16921676

1693-
hash = apr_psprintf(msr->mp, "%s", bytes2hex(msr->mp, digest, 16));
1677+
hash = apr_psprintf(msr->mp, "%s", bytes2hex(msr->mp, digest, APR_MD5_DIGESTSIZE));
16941678

16951679
if ((hash != NULL) && (gsb->gsb_table != NULL)) {
16961680
search = apr_hash_get(gsb->gsb_table, hash, APR_HASH_KEY_STRING);

apache2/re_variables.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2454,7 +2454,7 @@ static int var_full_request_generate(modsec_rec *msr, msre_var *var,
24542454
}
24552455
goto failed_not_enough_mem;
24562456
}
2457-
memset(full_request, '\0', sizeof(char)*msr->msc_full_request_length);
2457+
full_request[0] = '\0';
24582458
msr->msc_full_request_buffer = full_request;
24592459
msr->msc_full_request_length = full_request_length;
24602460

0 commit comments

Comments
 (0)