Skip to content

Document custom mapping callback consistency requirements - #381

Open
i1rr wants to merge 1 commit into
JuliaStrings:masterfrom
i1rr:super-hero/utf8proc-callback-contract
Open

i1rr wants to merge 1 commit into
JuliaStrings:masterfrom
i1rr:super-hero/utf8proc-callback-contract

Conversation

@i1rr

@i1rr i1rr commented Nov 24, 2026

Copy link
Copy Markdown

utf8proc_map_custom() can invoke a callback repeatedly, so a callback that consumes positional state can produce an unexpected result. Document that replacements must be independent of call count/order, while allowing memoization that preserves the mapping; apply context-dependent transformations separately before mapping.

This clarifies the contract described in #249. The overflow guard from #360 is already merged; this change only updates the public-header comment.

Verification on Linux with GCC 13.3.0 and GNU Make 4.3:

  • make libutf8proc.a test/custom && ./test/custom passes, including the existing inconsistent-callback regression.
  • Complete-header patch apply/reverse/reapply passes with zero fuzz; the original and documented headers produce identical library objects.
  • Local public-API comparisons pass for 40 memoized/stateless combinations and two separate positional transformations. Full make check, data regeneration and Doxygen rendering were not run (download/Julia/Doxygen requirements unavailable).

The reported positional assumption can be reproduced independently with this repro.c:

#include "utf8proc.h"
#include <stdio.h>
#include <string.h>
static utf8proc_int32_t first_only(utf8proc_int32_t cp, void *data) {
    int *first = (int *)data;
    utf8proc_int32_t result = *first ? 'A' : cp;
    *first = 0;
    return result;
}
int main(void) {
    int first = 1;
    utf8proc_uint8_t *out = NULL;
    utf8proc_ssize_t n = utf8proc_map_custom((const utf8proc_uint8_t *)"bb",
        0, &out, UTF8PROC_NULLTERM, first_only, &first);
    if (n < 0) return 2;
    printf("wanted Ab, got %s\n", out);
    int matches = n == 2 && memcmp(out, "Ab", 3) == 0;
    utf8proc_free(out);
    return matches ? 0 : 1;
}

cc -std=c99 -I. repro.c libutf8proc.a -o repro && ./repro prints wanted Ab, got bb and exits 1 on the pinned source, before and after this documentation change.

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.

1 participant