diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc index b0ea75e88b27..6994dd551434 100644 --- a/src/crypto/crypto_hash.cc +++ b/src/crypto/crypto_hash.cc @@ -372,6 +372,8 @@ const ncrypto::Digest GetDigest(Environment* env, const char* name) { return result.digest; #elif NCRYPTO_USE_BORINGSSL return ncrypto::Digest::FromName(name); +#else +#error "Unsupported crypto backend" #endif } diff --git a/src/crypto/crypto_x509_gen.cc b/src/crypto/crypto_x509_gen.cc index b797b2183601..b50462c4138d 100644 --- a/src/crypto/crypto_x509_gen.cc +++ b/src/crypto/crypto_x509_gen.cc @@ -122,7 +122,7 @@ X509Pointer NewCertificate() { #elif NCRYPTO_USE_BORINGSSL return X509Pointer(X509_new()); #else -#error "Unsupported ncrypto backend" +#error "Unsupported crypto backend" #endif } diff --git a/tools/cpplint.py b/tools/cpplint.py index 1e26e3ded396..d77fac39ce12 100755 --- a/tools/cpplint.py +++ b/tools/cpplint.py @@ -300,6 +300,7 @@ _ERROR_CATEGORIES = [ "build/c++11", "build/c++17", + "build/crypto_backend", "build/deprecated", "build/endif_comment", "build/explicit_make_pair", @@ -7466,6 +7467,55 @@ def CheckThreadLocalUsage(filename, lines, error): 'NOLINTNEXTLINE(runtime/thread_local).') +def CheckCryptoBackendUsage(filename, clean_lines, error): + """Require an explicit unsupported-backend error in crypto backend splits. + + A single backend gate may omit other backends entirely. Once a group has + alternative branches, it must explicitly reject unknown backends instead of + silently treating them as one of the supported implementations. + """ + backend = re.compile(r'\bNCRYPTO_USE_(?:OPENSSL_PROVIDER|BORINGSSL)\b') + directives = re.compile( + r'^\s*#\s*(if|ifdef|ifndef|elif|elifdef|elifndef|else|endif|error)\b(.*)$') + stack = [] + lines = iter(enumerate(clean_lines.lines)) + for linenum, line in lines: + # Preprocessor conditions can span multiple physical lines. + while line.endswith('\\'): + line = line[:-1] + next(lines, (0, ''))[1] + match = directives.match(line) + if not match: + continue + directive, condition = match.groups() + if directive in ('if', 'ifdef', 'ifndef'): + stack.append({ + 'linenum': linenum, + 'backend': bool(backend.search(condition)), + 'alternative': False, + 'else': False, + 'error': False, + }) + elif stack: + group = stack[-1] + if directive in ('elif', 'elifdef', 'elifndef'): + group['backend'] |= bool(backend.search(condition)) + group['alternative'] = True + elif directive == 'else': + group['alternative'] = True + group['else'] = True + elif directive == 'error' and group['else']: + if condition.strip() == '"Unsupported crypto backend"': + group['error'] = True + elif directive == 'endif': + stack.pop() + if (group['backend'] and group['alternative'] and + not (group['else'] and group['error'])): + error(filename, group['linenum'], 'build/crypto_backend', 5, + 'Crypto backend alternatives must end with ' + '#else and #error "Unsupported crypto backend"; ' + 'use an explicit #elif for each supported backend.') + + def ProcessLine( filename, file_extension, @@ -7631,6 +7681,8 @@ def ProcessFileData(filename, file_extension, lines, error, extra_check_function CheckThreadLocalUsage(filename, lines, error) + CheckCryptoBackendUsage(filename, clean_lines, error) + def ProcessConfigOverrides(filename): """Loads the configuration files and processes the config overrides.