Skip to content

libopendkim: fix strlcpy destination size in dkim_diffheaders()#291

Open
thegushi wants to merge 1 commit into
trusteddomainproject:developfrom
thegushi:fix-zdecode-strlcpy
Open

libopendkim: fix strlcpy destination size in dkim_diffheaders()#291
thegushi wants to merge 1 commit into
trusteddomainproject:developfrom
thegushi:fix-zdecode-strlcpy

Conversation

@thegushi
Copy link
Copy Markdown
Collaborator

Fixes #86.

The strlcpy call in dkim_diffheaders() was passing strlen(z) (the source length) as the size argument instead of the destination buffer size. strlcpy expects the size of the destination, so using the source length would silently truncate the copy by one character and is what triggers the compiler warning.

The buffer is allocated with MAXHEADERS (32768 bytes), so the correct size argument is MAXHEADERS.

Also fixed the adjacent allocation error message which was reporting strlen(z) bytes when MAXHEADERS bytes are actually being allocated.

Neither the original code nor the proposed patch in the issue was correct:

  • Original (strlen(z)): copies at most strlen(z) - 1 characters, truncating by one
  • Proposed (strlen(dkim->dkim_zdecode)): on freshly-allocated memory this is strlen("") = 0, copying nothing
  • Correct: MAXHEADERS, matching the DKIM_MALLOC(dkim, MAXHEADERS) on the line above

Pass MAXHEADERS (the actual allocation size of dkim_zdecode) as the
strlcpy size argument instead of strlen(z) (source size). Using the
source length silently truncates the copy by one character and suppresses
the compiler warning incorrectly. Also fix the adjacent error message
which reported strlen(z) bytes when MAXHEADERS bytes are actually
allocated.

Fixes trusteddomainproject#86.
@thegushi thegushi mentioned this pull request May 24, 2026
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.

1 participant