From 083b37734c8cdfa285dcf2c665e172244dc23313 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 23 Nov 2021 07:07:41 +0100 Subject: BUGFIX: c_default_hash32() was buggy in ccommon.h - fixed. --- examples/option_mkdir.c | 54 ++++++++++++++++++++++++------------------------- include/stc/ccommon.h | 10 ++++----- include/stc/template.h | 6 ++++++ 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/examples/option_mkdir.c b/examples/option_mkdir.c index e28c09e9..b9f4ddea 100644 --- a/examples/option_mkdir.c +++ b/examples/option_mkdir.c @@ -14,13 +14,13 @@ int parseArgs(int argc, char *argv[], struct AppArgs *args) { const char *shortopts = "pvm:Z"; coption_long longopts[] = { - {"mode", coption_required_argument, 'm'}, - {"parent", coption_no_argument, 'p'}, - {"verbose", coption_no_argument, 'v'}, - {"context", coption_optional_argument, 'Z'}, - {"help", coption_no_argument, 'H'}, - {"version", coption_no_argument, 'V'}, - {NULL} + {"mode", coption_required_argument, 'm'}, + {"parent", coption_no_argument, 'p'}, + {"verbose", coption_no_argument, 'v'}, + {"context", coption_optional_argument, 'Z'}, + {"help", coption_no_argument, 'H'}, + {"version", coption_no_argument, 'V'}, + {NULL} }; *args = (struct AppArgs){NULL}; coption opt = coption_init(); @@ -32,28 +32,28 @@ int parseArgs(int argc, char *argv[], struct AppArgs *args) case 'v': args->verbose = true; break; case 'Z': args->context = opt.arg? opt.arg : "DEFAULT"; break; case 'H': - printf( "Usage: mkdir [OPTION]... DIRECTORY...\n" - "Create the DIRECTORY(ies), if they do not already exist.\n\n" - "Mandatory arguments to long options are mandatory for short options too.\n" - " -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask\n" - " -p, --parents no error if existing, make parent directories as needed\n" - " -v, --verbose print a message for each created directory\n" - " -Z set SELinux security context of each created directory\n" - " to the default type\n" - " --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n" - " or SMACK security context to CTX\n" - " --help display this help and exit\n" - " --version output version information and exit\n\n" - "GNU coreutils online help: \n" - "Full documentation at: \n" - "or available locally via: info '(coreutils) mkdir invocation'\n" ); + printf("Usage: mkdir [OPTION]... DIRECTORY...\n" + "Create the DIRECTORY(ies), if they do not already exist.\n\n" + "Mandatory arguments to long options are mandatory for short options too.\n" + " -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask\n" + " -p, --parents no error if existing, make parent directories as needed\n" + " -v, --verbose print a message for each created directory\n" + " -Z set SELinux security context of each created directory\n" + " to the default type\n" + " --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n" + " or SMACK security context to CTX\n" + " --help display this help and exit\n" + " --version output version information and exit\n\n" + "GNU coreutils online help: \n" + "Full documentation at: \n" + "or available locally via: info '(coreutils) mkdir invocation'\n"); return 1; case 'V': - printf( "mkdir (GNU coreutils) 8.30\n" - "Copyright (C) 2018 Free Software Foundation, Inc.\n" - "License GPLv3+: GNU GPL version 3 or later .\n" - "This is free software: you are free to change and redistribute it.\n" - "There is NO WARRANTY, to the extent permitted by law.\n" ); + printf("mkdir (GNU coreutils) 8.30\n" + "Copyright (C) 2018 Free Software Foundation, Inc.\n" + "License GPLv3+: GNU GPL version 3 or later .\n" + "This is free software: you are free to change and redistribute it.\n" + "There is NO WARRANTY, to the extent permitted by law.\n"); return 1; case ':': printf("mkdir: option '%s' requires an argument\n" diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 0680dc2c..f71660b8 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -123,12 +123,12 @@ STC_INLINE uint64_t c_strhash(const char *s) { if (h) while ((c = *s++)) h = (h << 10) - h + c; return _c_rotl(h, 26) ^ h; } -// len = 2,4,6,...: +// len >= 1 STC_INLINE uint64_t c_default_hash(const void* key, size_t len) { - const uint16_t *x = (const uint16_t*) key; - uint64_t h = *x++; h += h << 14; - while ((len -= 2)) h = (h << 10) - h + *x++; - return _c_rotl(h, 24) ^ h; + const uint8_t *x = (const uint8_t*) key; + uint64_t h = *x++; + while (--len) h = (h << 10) - h + *x++; + return _c_rotl(h, 26) ^ h; } #define c_default_hash32(data, len_is_4) \ ((*(const uint32_t*)data * 0xc6a4a7935bd1e99d) >> 15) diff --git a/include/stc/template.h b/include/stc/template.h index 28c6879a..19ff97f2 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -116,6 +116,9 @@ #define i_tag i_key #endif #if !defined i_keyfrom && defined i_keydel + #ifdef STC_DEBUG + #error i_keydel defined, but not i_keyfrom. + #endif #define i_keyfrom c_no_clone #elif !defined i_keyfrom #define i_keyfrom c_default_fromraw @@ -143,6 +146,9 @@ #define i_tag i_val #endif #if !defined i_valfrom && defined i_valdel + #ifdef STC_DEBUG + #error i_del/i_valdel defined, but not i_valfrom. + #endif #define i_valfrom c_no_clone #elif !defined i_valfrom #define i_valfrom c_default_fromraw -- cgit v1.2.3