Skip to content

[rlgl] Undefined behavior in rlLoadTexture when computing applying a zero offset to a null pointer #3246

@ashn-dot-dev

Description

@ashn-dot-dev

Please, before submitting a new issue verify and check:

  • I tested it on latest raylib version from master branch
  • I checked there is no similar issue already reported
  • I checked the documentation on the wiki
  • My code has no errors or misuse of raylib
    • Can be replicated using an in-tree example program.

Issue description

The function rlLoadTexture contains the call glTexImage2D(GL_TEXTURE_2D, i, glInternalFormat, mipWidth, mipHeight, 0, glFormat, glType, (unsigned char *)data + mipOffset) here. When data is NULL, the expression (unsigned char *)data + mipOffset will resolve to NULL + 0 in the first iteration of the mipmap load loop.

Environment

Discovered on commit a86c93e using a PLATFORM_DESKTOP build for Linux via Windows Subsystem for Linux with CUSTOM_CFLAGS='-fsanitize=address,undefined'.

OpenGL information:

INFO: GL: OpenGL device information:
INFO:     > Vendor:   Microsoft Corporation
INFO:     > Renderer: D3D12 (NVIDIA GeForce RTX 3070 Ti)
INFO:     > Version:  4.2 (Core Profile) Mesa 23.0.4-0ubuntu1~22.04.1
INFO:     > GLSL:     4.20

Issue Screenshot

N/A

Code Example

The UBSan error will trigger on the in-tree example examples/shapes/shapes_top_down_lights.c on commit a86c93e without modification.

$ (cd /path/to/raylib/src/ && make clean all CC=clang CUSTOM_CFLAGS='-fsanitize=address,undefined' && sudo make install)
$ (cd /path/to/raylib/examples/ && make clean shapes/shapes_top_down_lights)
$ /path/to/raylib/examples/shapes/shapes_top_down_lights 2>&1 | grep 'runtime error'
rlgl.h:3014:175: runtime error: applying zero offset to null pointer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions