Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_x509_gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ X509Pointer NewCertificate() {
#elif NCRYPTO_USE_BORINGSSL
return X509Pointer(X509_new());
#else
#error "Unsupported ncrypto backend"
#error "Unsupported crypto backend"
#endif
}

Expand Down
52 changes: 52 additions & 0 deletions tools/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@
_ERROR_CATEGORIES = [
"build/c++11",
"build/c++17",
"build/crypto_backend",
"build/deprecated",
"build/endif_comment",
"build/explicit_make_pair",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
Loading