protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

stdout/stderr/stdin are allowed fields in C++ despite being reserved by the C specification

Open deeglaze opened this issue 5 years ago • 6 comments

These names are specified as #define'd symbols, and as such should not be compiled to. When a libc implements these macros as, say REENT->stdin the way newlib does, then the clash causes compilation errors rather than simply rebinding a name.

Version: v3.5.0 Language: C++

I expect these fields to be rejected by the compiler, renamed and not aliased as they currently are, or at least provide a warning that can be made into an error with an appropriate flag. I know the Google codebase couldn't handle introducing an error or removing a symbol definition right now, but it'd be nice to selectively be stricter in a proto_library_cc build rule.

deeglaze avatar Nov 29 '21 20:11 deeglaze

Do you have a test case? Might be just my limited C++ knowledge here, but it's not obvious to me what exactly you're suggesting.

elharo avatar Nov 29 '21 23:11 elharo

test.proto:

package test;
message Test {
  string stdout;
}

test.cc

#include <stdio.h>

#include "test.proto.h"

int main() {
  test::Test t;
  t.set_stdout("awkward...");
  fprintf(stdout, "oh, uh, %s\n", t.stdout().c_str());
  return 0;
}

When you build this in a toolchain whose libc has the (specified as implementation-defined macro) stdout symbol #defined as something that doesn't work as valid syntax for string test::Test::stdout() const; then we have a compilation error. One such libc is newlib, which is used in cygwin, many embedded devices, and the Asylo project I'm a part of (https://github.com/google/asylo).

Edit: here's newlib's stdio.h that has a standards-compliant stdout definition that causes compilation failure in protos that allow stdout as an identifier https://sourceware.org/git/?p=newlib-cygwin.git;a=blob_plain;f=newlib/libc/include/stdio.h;hb=HEAD

deeglaze avatar Nov 30 '21 20:11 deeglaze

I can confirm this is an issue with musl (alpine) which has #define stdout (stdout) in its stdio.h.

not-a-user avatar Jul 28 '23 08:07 not-a-user

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago.

github-actions[bot] avatar Feb 03 '24 10:02 github-actions[bot]

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

github-actions[bot] avatar Feb 18 '24 10:02 github-actions[bot]

Given the language has gone into a different versioning scheme, can we reassess if the next major release can reserve these field names?

deeglaze avatar Oct 21 '24 15:10 deeglaze