From f40955e03cd361966f927acfaa477cfceb8930e5 Mon Sep 17 00:00:00 2001 From: Christian Göttsche Date: Wed, 29 May 2024 20:38:23 +0200 Subject: Avoid dropping const qualifier in casts Adjusts casts to void dropping const qualifiers. This helps to avoid mistakes, e.g. modifying string literals. Also use size_t for length, similar to the upstream php interfaces. --- src/sp_config.h | 2 +- src/sp_config_scanner.h | 4 ++-- src/sp_config_scanner.re | 10 +++++----- src/sp_cookie_encryption.c | 2 +- src/sp_crypt.c | 4 ++-- src/sp_ifilter.c | 4 ++-- src/sp_utils.h | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/sp_config.h b/src/sp_config.h index f957d15..f245943 100644 --- a/src/sp_config.h +++ b/src/sp_config.h @@ -188,7 +188,7 @@ typedef struct { typedef struct { SP_PARSE_FN((*func)); - char *token; + const char *token; void *retval; } sp_config_keyword; diff --git a/src/sp_config_scanner.h b/src/sp_config_scanner.h index 71f8f7e..560ea6e 100644 --- a/src/sp_config_scanner.h +++ b/src/sp_config_scanner.h @@ -10,9 +10,9 @@ typedef enum { } sp_argtype; typedef struct { - char *kw; // keyword points directly to the parsed input text and as such is not null-terminated + const char *kw; // keyword points directly to the parsed input text and as such is not null-terminated size_t kwlen; - char *arg; // optional argument / can be not null terminated + const char *arg; // optional argument / can be not null terminated size_t arglen; sp_argtype argtype; long lineno; diff --git a/src/sp_config_scanner.re b/src/sp_config_scanner.re index 82359d6..8f0b24e 100644 --- a/src/sp_config_scanner.re +++ b/src/sp_config_scanner.re @@ -197,8 +197,8 @@ zend_result sp_config_scan(const char *data, zend_result (*process_rule)(sp_pars end { ret = SUCCESS; goto out; } "@"? "set" whitespace+ @t1 keyword @t2 whitespace+ @t3 string @t4 whitespace* ";" { if (!cond_res[0]) { goto yyc_init; } - char *key = (char*)t1; - int keylen = t2 - t1; + const char *key = t1; + size_t keylen = t2 - t1; zend_string *tmp = zend_hash_str_find_ptr(&vars, key, keylen); if (tmp) { zend_hash_str_del(&vars, key, keylen); @@ -304,9 +304,9 @@ zend_result sp_config_scan(const char *data, zend_result (*process_rule)(sp_pars goto out; } sp_parsed_keyword kw = { - .kw = (char*)t1, + .kw = t1, .kwlen = t2-t1, - .arg = (char*)t3, + .arg = t3, .arglen = t4-t3, .argtype = SP_ARGTYPE_UNKNOWN, .lineno = lineno @@ -315,7 +315,7 @@ zend_result sp_config_scan(const char *data, zend_result (*process_rule)(sp_pars if (t3 == t4) { kw.argtype = SP_ARGTYPE_EMPTY; } else if (t4-t3 >= 2 && *t3 == '"') { - kw.arg = (char*)t3 + 1; + kw.arg = t3 + 1; kw.arglen = t4 - t3 - 2; kw.argtype = SP_ARGTYPE_STR; } else { diff --git a/src/sp_cookie_encryption.c b/src/sp_cookie_encryption.c index b2cff66..8b24a9c 100644 --- a/src/sp_cookie_encryption.c +++ b/src/sp_cookie_encryption.c @@ -98,7 +98,7 @@ PHP_FUNCTION(sp_setcookie) { zval *expires_or_options = NULL; zend_bool secure = 0, httponly = 0; const sp_cookie *cookie_node = NULL; - char *cookie_samesite; + const char *cookie_samesite; // LCOV_EXCL_BR_START ZEND_PARSE_PARAMETERS_START(1, 7) diff --git a/src/sp_crypt.c b/src/sp_crypt.c index a27cc67..6d48554 100644 --- a/src/sp_crypt.c +++ b/src/sp_crypt.c @@ -14,11 +14,11 @@ void generate_key(unsigned char *key) { PHP_SHA256Init(&ctx); if (user_agent) { - PHP_SHA256Update(&ctx, (unsigned char *)user_agent, strlen(user_agent)); + PHP_SHA256Update(&ctx, (const unsigned char *)user_agent, strlen(user_agent)); } if (env_var) { - PHP_SHA256Update(&ctx, (unsigned char *)env_var, strlen(env_var)); + PHP_SHA256Update(&ctx, (const unsigned char *)env_var, strlen(env_var)); } else { sp_log_warn("cookie_encryption", "The environment variable '%s' " diff --git a/src/sp_ifilter.c b/src/sp_ifilter.c index 9c46875..67eb5f3 100644 --- a/src/sp_ifilter.c +++ b/src/sp_ifilter.c @@ -24,7 +24,7 @@ static const char sp_is_dangerous_char[256] = { /* 0xf0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -static void sp_server_strip(HashTable *svars, char *key, int keylen) { +static void sp_server_strip(HashTable *svars, const char *key, size_t keylen) { zval *value = zend_hash_str_find(svars, key, keylen); if (!value || Z_TYPE_P(value) != IS_STRING) { return; } @@ -39,7 +39,7 @@ static void sp_server_strip(HashTable *svars, char *key, int keylen) { } } -static void sp_server_encode(HashTable *svars, char *key, int keylen) { +static void sp_server_encode(HashTable *svars, const char *key, size_t keylen) { zval *value = zend_hash_str_find(svars, key, keylen); if (!value || Z_TYPE_P(value) != IS_STRING) { return; } diff --git a/src/sp_utils.h b/src/sp_utils.h index 7bab4ba..36caa52 100644 --- a/src/sp_utils.h +++ b/src/sp_utils.h @@ -84,7 +84,7 @@ void unhook_functions(HashTable *ht); int hook_regexp(const sp_pcre *, HashTable *, zif_handler); bool check_is_in_eval_whitelist(const char* function_name); int sp_log_request(zend_string const* const restrict folder, zend_string const* const restrict text_repr); -#define sp_zend_string_equals(s1, s2) zend_string_equals((zend_string*)s1, (zend_string*)s2) +#define sp_zend_string_equals(s1, s2) zend_string_equals((const zend_string*)s1, (const zend_string*)s2) static inline bool sp_zend_string_equals_str(const zend_string* s1, const char *str, size_t len) { return (ZSTR_LEN(s1) == len && !memcmp(ZSTR_VAL(s1), str, len)); } -- cgit v1.3