Skip to content

Commit

Permalink
Fix an UBSAN warning
Browse files Browse the repository at this point in the history
No need to use an ulong, an uint is more than enough for our usecases.

This fixes the following warning:

```
src/sp_config.c:207:20: runtime error: store to misaligned address 0x796d1f9a78d4 for type 'u_long', which requires 8 byte alignment
0x796d1f9a78d4: note: pointer points here
  00 00 00 00 ff 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
              ^
    #0 0x796d1f92251a in parse_ulong src/sp_config.c:207
    #1 0x796d1f9239a9 in sp_process_rule src/sp_config.c:76
    #2 0x796d1f92404f in sp_process_config_root src/sp_config.c:31
    #3 0x796d1f96770d in sp_config_scan src/sp_config_scanner.c:1482
    #4 0x796d1f922cd3 in sp_parse_config src/sp_config.c:62
    #5 0x796d1f91e6cf in OnUpdateConfiguration src/snuffleupagus.c:522
    #6 0x598aae727646 in zend_register_ini_entries_ex (/usr/bin/php8.2+0x36d646) (BuildId: 5228f916ded87172ddf0f3eca448f1e43874d60f)
    #7 0x796d1f91cae1 in zm_startup_snuffleupagus src/snuffleupagus.c:121
    #8 0x598aae6b70df in zend_startup_module_ex (/usr/bin/php8.2+0x2fd0df) (BuildId: 5228f916ded87172ddf0f3eca448f1e43874d60f)
    #9 0x598aae6b717f  (/usr/bin/php8.2+0x2fd17f) (BuildId: 5228f916ded87172ddf0f3eca448f1e43874d60f)
    #10 0x598aae6c566a in zend_hash_apply (/usr/bin/php8.2+0x30b66a) (BuildId: 5228f916ded87172ddf0f3eca448f1e43874d60f)
    #11 0x598aae64c0b5 in php_module_startup (/usr/bin/php8.2+0x2920b5) (BuildId: 5228f916ded87172ddf0f3eca448f1e43874d60f)
    #12 0x598aae4e2265  (/usr/bin/php8.2+0x128265) (BuildId: 5228f916ded87172ddf0f3eca448f1e43874d60f)
    #13 0x796d21b4dc89 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #14 0x796d21b4dd44 in __libc_start_main_impl ../csu/libc-start.c:360
    #15 0x598aae4e3550 in _start (/usr/bin/php8.2+0x129550) (BuildId: 5228f916ded87172ddf0f3eca448f1e43874d60f)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/sp_config.c:207:20 in
```
  • Loading branch information
jvoisin committed Jun 18, 2024
1 parent b005df2 commit 358bd3d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/sp_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static zend_result sp_process_config_root(sp_parsed_keyword *parsed_rule) {
{parse_unserialize_noclass, SP_TOKEN_UNSERIALIZE_NOCLASS, &(SPCFG(unserialize_noclass))},
{parse_enable, SP_TOKEN_HARDEN_RANDOM, &(SPCFG(random).enable)},
{parse_log_media, SP_TOKEN_LOG_MEDIA, &(SPCFG(log_media))},
{parse_ulong, SP_TOKEN_LOG_MAX_LEN, &(SPCFG(log_max_len))},
{parse_uint, SP_TOKEN_LOG_MAX_LEN, &(SPCFG(log_max_len))},
{parse_disabled_functions, SP_TOKEN_DISABLE_FUNC, NULL},
{parse_readonly_exec, SP_TOKEN_READONLY_EXEC, &(SPCFG(readonly_exec))},
{parse_enable, SP_TOKEN_GLOBAL_STRICT, &(SPCFG(global_strict).enable)},
Expand Down Expand Up @@ -198,13 +198,13 @@ SP_PARSEKW_FN(parse_int) {
return ret;
}

SP_PARSEKW_FN(parse_ulong) {
SP_PARSEKW_FN(parse_uint) {
int ret = SP_PARSER_SUCCESS;
SP_PARSE_ARG(value);

char *endptr;
errno = 0;
*(u_long*)retval = (u_long)strtoul(ZSTR_VAL(value), &endptr, 10);
*(u_int*)retval = (u_int)strtoul(ZSTR_VAL(value), &endptr, 10);
if (errno != 0 || !endptr || endptr == ZSTR_VAL(value)) {
sp_log_err("config", "Failed to parse arg '%s' of `%s` on line %zu", ZSTR_VAL(value), token, kw->lineno);
ret = SP_PARSER_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion src/sp_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ SP_PARSEKW_FN(parse_str);
SP_PARSEKW_FN(parse_regexp);
SP_PARSEKW_FN(parse_empty);
SP_PARSEKW_FN(parse_int);
SP_PARSEKW_FN(parse_ulong);
SP_PARSEKW_FN(parse_uint);
SP_PARSEKW_FN(parse_php_type);
SP_PARSEKW_FN(parse_cidr);
SP_PARSEKW_FN(parse_list);
Expand Down
10 changes: 5 additions & 5 deletions src/sp_config_keywords.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ SP_PARSE_FN(parse_session) {
{parse_empty, SP_TOKEN_ENCRYPT, &(cfg->encrypt)},
{parse_empty, SP_TOKEN_SIMULATION, &(cfg->simulation)},
{parse_empty, SP_TOKEN_SIM, &(cfg->simulation)},
{parse_ulong, SP_TOKEN_SID_MIN_LENGTH, &(cfg->sid_min_length)},
{parse_ulong, SP_TOKEN_SID_MAX_LENGTH, &(cfg->sid_max_length)},
{parse_uint, SP_TOKEN_SID_MIN_LENGTH, &(cfg->sid_min_length)},
{parse_uint, SP_TOKEN_SID_MAX_LENGTH, &(cfg->sid_max_length)},
{0, 0, 0}};

SP_PROCESS_CONFIG_KEYWORDS_ERR();
Expand Down Expand Up @@ -144,8 +144,8 @@ SP_PARSE_FN(parse_global) {
{parse_str, SP_TOKEN_ENCRYPTION_KEY, &(SPCFG(encryption_key))},
{parse_str, SP_TOKEN_ENV_VAR, &(SPCFG(cookies_env_var))},
{parse_log_media, SP_TOKEN_LOG_MEDIA, &(SPCFG(log_media))},
{parse_ulong, SP_TOKEN_LOG_MAX_LEN, &(SPCFG(log_max_len))},
{parse_ulong, SP_TOKEN_MAX_EXECUTION_DEPTH, &(SPCFG(max_execution_depth))},
{parse_uint, SP_TOKEN_LOG_MAX_LEN, &(SPCFG(log_max_len))},
{parse_uint, SP_TOKEN_MAX_EXECUTION_DEPTH, &(SPCFG(max_execution_depth))},
{parse_enable, SP_TOKEN_SERVER_ENCODE, &(SPCFG(server_encode))},
{parse_enable, SP_TOKEN_SERVER_STRIP, &(SPCFG(server_strip))},
{parse_enable, SP_TOKEN_SHOW_OLD_PHP_WARNING, &(SPCFG(show_old_php_warning))},
Expand Down Expand Up @@ -333,7 +333,7 @@ SP_PARSE_FN(parse_disabled_functions) {
{parse_php_type, SP_TOKEN_RET_TYPE, &(df->ret_type)},
{parse_str, SP_TOKEN_LOCAL_VAR, &(var)},
{parse_int, SP_TOKEN_VALUE_ARG_POS, &(df->pos)},
{parse_ulong, SP_TOKEN_LINE_NUMBER, &(df->line)},
{parse_uint, SP_TOKEN_LINE_NUMBER, &(df->line)},
{0, 0, 0}};

SP_PROCESS_CONFIG_KEYWORDS(goto out);
Expand Down

0 comments on commit 358bd3d

Please sign in to comment.