Skip to content

fix strncpy truncation; enable FORTIFY_SOURCE=3#1218

Open
xq9mend wants to merge 2 commits into
sonic-net:masterfrom
xq9mend:fix/gcc-fortify-source-3
Open

fix strncpy truncation; enable FORTIFY_SOURCE=3#1218
xq9mend wants to merge 2 commits into
sonic-net:masterfrom
xq9mend:fix/gcc-fortify-source-3

Conversation

@xq9mend

@xq9mend xq9mend commented Jul 1, 2026

Copy link
Copy Markdown

Why I did it

zmq_msg_init_size() allocates a byte buffer sized exactly s.size() — no room for a null terminator. The ZMQ message reader (zmsg_to_map) constructs a std::string using zmq_msg_size(), so null termination is irrelevant. strncpy is the wrong primitive here; memcpy is semantically correct for raw byte blobs and eliminates the FORTIFY_SOURCE=3 provable-truncation diagnostic.

Also upgrade FORTIFY_SOURCE to level 3 in debian/rules so all future builds use the stronger compile-time check. dpkg sets =2 by default; the -U flag clears it before setting =3.

How I did it

  • common/events_common.h: replace strncpy with memcpy in map_to_zmsg()
  • debian/rules: add DEB_CPPFLAGS_MAINT_APPEND = -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3

How to verify it

Build passes cleanly with dpkg-buildpackage. No compiler warnings or errors related to FORTIFY_SOURCE.

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@qiluo-msft

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

Hi, there are workflow run(s) waiting for approval, you may be first-time contributor. I will notify maintainers to help approve once PR is approved. Thanks!

---Powered by SONiC BuildBot

@xq9mend
xq9mend force-pushed the fix/gcc-fortify-source-3 branch from 68eeb55 to 76d9d7d Compare July 4, 2026 09:45
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

zmq message buffers are length-prefixed byte blobs — the reader uses
zmq_msg_size(), not null termination. strncpy stops at the first null
byte in the serialized boost archive, corrupting messages that contain
embedded nulls. memcpy copies exactly s.size() bytes unconditionally,
which is the semantically correct call for raw byte blobs.

Also silences the FORTIFY_SOURCE=3 provable-truncation diagnostic on
this call site.

Signed-off-by: xq9mend <xq9mend@users.noreply.github.com>
@xq9mend
xq9mend force-pushed the fix/gcc-fortify-source-3 branch from 2389b01 to 336c0b8 Compare July 5, 2026 12:24
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: xq9mend <xq9mend@users.noreply.github.com>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

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