Repository: netty/netty
File: codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameReader.java (Java)

Code before:
                // we will be expecting to read a new frame header.
                readingHeaders = true;
                verifyHeaderState();
                processPayloadState(ctx, framePayload, listener);
            } while (input.isReadable());
        } catch (Http2Exception e) {
            readError = !Http2Exception.isStreamError(e);
            throw e;
        } catch (RuntimeException e) {
            readError = true;
            throw e;
        } catch (Throwable cause) {
            readError = true;
            PlatformDependent.throwException(cause);
        }
    }

    private boolean preProcessFrame(ByteBuf in) throws Http2Exception {
        // Start pre-processing the frame by reading the necessary data
        // in common between all frame types
        if (in.readableBytes() < FRAME_HEADER_LENGTH) {
            // Wait until the entire framing section has been read.
            return false;
        }
        payloadLength = in.readUnsignedMedium();
        frameType = in.readByte();
        flags = new Http2Flags(in.readUnsignedByte());
        streamId = readUnsignedInt(in);
        if (payloadLength > maxFrameSize) {
            throw connectionError(FRAME_SIZE_ERROR, "Frame length: %d exceeds maximum: %d", payloadLength,
                                  maxFrameSize);
        }
        readingHeaders = false;
        return true;
    }

    private void verifyHeaderState() throws Http2Exception {
        switch (frameType) {
            case DATA:
                verifyDataFrame();
                break;
            case HEADERS:
                verifyHeadersFrame();
                break;
            case PRIORITY:
                verifyPriorityFrame();
                break;
            case RST_STREAM:
                verifyRstStreamFrame();
                break;
            case SETTINGS:

Review comment (suggestion):
Consider adding a comment for future devs why we read the full header before throwing an excepting on case `payloadLength > maxFrameSize`

Code after:
                // we will be expecting to read a new frame header.
                readingHeaders = true;
                verifyFrameState();
                processPayloadState(ctx, framePayload, listener);
            } while (input.isReadable());
        } catch (Http2Exception e) {
            readError = !Http2Exception.isStreamError(e);
            throw e;
        } catch (RuntimeException e) {
            readError = true;
            throw e;
        } catch (Throwable cause) {
            readError = true;
            PlatformDependent.throwException(cause);
        }
    }

    private boolean preProcessFrame(ByteBuf in) throws Http2Exception {
        // Start pre-processing the frame by reading the necessary data
        // in common between all frame types
        if (in.readableBytes() < FRAME_HEADER_LENGTH) {
            // Wait until the entire framing section has been read.
            return false;
        }
        payloadLength = in.readUnsignedMedium();
        if (payloadLength > maxFrameSize) {
            throw connectionError(FRAME_SIZE_ERROR, "Frame length: %d exceeds maximum: %d", payloadLength,
                                  maxFrameSize);
        }
        frameType = in.readByte();
        flags = new Http2Flags(in.readUnsignedByte());
        streamId = readUnsignedInt(in);
        readingHeaders = false;
        return true;
    }

    private void verifyFrameState() throws Http2Exception {
        switch (frameType) {
            case DATA:
                verifyDataFrame();
                break;
            case HEADERS:
                verifyHeadersFrame();
                break;
            case PRIORITY:
                verifyPriorityFrame();
                break;
            case RST_STREAM:
                verifyRstStreamFrame();
                break;
            case SETTINGS: