Skip to content

check packet size by static_assert#2830

Merged
salix5 merged 1 commit into
Fluorohydride:masterfrom
salix5:patch-assert
Jun 8, 2025
Merged

check packet size by static_assert#2830
salix5 merged 1 commit into
Fluorohydride:masterfrom
salix5:patch-assert

Conversation

@salix5

@salix5 salix5 commented Jun 8, 2025

Copy link
Copy Markdown
Collaborator

No description provided.

@purerosefallen

Copy link
Copy Markdown
Collaborator

What would happen if it becomes too large?
Could you offer me an example of bad code, and how it compiles?

@salix5 salix5 requested a review from Copilot June 8, 2025 10:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces the runtime packet-size guard in two send functions with a compile-time assertion to enforce size limits.

  • Replace if (sizeof(ST) > MAX_DATA_SIZE) return; checks with static_assert in both SendPacketToPlayer and SendPacketToServer.
  • Enforce packet payload size at compile time rather than silently dropping oversized packets.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
gframe/netserver.h Swapped runtime check for packet size with a compile-time static_assert.
gframe/duelclient.h Swapped runtime check for packet size with a compile-time static_assert.
Comments suppressed due to low confidence (2)

gframe/netserver.h:46

  • Add a brief comment above this static_assert to explain that it replaces the old runtime guard to enforce packet-size limits at compile time and avoid silent drops.
static_assert(sizeof(ST) <= MAX_DATA_SIZE, "Packet size is too large.");

gframe/duelclient.h:58

  • Add a brief comment above this static_assert to explain that it replaces the old runtime guard to enforce packet-size limits at compile time and avoid silent drops.
static_assert(sizeof(ST) <= MAX_DATA_SIZE, "Packet size is too large.");

Comment thread gframe/netserver.h
Comment thread gframe/duelclient.h
@salix5

salix5 commented Jun 8, 2025

Copy link
Copy Markdown
Collaborator Author

What would happen if it becomes too large? Could you offer me an example of bad code, and how it compiles?

You will get an error like:

static assertion failed with "Packet size is too large."

So you cannot compile.
You can see this error if you change the condition to:

static_assert(sizeof(ST) > MAX_DATA_SIZE, "Packet size is too large.");

MAX_DATA_SIZE is 65534 now.
If a struct is bigger than this size, it should not be compiled.

@salix5 salix5 merged commit e715bd0 into Fluorohydride:master Jun 8, 2025
8 checks passed
@salix5 salix5 deleted the patch-assert branch June 8, 2025 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants