Skip to content

strlen and wcslen are bound as int, but C returns size_t #3535

Open

Description

@GreenFuze

Two C entry points return a size_t and are declared int on the Java side. The stub casts the result down, so the C compiler has nothing to warn about, and nothing anywhere compares the Java declaration with the C one.

/* bundles/org.eclipse.swt/Eclipse SWT PI/common/library/c.c:358 */
rc = (jint)strlen((char *)arg0);
/* string.h */  size_t strlen(const char *);
/* bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/os.c:9855 */
rc = (jint)wcslen((const wchar_t *)arg0);
/* wchar.h */  size_t wcslen(const wchar_t *);

The Java declarations:

/* Eclipse SWT PI/common/org/eclipse/swt/internal/C.java:144      */  public static final native int strlen (long s);
/* Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java:4554 */  public static final native int wcslen (long string);

What it can cause

On LP64 size_t is 64-bit and jint is 32-bit, so the top 32 bits are dropped unconditionally. For a string of 2 GiB or more the returned length is wrong — and wrong in the worst way, because the cast makes it a plausible small number rather than an obvious failure.

Every call site uses the value directly as a length:

/* Eclipse SWT/gtk/org/eclipse/swt/graphics/Font.java:189      */  int length = C.strlen(family);
/* Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java:3424 */  int length = OS.wcslen (sharedData);

I have not observed a failure and nothing was built or run. Whether a string that long reaches these calls is a question about SWT's own use, not about the declarations.

Suggested fix

SWT already models size_t as Java long everywhere else in the same file. C.java declares eighteen memmove overloads and memset, and every one takes a long annotated cast=(size_t):

/* C.java:141 */  public static final native long memset (long buffer, int c, long num);   /* @param num cast=(size_t) */

strlen and wcslen are the only places size_t appears as a return, and the only two that use int. The repair would be to make them look like the rest — declare long and regenerate the stubs.

Call sites assign to int length, so each would need a cast or a wider local. TextLayout already writes one for the same kind of value:

/* Eclipse SWT/gtk/org/eclipse/swt/graphics/TextLayout.java:655 */
lineEnd = (int)OS.g_utf16_strlen(ptr, -1);

Provenance

Reported as part of ongoing research into cross-language boundaries. Every line above was checked against master on 2026-08-23. No runtime observation; nothing was built or run.

wcslen is Win32-only, where size_t is 64-bit under LLP64 as well, so the narrowing holds on both data models.

Activity

  1. HeikoKlare commented on Aug 24, 2026

    @HeikoKlare
    Contributor

    Good catch. While many data types have been updated/replaced for 64-bit architectures, it seems like these were missed (maybe because the int representation of the size_t return type was not explicitly documented in contrast to the parameter casts).

  2. GreenFuze commented on Sep 28, 2026

    @GreenFuze
    ContributorAuthor

    OS.GlobalSize is declared int in Java, but Win32's GlobalSize returns SIZE_T, which is
    64 bits on 64-bit Windows.

    /* Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java:3056 */
    public static final native int GlobalSize (long hMem);
    /* Eclipse SWT PI/win32/library/os.c:3663 */
    rc = (jint)GlobalSize((HANDLE)arg0);
    /* Windows SDK, um/WinBase.h */
    WINBASEAPI SIZE_T WINAPI GlobalSize(_In_ HGLOBAL hMem);

    The stub casts the result to jint, so the compiler has nothing to warn about, and the top 32
    bits are dropped. For a global memory block of 2 GiB or more, the size SWT gets back is wrong,
    possibly negative. All seven callers use it directly as a byte count, for example for clipboard
    and drag-and-drop data:

    /* Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/ByteArrayTransfer.java:208 */
    int size = OS.GlobalSize(hMem);

    The fix: declare it long in OS.java and cast to (jlong) in os.c. It is the same fix this
    issue needs for strlen and wcslen, so I'm happy to send one PR for all three.

    No failure was observed; nothing was built or run.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions