Skip to content

Fix TypeError with trim on undefined logs in WebGLProgram #30400

@Barmadmim

Description

@Barmadmim

Description

When gl.getProgramInfoLog() is called in WebGLProgram, the value may be undefined, causing an error when trying to .trim() on it.

Solution proposal:
A check can be added before doing .trim() to ensure that it is not trying to apply on an undefined value.

const programLog = gl.getProgramInfoLog(program);
const vertexLog = gl.getShaderInfoLog(glVertexShader);
const fragmentLog = gl.getShaderInfoLog(glFragmentShader);

// Check if the logs are valid before calling trim
const safeProgramLog = programLog? programLog.trim() : '';
const safeVertexLog = vertexLog? vertexLog.trim() : '';
const safeFragmentLog = fragmentLog? fragmentLog.trim() : '';

### Reproduction steps

1. Install Three.js in a project using yarn.
2. Include the three.cjs file from node_modules/three/build/three.cjs in your project.
3. Create a basic WebGL program that compiles shaders and links them into a program.
4. Call gl.getProgramInfoLog() on the WebGL program.
5. Call gl.getShaderInfoLog() on the vertex and fragment shaders.
6. Attempt to call .trim() on the result of the logs returned by the previous calls.
7. If the logs are undefined, the TypeError: Cannot read property 'trim' of undefined will occur.

### Code

node_modules/three/build/three.cjs
// Sample code to reproduce the error
const gl = canvas.getContext('webgl');
const vertexShaderSource = '...'; // Your vertex shader code
const fragmentShaderSource = '...'; // Your fragment shader code

// Create shaders and program
const glVertexShader = gl.createShader(gl.VERTEX_SHADER);
const glFragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(glVertexShader, vertexShaderSource);
gl.shaderSource(glFragmentShader, fragmentShaderSource);
gl.compileShader(glVertexShader);
gl.compileShader(glFragmentShader);

const program = gl.createProgram();
gl.attachShader(program, glVertexShader);
gl.attachShader(program, glFragmentShader);
gl.linkProgram(program);

// Attempting to get logs and trim (causes error)
const programLog = gl.getProgramInfoLog(program);
const vertexLog = gl.getShaderInfoLog(glVertexShader);
const fragmentLog = gl.getShaderInfoLog(glFragmentShader);

// Error occurs if any log is undefined and trim is called
const safeProgramLog = programLog ? programLog.trim() : '';
const safeVertexLog = vertexLog ? vertexLog.trim() : '';
const safeFragmentLog = fragmentLog ? fragmentLog.trim() : '';

Live example

none

Screenshots

Image

Version

18.3.1

Device

Mobile

Browser

No response

OS

Android

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions