Skip to content

Commit

Permalink
Added check for stdbool.h header and forcing its use (#159)
Browse files Browse the repository at this point in the history
When compiling YARA without Python bindings, stdbool.h is actually used
if it can be used. However Python bindings completely ignore it resulting
in YARA defining its own bool type with sizeof(bool) == sizeof(int).
However before [#1377](VirusTotal/yara#1377), this
can result in different compilation units using different sizes of
bool type and chaos ensues.
  • Loading branch information
plusvic committed Feb 23, 2021
2 parents 4ab6058 + a7e5627 commit 42e6ce7
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
# limitations under the License.
#

from setuptools import setup, Command, Extension
from distutils.command.build import build
from distutils.command.build_ext import build_ext
from setuptools import setup, Command, Extension
from codecs import open

import distutils.errors
Expand Down Expand Up @@ -87,6 +87,23 @@ def has_function(function_name, libraries=None):
return result


def has_header(header_name):
compiler = distutils.ccompiler.new_compiler()
with muted(sys.stdout, sys.stderr):
with tempfile.NamedTemporaryFile(mode='w', prefix=header_name, delete=False, suffix='.c') as f:
f.write("""
#include <{}>
int main() {{ return 0; }}
""".format(header_name))
f.close()
try:
compiler.compile([f.name])
except distutils.errors.CompileError:
return False
return True


class BuildCommand(build):

user_options = build.user_options + OPTIONS
Expand Down Expand Up @@ -221,6 +238,9 @@ def run(self):
module.define_macros.append(('USE_NO_PROC', '1'))
module.extra_compile_args.append('-std=c99')

if has_header('stdbool.h'):
module.define_macros.append(('HAVE_STDBOOL_H', '1'))

if has_function('memmem'):
module.define_macros.append(('HAVE_MEMMEM', '1'))
if has_function('strlcpy'):
Expand All @@ -240,6 +260,9 @@ def run(self):
module.define_macros.append(('HASH_MODULE', '1'))
module.define_macros.append(('HAVE_LIBCRYPTO', '1'))
module.libraries.append('crypto')
elif building_for_windows:
module.define_macros.append(('HASH_MODULE', '1'))
module.define_macros.append(('HAVE_WINCRYPT_H', '1'))
else:
exclusions.append('yara/libyara/modules/hash/hash.c')

Expand Down

0 comments on commit 42e6ce7

Please sign in to comment.