Skip to content

Vector element index out-of-bounds not leading to compile error #3693

Open

Description

@mstrgram

It is currently possible to index vector elements out-of-bounds without the compiler issuing and error:

// Load index buffer
uint indexOffset = PrimitiveIndex() * 3 /* indices per primitive */ * 4 /* 4 bytes per index*/;
const uint3 indices = g_indices.Load3(indexOffset);

// Retrieve corresponding vertex normals for the triangle vertices.
float3 vertexNormals[3] = { g_vertices[indices[0]].normal, g_vertices[indices[1]].normal, g_vertices[indices[3]].normal };

// Compute the triangle's interpolated normal
float3 triangleNormal = HitAttribute(vertexNormals, attr);

Notice the indices[3] above is out-of-bounds.

Above can be compiled with this command line (shader attached to bug)

dxc.exe /T lib_6_6 /Zpr /all_resources_bound /Zi /Od /Vn"g_DefaultRT_DXIL_Lib" /Fh"DefaultRT.dxil-lib.h" /nologo DefaultRT.hlsl

Shader repro attached: DefaultRT.zip

Activity

  1. added
    bug
    diagnostic
    and removed
    needs-triage
    on Apr 18, 2024
  2. damyanp commented on Sep 4, 2026

    @damyanp
    Member

    Ported from a note on Internal bug 32635218, dated 2022-07-13: FXC diagnoses this out-of-bounds vector access, while DXC accepts it.

    The internal note included this smaller standalone repro:

    ByteAddressBuffer g_indices;
    
    struct Vert {
      float3 normal;
    };
    
    Buffer<Vert> g_vertices;
    
    float4 main(uint ix : IX) : SV_Target {
      uint indexOffset = ix * 3 * 4;
      const uint3 indices = g_indices.Load3(indexOffset);
      float3 vertexNormals[3] = {
        g_vertices[indices[0]].normal,
        g_vertices[indices[1]].normal,
        g_vertices[indices[3]].normal
      };
      float3 triangleNormal = vertexNormals[0] * vertexNormals[1] * vertexNormals[2];
      return 0.0;
    }

    The relevant expression is indices[3], which is out of bounds for uint3.

    Assisted by GitHub Copilot.

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

    bugdiagnostic

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions