Skip to content

dictBuilder: declare qsort_r on OpenHarmony (musl headers omit it) - #4886

Open
bfs2021 wants to merge 1 commit into
meta:devfrom
bfs2021:fix/cover-qsort-r-openharmony
Open

bfs2021 wants to merge 1 commit into
meta:devfrom
bfs2021:fix/cover-qsort-r-openharmony

Conversation

@bfs2021

@bfs2021 bfs2021 commented Nov 3, 2026

Copy link
Copy Markdown

Problem

lib/dictBuilder/cover.c fails to compile on OpenHarmony (musl libc): the OHOS libc exports qsort_r() with the GNU signature, but its headers do not declare it, so the call in stableSort() is an implicit function declaration, which newer OHOS SDK clang treats as an error. Fixes #4872.

Root cause

On OpenHarmony __linux__ is defined, so cover.c defines _GNU_SOURCE and the ZDICT_QSORT_GNU path is selected, but the OHOS musl headers omit the qsort_r() prototype. Unlike Android (which is excluded from the GNU path and falls back), there is no handling for this platform, and the C90 fallback (qsort() with a global context) would be a thread-safety regression.

Fix

Declare qsort_r() locally under #if defined(__OHOS__), right after the system includes. This is the Option B from the issue, which the reporter already validated on-device: OHOS's qsort_r is the GNU variant, so the re-entrant sort keeps being used and no behavior changes on any other platform. __OHOS__ is the macro predefined by the OHOS SDK toolchain.

Testing

  • Reproduced the failure mode locally on a host whose libc also does not declare qsort_r: gcc -c -Ilib -D__linux__ -D__OHOS__ lib/dictBuilder/cover.c fails with error: implicit declaration of function 'qsort_r' before the change and compiles cleanly after it (also with -Wall -Wextra), same as the OHOS clang error class.
  • Native build unaffected: gcc -c -Ilib lib/dictBuilder/cover.c clean before/after.
  • Built the full static lib + CLI with CMake/Ninja and ran zstd --train-cover --split=4096 (which exercises stableSort() in cover.c) plus a compress/decompress round-trip with the trained dictionary: all pass.

OpenHarmony (musl) exports qsort_r() with the GNU signature but its
headers do not declare it, so stableSort() compiles as an implicit
function declaration and fails under -Werror=implicit-function-declaration
(OHOS SDK clang 15 defaults).

Declare it locally when __OHOS__ is defined so cover.c keeps using the
re-entrant GNU variant instead of falling back to the thread-unsafe
C90 qsort() path.
@meta-cla

meta-cla Bot commented Nov 3, 2026

Copy link
Copy Markdown

Hi @bfs2021!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cover.c does not build on OpenHarmony: qsort_r is exported but not declared (musl)

1 participant