Skip to content

Commit fff87b3

Browse files
committed
wip
1 parent 1f674a6 commit fff87b3

31 files changed

+556
-157
lines changed

appsec/src/extension/commands/client_init.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ static dd_result _process_response(
245245
return dd_error;
246246
}
247247

248-
static void _process_meta_and_metrics(mpack_node_t root, struct req_info *nonnull ctx)
248+
static void _process_meta_and_metrics(
249+
mpack_node_t root, struct req_info *nonnull ctx)
249250
{
250251
zend_object *span = ctx->root_span;
251252
if (!span) {

appsec/src/extension/commands/request_exec.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct ctx {
1818
};
1919

2020
static dd_result _pack_command(
21-
mpack_writer_t *nonnull w, ATTR_UNUSED void *nullable ctx);
21+
mpack_writer_t *nonnull w, void *nonnull ctx);
2222

2323
static const dd_command_spec _spec = {
2424
.name = "request_exec",
@@ -37,13 +37,13 @@ dd_result dd_request_exec(dd_conn *nonnull conn, zval *nonnull data)
3737
return dd_error;
3838
}
3939

40-
struct ctx ctx = { .data = data };
40+
struct ctx ctx = {.data = data};
4141

4242
return dd_command_exec(conn, &_spec, &ctx);
4343
}
4444

4545
static dd_result _pack_command(
46-
mpack_writer_t *nonnull w, ATTR_UNUSED void *nullable _ctx)
46+
mpack_writer_t *nonnull w, void *nonnull _ctx)
4747
{
4848
assert(_ctx != NULL);
4949
struct ctx *ctx = _ctx;

appsec/src/extension/commands/request_init.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <zend_string.h>
2323

2424
static dd_result _request_pack(
25-
mpack_writer_t *nonnull w, void *nullable ATTR_UNUSED ctx);
25+
mpack_writer_t *nonnull w, void *nonnull ctx);
2626
static void _init_autoglobals(void);
2727
static void _pack_headers(
2828
mpack_writer_t *nonnull w, const zend_array *nonnull server);
@@ -48,9 +48,9 @@ dd_result dd_request_init(
4848
return dd_command_exec(conn, &_spec, ctx);
4949
}
5050

51-
static dd_result _request_pack(mpack_writer_t *nonnull w, void *nullable _ctx)
51+
static dd_result _request_pack(mpack_writer_t *nonnull w, void *nonnull _ctx)
5252
{
53-
struct req_info_init *nullable ctx = _ctx;
53+
struct req_info_init *nonnull ctx = _ctx;
5454

5555
bool send_raw_body = get_global_DD_APPSEC_TESTING() &&
5656
get_global_DD_APPSEC_TESTING_RAW_BODY();

appsec/src/extension/ddappsec.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,7 @@ static PHP_MSHUTDOWN_FUNCTION(ddappsec)
235235

236236
static pthread_once_t _rinit_once_control = PTHREAD_ONCE_INIT;
237237

238-
static void _rinit_once()
239-
{
240-
dd_config_first_rinit();
241-
}
238+
static void _rinit_once() { dd_config_first_rinit(); }
242239

243240
// NOLINTNEXTLINE
244241
static PHP_RINIT_FUNCTION(ddappsec)
@@ -352,8 +349,8 @@ static void _check_enabled()
352349
DDAPPSEC_G(active) = false;
353350
DDAPPSEC_G(to_be_configured) = false;
354351
} else if (!dd_cfg_enable_via_remcfg()) {
355-
DDAPPSEC_G(enabled) =
356-
get_DD_APPSEC_ENABLED() ? APPSEC_FULLY_ENABLED : APPSEC_FULLY_DISABLED;
352+
DDAPPSEC_G(enabled) = get_DD_APPSEC_ENABLED() ? APPSEC_FULLY_ENABLED
353+
: APPSEC_FULLY_DISABLED;
357354
DDAPPSEC_G(active) = get_DD_APPSEC_ENABLED() ? true : false;
358355
DDAPPSEC_G(to_be_configured) = false;
359356
} else {

appsec/src/extension/ddtrace.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ static void _register_testing_objects(void);
3636
static zend_object *(*nullable _ddtrace_get_root_span)(void);
3737
static void (*nullable _ddtrace_close_all_spans_and_flush)(void);
3838
static void (*nullable _ddtrace_set_priority_sampling_on_span_zobj)(
39-
zend_object *nonnull zobj, zend_long priority, enum dd_sampling_mechanism mechanism);
39+
zend_object *nonnull zobj, zend_long priority,
40+
enum dd_sampling_mechanism mechanism);
4041

