Skip to content

THRIFT-5932: compiler/cpp: bound saferealpath() output on Windows#3357

Open
neosys007 wants to merge 2 commits intoapache:masterfrom
neosys007:codex/thrift-5932-windows-realpath
Open

THRIFT-5932: compiler/cpp: bound saferealpath() output on Windows#3357
neosys007 wants to merge 2 commits intoapache:masterfrom
neosys007:codex/thrift-5932-windows-realpath

Conversation

@neosys007
Copy link
Copy Markdown
Contributor

@neosys007 neosys007 commented Mar 21, 2026

This PR makes the Windows realpath fallback in the Thrift compiler size-aware.

Current head still has a Windows fallback that copies the resolved path back into resolved_path with strcpy(). That is safe only when the caller buffer is large enough, but saferealpath() currently has no way to verify that. The compiler call sites all use fixed THRIFT_PATH_MAX buffers, so the fallback needs to reject overlong paths before copying them back.

The fix changes saferealpath() to take the destination capacity explicitly and return nullptr when the path does not fit. The existing compiler call sites were updated to pass THRIFT_PATH_MAX.

This keeps the change small, makes the contract explicit, and avoids hiding the overflow behind the Windows-specific fallback.

Validation performed locally:

  • git diff --check
  • syntax-only compile for compiler/cpp/src/thrift/main.cc
  • the updated main.cc compiles cleanly in syntax-only mode after the signature update.

Related Jira:

@mergeable mergeable bot added the compiler label Mar 21, 2026
if (source_len >= resolved_path_size) {
return nullptr;
}
strcpy(resolved_path, source);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or maybe strcnpy ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would prefer not to truncate here. This path now rejects inputs that do not fit in the destination buffer, so strcpy() matches the intended semantics better than strncpy(): we want a full string copy after the explicit length check, not truncation or zero-padding.

The Windows fallback in saferealpath() still copies either the original path or the GetFullPathNameA() result into resolved_path with strcpy(), but the helper does not know the caller buffer size.

Make saferealpath() size-aware, reject paths that do not fit the caller buffer, and keep the existing call sites using their fixed THRIFT_PATH_MAX storage.

Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
@neosys007 neosys007 force-pushed the codex/thrift-5932-windows-realpath branch from b97cc5c to 72e169c Compare March 22, 2026 13:40
return nullptr;
}
memcpy(resolved_path, source, source_len + 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why memcpy() now?

Use strcpy() once the source path has been checked to fit in the\ndestination buffer. This keeps the operation expressed as a string copy,\nwhich is clearer in this code path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants