capstone-engine / capstone

Capstone disassembly/disassembler framework for ARM, ARM64 (ARMv8), Alpha, BPF, Ethereum VM, HPPA, LoongArch, M68K, M680X, Mips, MOS65XX, PPC, RISC-V(rv32G/rv64G), SH, Sparc, SystemZ, TMS320C64X, TriCore, Webassembly, XCore and X86.
http://www.capstone-engine.org
7.62k stars 1.56k forks source link

Usage of non-portable strndup in cstest #2517

Open thestr4ng3r opened 1 month ago

thestr4ng3r commented 1 month ago

Work environment

Questions Answers
OS/arch/bits Mac OS X 10.5
Architecture powerpc
Source of Capstone Source with -DCAPSTONE_BUILD_CSTEST=ON
Version/git commit next, 5bd05e34245eeca7833d30b677131cab6ea7a6fa

Expected behavior

cstest (-DCAPSTONE_BUILD_CSTEST=ON) compiles

Actual behavior

[98/175] Building C object suite/cstest/CMakeFiles/libcstest.dir/src/helper.c.o
FAILED: suite/cstest/CMakeFiles/libcstest.dir/src/helper.c.o
/opt/local/bin/gcc-mp-7 -DCAPSTONE_AARCH64_SUPPORT -DCAPSTONE_ALPHA_SUPPORT -DCAPSTONE_ARM_SUPPORT -DCAPSTONE_BPF_SUPPORT -DCAPSTONE_DEBUG -DCAPSTONE_EVM_SUPPORT -DCAPSTONE_HAS_AARCH64 -DCAPSTONE_HAS_ALPHA -DCAPSTONE_HAS_ARM -DCAPSTONE_HAS_BPF -DCAPSTONE_HAS_EVM -DCAPSTONE_HAS_HPPA -DCAPSTONE_HAS_LOONGARCH -DCAPSTONE_HAS_M680X -DCAPSTONE_HAS_M68K -DCAPSTONE_HAS_MIPS -DCAPSTONE_HAS_MOS65XX -DCAPSTONE_HAS_POWERPC -DCAPSTONE_HAS_RISCV -DCAPSTONE_HAS_SH -DCAPSTONE_HAS_SPARC -DCAPSTONE_HAS_SYSTEMZ -DCAPSTONE_HAS_TMS320C64X -DCAPSTONE_HAS_TRICORE -DCAPSTONE_HAS_WASM -DCAPSTONE_HAS_X86 -DCAPSTONE_HAS_XCORE -DCAPSTONE_HAS_XTENSA -DCAPSTONE_HPPA_SUPPORT -DCAPSTONE_LOONGARCH_SUPPORT -DCAPSTONE_M680X_SUPPORT -DCAPSTONE_M68K_SUPPORT -DCAPSTONE_MIPS_SUPPORT -DCAPSTONE_MOS65XX_SUPPORT -DCAPSTONE_PPC_SUPPORT -DCAPSTONE_RISCV_SUPPORT -DCAPSTONE_SH_SUPPORT -DCAPSTONE_SPARC_SUPPORT -DCAPSTONE_SYSTEMZ_SUPPORT -DCAPSTONE_TMS320C64X_SUPPORT -DCAPSTONE_TRICORE_SUPPORT -DCAPSTONE_USE_SYS_DYN_MEM -DCAPSTONE_WASM_SUPPORT -DCAPSTONE_X86_SUPPORT -DCAPSTONE_XCORE_SUPPORT -DCAPSTONE_XTENSA_SUPPORT -I"/Users/florian/dev/capstone/include>" -I/Users/florian/dev/capstone/suite/cstest/include -I/Users/florian/dev/capstone/build/suite/cstest/extern/src/cmocka_ext/include -I/Users/florian/dev/capstone/build/suite/cstest/extern/src/libcyaml_ext/include -I/Users/florian/dev/capstone/include -g -Werror -Wall -Warray-bounds -Wshift-negative-value -Wreturn-type -Wformat -Wmissing-braces -Wunused-function -Wunused-variable -Wparentheses -Wint-in-bool-context -Wmisleading-indentation -Wno-maybe-uninitialized -Wshadow=local -MD -MT suite/cstest/CMakeFiles/libcstest.dir/src/helper.c.o -MF suite/cstest/CMakeFiles/libcstest.dir/src/helper.c.o.d -o suite/cstest/CMakeFiles/libcstest.dir/src/helper.c.o -c /Users/florian/dev/capstone/suite/cstest/src/helper.c
/Users/florian/dev/capstone/suite/cstest/src/helper.c: In function 'replace_hex':
/Users/florian/dev/capstone/suite/cstest/src/helper.c:55:13: error: implicit declaration of function 'strndup' [-Werror=implicit-function-declaration]
   tmp_tmp = strndup(tmp, orig_found - tmp);
             ^~~~~~~
/Users/florian/dev/capstone/suite/cstest/src/helper.c:55:13: error: incompatible implicit declaration of built-in function 'strndup' [-Werror]
/Users/florian/dev/capstone/suite/cstest/src/helper.c: In function 'replace_negative':
/Users/florian/dev/capstone/suite/cstest/src/helper.c:120:13: error: incompatible implicit declaration of built-in function 'strndup' [-Werror]
   tmp_tmp = strndup(tmp, orig_found - tmp);
             ^~~~~~~
cc1: all warnings being treated as errors
[100/175] Linking C static library libcapstone.a
ninja: build stopped: subcommand failed.

Steps to reproduce the behavior

Compile with cmake and cstest enabled (-DCAPSTONE_BUILD_CSTEST=ON) on a platform that does not have strndup.

Additional Logs, screenshots, source code, configuration dump, ...

There are two usages of strndup which is specified by POSIX, but not C standard, and thus not widely available, e.g. on Mac OS X 10.5 with gcc 7. On this platform, https://github.com/capstone-engine/capstone/pull/2508 is also necessary for cstest.

Rot127 commented 1 month ago

Note please, people who find this one and need to test, please use cstest_py. The C and Python code consume the exact same test cases.

pip install ./bindings/python
pip install ./bindings/python/cstest_py
cstest_py tests/

Note, cstest is not deprecated. I just didn't yet implemented it for other platforms then Linux yet, because if time constraints.