4142
static bool (*nullable _ddtrace_user_req_add_listeners)(
4243
ddtrace_user_req_listeners *listeners);
@@ -64,7 +65,7 @@ static void dd_trace_load_symbols(void)
6465
_ddtrace_get_root_span = dlsym(handle, "ddtrace_get_root_span");
6566
if (_ddtrace_get_root_span == NULL && !testing) {
6667
mlog(dd_log_error, "Failed to load ddtrace_get_root_span: %s",
67-
dlerror()); // NOLINT(concurrency-mt-unsafe)
68+
dlerror()); // NOLINT(concurrency-mt-unsafe)
6869
}
6970

7071
_ddtrace_runtime_id = dlsym(handle, "ddtrace_runtime_id");
@@ -245,8 +246,8 @@ static zval *_get_span_modifiable_array_property(
245246
ZVAL_OBJ(&obj, zobj);
246247
zval prop;
247248
ZVAL_STR(&prop, propname);
248-
zval *res = zobj->handlers->get_property_ptr_ptr(&obj, &prop, BP_VAR_R,
249-
NULL);
249+
zval *res =
250+
zobj->handlers->get_property_ptr_ptr(&obj, &prop, BP_VAR_R, NULL);
250251

251252
#endif
252253

@@ -314,7 +315,8 @@ void dd_trace_set_priority_sampling_on_span_zobj(zend_object *nonnull root_span,
314315
_ddtrace_set_priority_sampling_on_span_zobj(root_span, priority, mechanism);
315316
}
316317

317-
bool dd_trace_user_req_add_listeners(ddtrace_user_req_listeners *nonnull listeners)
318+
bool dd_trace_user_req_add_listeners(
319+
ddtrace_user_req_listeners *nonnull listeners)
318320
{
319321
if (_ddtrace_user_req_add_listeners == NULL) {
320322
return false;

appsec/src/extension/ip_extraction.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ static bool _parse_forwarded(zend_string *nonnull zvalue, ipaddr *nonnull out)
269269
} state = between;
270270
const char *r = ZSTR_VAL(zvalue);
271271
const char *end = r + ZSTR_LEN(zvalue);
272-
const char *start;
272+
const char *start = r; // meaningless assignment to satisfy static analysis
273273
bool consider_value = false;
274274

275275
// https://datatracker.ietf.org/doc/html/rfc7239#section-4

appsec/src/extension/msgpack_helpers.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ void dd_mpack_write_array(
7474
if (arr_type == php_array_type_sequential) {
7575
mpack_start_array(w, num_elems);
7676
zval *val;
77-
ZEND_HASH_FOREACH_VAL((zend_array*)arr, val) { _mpack_write_zval(w, val); }
77+
ZEND_HASH_FOREACH_VAL((zend_array *)arr, val)
78+
{
79+
_mpack_write_zval(w, val);
80+
}
7881
ZEND_HASH_FOREACH_END();
7982
mpack_finish_array(w);
8083
} else {
@@ -83,7 +86,7 @@ void dd_mpack_write_array(
8386
zend_string *key_s;
8487
zend_ulong key_i;
8588
zval *val;
86-
ZEND_HASH_FOREACH_KEY_VAL((zend_array*)arr, key_i, key_s, val)
89+
ZEND_HASH_FOREACH_KEY_VAL((zend_array *)arr, key_i, key_s, val)
8790
{
8891
if (key_s) {
8992
mpack_write_str(w, ZSTR_VAL(key_s), ZSTR_LEN(key_s));

appsec/src/extension/php_compat.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ static const uint32_t uninitialized_bucket[-HT_MIN_MASK] = {
2020
HT_INVALID_IDX, HT_INVALID_IDX};
2121

2222
const HashTable zend_empty_array = {.gc.refcount = 2,
23-
.gc.u.type_info = IS_ARRAY,
23+
.gc.u.v.type = IS_ARRAY,
24+
.gc.u.v.flags = IS_ARRAY_IMMUTABLE,
2425
.u.flags =
2526
HASH_FLAG_STATIC_KEYS | HASH_FLAG_INITIALIZED | HASH_FLAG_PERSISTENT,
2627
.nTableMask = HT_MIN_MASK,

appsec/src/extension/php_compat.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,22 @@ extern const HashTable zend_empty_array;
3838

3939
# define GC_ADDREF(x) (++GC_REFCOUNT(x))
4040
# define GC_DELREF(x) (--GC_REFCOUNT(x))
41-
# define GC_TRY_ADDREF GC_ADDREF
42-
# define GC_TRY_DELREF GC_DELREF
41+
static zend_always_inline void _gc_try_addref(zend_refcounted_h *_rc)
42+
{
43+
struct _zend_refcounted *rc = (struct _zend_refcounted *)_rc;
44+
if (!(GC_FLAGS(rc) & IS_ARRAY_IMMUTABLE)) {
45+
GC_ADDREF(rc);
46+
}
47+
}
48+
# define GC_TRY_ADDREF(p) _gc_try_addref(&(p)->gc)
49+
static zend_always_inline void _gc_try_delref(zend_refcounted_h *_rc)
50+
{
51+
struct _zend_refcounted *rc = (struct _zend_refcounted *)_rc;
52+
if (!(GC_FLAGS(rc) & IS_ARRAY_IMMUTABLE)) {
53+
GC_DELREF(rc);
54+
}
55+
}
56+
# define GC_TRY_DELREF(p) _gc_try_delref(&(p)->gc)
4357

4458
zend_bool zend_ini_parse_bool(zend_string *str);
4559
# define zend_string_efree zend_string_free
@@ -56,19 +70,21 @@ static inline HashTable *zend_new_array(uint32_t nSize) {
5670
# define tsrm_env_unlock()
5771
#endif
5872

59-
#if PHP_VERSION_ID >= 70300 && PHP_VERSION_ID < 80100
73+
#if PHP_VERSION_ID >= 70300 && PHP_VERSION_ID < 80000
6074
static zend_always_inline void _gc_try_addref(zend_refcounted_h *rc)
6175
{
6276
if (!(rc->u.type_info & GC_IMMUTABLE)) {
6377
rc->refcount++;
6478
}
6579
}
80+
#define GC_TRY_ADDREF(p) _gc_try_addref(&(p)->gc)
81+
#endif
82+
#if PHP_VERSION_ID >= 70300 && PHP_VERSION_ID < 80100
6683
static zend_always_inline void _gc_try_delref(zend_refcounted_h *rc)
6784
{
6885
if (!(rc->u.type_info & GC_IMMUTABLE)) {
6986
rc->refcount--;
7087
}
7188
}
72-
#define GC_TRY_ADDREF(p) _gc_try_addref(&(p)->gc)
7389
#define GC_TRY_DELREF(p) _gc_try_delref(&(p)->gc)
7490
#endif

appsec/src/extension/request_abort.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ static void _set_output_zstr(const zend_string *str)
139139
_set_output(ZSTR_VAL(str), ZSTR_LEN(str));
140140
}
141141

142-
static dd_response_type _get_response_type_from_accept_header(const zend_array *nonnull _server)
142+
static dd_response_type _get_response_type_from_accept_header(
143+
const zend_array *nonnull _server)
143144
{
144-
const zend_string *accept_zstr = dd_php_get_string_elem_cstr(
145-
_server, LSTRARG("HTTP_ACCEPT"));
145+
const zend_string *accept_zstr =
146+
dd_php_get_string_elem_cstr(_server, LSTRARG("HTTP_ACCEPT"));
146147
if (!accept_zstr) {
147148
mlog(dd_log_info,
148149
"Could not find Accept header, using default content-type");
@@ -284,7 +285,6 @@ void _request_abort_static_page(int response_code, int type)
284285
}
285286
}
286287

287-
288288
zend_string *body = NULL;
289289
const char *content_type;
290290
if (response_type == response_type_html) {

appsec/src/extension/request_abort.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ void dd_request_abort_static_page(void);
2828
zend_array *nonnull dd_request_abort_static_page_spec(
2929
const zend_array *nonnull);
3030
void dd_request_abort_redirect(void);
31-
zend_array *nonnull dd_request_abort_redirect_spec();
31+
zend_array *nonnull dd_request_abort_redirect_spec(void);

appsec/src/extension/request_lifecycle.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ static THREAD_LOCAL_ON_ZTS zval _blocking_function;
4545
static THREAD_LOCAL_ON_ZTS bool _shutdown_done_on_commit;
4646
#define CLIENT_IP_LOOKUP_FAILED ((zend_string *)-1)
4747

48-
bool dd_req_is_user_req()
49-
{
50-
return _enabled_user_req;
51-
}
48+
bool dd_req_is_user_req() { return _enabled_user_req; }
5249

5350
void dd_req_lifecycle_startup()
5451
{
@@ -107,10 +104,7 @@ void dd_req_lifecycle_rinit(bool force)
107104
_do_request_begin_php();
108105
}
109106

110-
static void _do_request_begin_php()
111-
{
112-
(void) _do_request_begin(false);
113-
}
107+
static void _do_request_begin_php() { (void)_do_request_begin(false); }
114108

115109
static zend_array *nullable _do_request_begin_user_req()
116110
{
@@ -248,9 +242,9 @@ static void _do_request_finish_php(bool ignore_verdict)
248242
}
249243
}
250244

251-
static zend_array *_do_request_finish_user_req(
252-
bool ignore_verdict, zend_array *nonnull superglob_equiv,
253-
int status_code, zend_array *nullable resp_headers)
245+
static zend_array *_do_request_finish_user_req(bool ignore_verdict,
246+
zend_array *nonnull superglob_equiv, int status_code,
247+
zend_array *nullable resp_headers)
254248
{
255249
int verdict = dd_success;
256250
dd_conn *conn = dd_helper_mgr_cur_conn();
@@ -410,10 +404,10 @@ static zend_array *nullable _response_commit(
410404
}
411405

412406
if (!_cur_req_span) {
413-
mlog(
414-
dd_log_warning, "Request commit callback called, but there is no "
415-
"root span currently associated through the "
416-
"request started span (or it was cleared already)");
407+
mlog(dd_log_warning,
408+
"Request commit callback called, but there is no "
409+
"root span currently associated through the "
410+
"request started span (or it was cleared already)");
417411
return NULL;
418412
}
419413
if (_cur_req_span != span) {
@@ -455,11 +449,11 @@ static void _finish_user_req(
455449
}
456450

457451
if (_cur_req_span == NULL) {
458-
mlog(
459-
dd_log_warning, "Request finished callback called, but there is no "
460-
"root span currently associated through the "
461-
"request started span (or it was cleared already). "
462-
"Resetting");
452+
mlog(dd_log_warning,
453+
"Request finished callback called, but there is no "
454+
"root span currently associated through the "
455+
"request started span (or it was cleared already). "
456+
"Resetting");
463457
_reset_globals();
464458
return;
465459
}
@@ -492,7 +486,7 @@ const zend_array *nonnull _get_server_equiv(
492486
}
493487

494488
static void _set_blocking_function(ddtrace_user_req_listeners *nonnull self,
495-
zend_object *nonnull span, zval *nonnull blocking_function)
489+
zend_object *nonnull span, zval *nonnull blocking_function)
496490
{
497491
UNUSED(self);
498492

@@ -549,8 +543,10 @@ void dd_req_call_blocking_function(dd_result res)
549543
} else if (res == dd_should_redirect) {
550544
spec = dd_request_abort_redirect_spec(sv);
551545
} else {
552-
mlog(dd_log_warning, "dd_req_call_blocking_function called with "
553-
"invalid result %d", res);
546+
mlog(dd_log_warning,
547+
"dd_req_call_blocking_function called with "
548+
"invalid result %d",
549+
res);
554550
}
555551
assert(spec != NULL);
556552

appsec/src/extension/tags.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ void dd_tags_add_tags(
344344
_set_runtime_family(span);
345345

346346
if (_force_keep) {
347-
dd_trace_set_priority_sampling_on_span_zobj(span,
348-
PRIORITY_SAMPLING_USER_KEEP, DD_MECHANISM_MANUAL);
347+
dd_trace_set_priority_sampling_on_span_zobj(
348+
span, PRIORITY_SAMPLING_USER_KEEP, DD_MECHANISM_MANUAL);
349349
mlog(dd_log_debug, "Updated sampling priority to user_keep");
350350
}
351351

@@ -454,7 +454,8 @@ static void _dd_response_headers(zend_array *meta_ht);
454454
static void _dd_event_user_id(zend_array *meta_ht);
455455
static void _dd_appsec_blocked(zend_array *meta_ht);
456456

457-
static void _add_basic_ancillary_tags(zend_object *nonnull span, const zend_array *nonnull server)
457+
static void _add_basic_ancillary_tags(
458+
zend_object *nonnull span, const zend_array *nonnull server)
458459
{
459460
zval *nullable meta = dd_trace_span_get_meta(span);
460461
if (!meta) {
@@ -795,8 +796,8 @@ static zend_string *nullable _is_relevant_resp_header(
795796

796797
void _set_runtime_family(zend_object *nonnull span)
797798
{
798-
bool res = dd_trace_span_add_tag_str(span,
799-
LSTRARG(DD_TAG_RUNTIME_FAMILY), LSTRARG("php"));
799+
bool res = dd_trace_span_add_tag_str(
800+
span, LSTRARG(DD_TAG_RUNTIME_FAMILY), LSTRARG("php"));
800801
if (!res && !get_global_DD_APPSEC_TESTING()) {
801802
mlog(dd_log_warning,
802803
"Failed to add " DD_TAG_RUNTIME_FAMILY " to root span");
@@ -995,7 +996,6 @@ static PHP_FUNCTION(datadog_appsec_track_user_login_success_event)
995996
}
996997
}
997998

998-
999999
zval *nullable meta = _root_span_get_meta();
10001000
if (!meta) {
10011001
return;
@@ -1071,7 +1071,6 @@ static PHP_FUNCTION(datadog_appsec_track_user_login_failure_event)
10711071
}
10721072
}
10731073

1074-
10751074
zval *nullable meta = _root_span_get_meta();
10761075
if (!meta) {
10771076
return;
@@ -1137,7 +1136,6 @@ static PHP_FUNCTION(datadog_appsec_track_custom_event)
11371136
return;
11381137
}
11391138

1140-
11411139
zval *nullable meta = _root_span_get_meta();
11421140
if (!meta) {
11431141
return;

appsec/src/helper/remote_config/http_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ std::string execute_request(const std::string &host, const std::string &port,
6969
// If we get here then the connection is closed gracefully
7070
} catch (std::exception const &e) {
7171
auto sv = request.target();
72-
std::string err{sv.data(), sv.size()};
72+
const std::string err{sv.data(), sv.size()};
7373
SPDLOG_ERROR("Connection error - {} - {}", err, e.what());
7474
throw dds::remote_config::network_exception(
7575
"Connection error - " + err + " - " + e.what());

0 commit comments

Comments
 (0)