Skip to content

strlcpy_is_not_posix truncates valid data and causes crash in mlx_xpm_to_image #78

@luna7111

Description

@luna7111

The function strlcpy_is_not_posix receives a size argument that comes from strlen(), which does not include the null terminator ('\0') in its count:

strlcpy_is_not_posix(copy, str, len2);

unsigned int strlcpy_is_not_posix(char *dest, char *src, unsigned int size)

However, strlcpy_is_not_posix then copies up to size - 1 characters, assuming room is needed for the null terminator. Since size already equals the actual length of the string (excluding \0), subtracting 1 causes the last character of the string to be omitted — resulting in malformed XPM lines:
while (src[i] != '\0' && i < (size - 1))

This leads to a crash when calling mlx_xpm_to_image.

I'm guessing this happens to remove the '\n' characters of every line, but well-formed XPM data should not have those characters to begin with.
Removing the "- 1" solves the problem, but if stripping a potential newline is intentional, manually checking and replacing a final \n with \0 only if it exists could do it.

Other solution would be just using the original strlcpy.

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