Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncommon permutation of #defines breaks compilation in Errno.cpp #62804

Closed
Gadal opened this issue May 19, 2023 · 3 comments
Closed

Uncommon permutation of #defines breaks compilation in Errno.cpp #62804

Gadal opened this issue May 19, 2023 · 3 comments

Comments

@Gadal
Copy link

Gadal commented May 19, 2023

In llvm/lib/Support/Errno.cpp, we have the following (comments removed for brevity):

#include "llvm/Support/Errno.h"
#include "llvm/Config/config.h"
#include <cstring>

#if HAVE_ERRNO_H
#include <errno.h>
#endif

namespace llvm {
namespace sys {

#if HAVE_ERRNO_H
std::string StrError() {
  return StrError(errno);
}
#endif  // HAVE_ERRNO_H

std::string StrError(int errnum) {
  std::string str;
  if (errnum == 0)
    return str;
#if defined(HAVE_STRERROR_R) || HAVE_DECL_STRERROR_S
  const int MaxErrStrLen = 2000;
  char buffer[MaxErrStrLen];
  buffer[0] = '\0';
#endif

#ifdef HAVE_STRERROR_R
#if defined(__GLIBC__) && defined(_GNU_SOURCE)
  str = strerror_r(errnum, buffer, MaxErrStrLen - 1);
#else
  strerror_r(errnum, buffer, MaxErrStrLen - 1);
  str = buffer;
#endif
#elif HAVE_DECL_STRERROR_S // "Windows Secure API"
  strerror_s(buffer, MaxErrStrLen - 1, errnum);
  str = buffer;
#elif defined(HAVE_STRERROR)
  str = strerror(errnum);
#else
  raw_string_ostream stream(str);                            // Appears to be unable to compile.
  stream << "Error #" << errnum;
  stream.flush();
#endif
  return str;
}

}  // namespace sys
}  // namespace llvm

In the event that HAVE_STRERROR_R is undefined, HAVE_DECL_STRERROR_S evaluates to false, and HAVE_STRERROR is undefined, the portion of text marked above is inserted. Compilation fails with the following error:

llvm\lib\Support\Errno.cpp(66): error C2065: 'raw_string_ostream': undeclared identifier

It appears that raw_string_ostream is not defined by Errno.h, config.h, errno.h, or any of their transitive inclusions.

L66 was added 11 years ago here, and that commit added an inclusion for llvm/Support/raw_ostream.h. That inclusion was deleted here.

Adding back that inclusion resolves the compilation error.

@Gadal
Copy link
Author

Gadal commented May 19, 2023

@serge-sans-paille It may be worth a look at fbbc41f to see if any of the other 70 deletions in that change are invalid for certain combinations of #defines.

@MaskRay
Copy link
Member

MaskRay commented May 30, 2023

In the event that HAVE_STRERROR_R is undefined, HAVE_DECL_STRERROR_S evaluates to false, and HAVE_STRERROR is undefined, the portion of text marked above is inserted. Compilation fails with the following error:

Does this scenario happen for a known supported OS?

strerror_r has great portability. It may not exist in MSVC but strerror is available there. If we cannot find a supported OS exercising this code path of the following lines, the real fix should be to remove these lines:

  raw_string_ostream stream(str);
  stream << "Error #" << errnum;
  stream.flush();

@Gadal
Copy link
Author

Gadal commented May 31, 2023

@MaskRay

Does this scenario happen for a known supported OS?

Not to my knowledge. In my case, I bumped into the set of conditions I described above due to a misconfiguration, but that was easily resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants