diff options
author | Tomasz Miąsko <tomasz.miasko@gmail.com> | 2018-04-27 00:00:00 +0000 |
---|---|---|
committer | Christoph Reiter <reiter.christoph@gmail.com> | 2018-07-11 09:16:14 +0200 |
commit | a41abe1868a693387cd5cf85567cf2e0fd6c62df (patch) | |
tree | 6b02fbbee3a66cd19514e9b032ae13ed849463af /giscanner/shlibs.py | |
parent | eaabc3ba8a827ca1455acc44c1d7c73e2a92bee4 (diff) | |
download | gobject-introspection-a41abe1868a693387cd5cf85567cf2e0fd6c62df.tar.gz |
Avoid accidental library name matches when parsing ldd output.
* Use a single pattern that matches against potentially complete paths.
* Extract filename only afterwards on platforms where it is necessary.
* Match patterns against complete words in ldd output instead of searching
for them inside the lines - this avoids unintentional matches without
complexity of negative lookbehinds and negative lookaheads.
Fixes issue #208.
Diffstat (limited to 'giscanner/shlibs.py')
-rw-r--r-- | giscanner/shlibs.py | 73 |
1 files changed, 40 insertions, 33 deletions
diff --git a/giscanner/shlibs.py b/giscanner/shlibs.py index 525bdba2..32fc5e44 100644 --- a/giscanner/shlibs.py +++ b/giscanner/shlibs.py @@ -49,19 +49,17 @@ def _resolve_libtool(options, binary, libraries): # libpangoft2-1.0.so.0 => /usr/lib/libpangoft2-1.0.so.0 (0x006c1000) # # We say that if something in the output looks like libpangoft2<blah> -# then the *first* such in the output is the soname. We require <blah> -# to start with [^A-Za-z0-9_-] to avoid problems with libpango vs libpangoft2 -# -# The negative lookbehind at the start is to avoid problems if someone -# is crazy enough to name a library liblib<foo> when lib<foo> exists. -# -# Match absolute paths on OS X to conform to how libraries are usually -# referenced on OS X systems. +# then the *first* such in the output is the soname. def _ldd_library_pattern(library_name): - pattern = "(?<![A-Za-z0-9_-])(lib*%s[^A-Za-z0-9_-][^\s\(\)]*)" - if platform.system() == 'Darwin': - pattern = "([^\s]*lib*%s[^A-Za-z0-9_-][^\s\(\)]*)" - return re.compile(pattern % re.escape(library_name)) + return re.compile(r"""^ + # Require trailing slash to avoid matching liblibfoo when looking for libfoo. + (.*[/])? + lib%s + # Prohibit library name characters to avoid matching libpangoft2 when looking for libpango. + [^/A-Za-z0-9_-] + # Anything but the path separator to avoid matching directories. + [^/]* + $""" % re.escape(library_name), re.VERBOSE) # This is a what we do for non-la files. We assume that we are on an @@ -96,8 +94,7 @@ def _resolve_non_libtool(options, binary, libraries): if host_os() == 'nt': cc = CCompiler() - shlibs = cc.resolve_windows_libs(libraries, options) - + return cc.resolve_windows_libs(libraries, options) else: args = [] libtool = get_libtool_command(options) @@ -109,31 +106,41 @@ def _resolve_non_libtool(options, binary, libraries): args.extend(['otool', '-L', binary.args[0]]) else: args.extend(['ldd', binary.args[0]]) - proc = subprocess.Popen(args, stdout=subprocess.PIPE) - patterns = {} - for library in libraries: - patterns[library] = _ldd_library_pattern(library) - - shlibs = [] - for line in proc.stdout: - line = line.decode('ascii') - # ldd on *BSD show the argument passed on the first line even if - # there is only one argument. We have to ignore it because it is - # possible for the name of the binary to match _ldd_library_pattern. - if line == binary.args[0] + ':\n': - continue + output = subprocess.check_output(args, universal_newlines=True, errors='replace') + + # Use absolute paths on OS X to conform to how libraries are usually + # referenced on OS X systems, and file names everywhere else. + basename = platform.system() != 'Darwin' + return resolve_from_ldd_output(libraries, output, basename=basename) + + +def resolve_from_ldd_output(libraries, output, basename=False): + patterns = {} + for library in libraries: + patterns[library] = _ldd_library_pattern(library) + + shlibs = [] + for line in output.splitlines(): + # ldd on *BSD show the argument passed on the first line even if + # there is only one argument. We have to ignore it because it is + # possible for the name of the binary to match _ldd_library_pattern. + if line.endswith(':'): + continue + for word in line.split(): for library, pattern in patterns.items(): - m = pattern.search(line) + m = pattern.match(word) if m: del patterns[library] - shlibs.append(m.group(1)) + shlibs.append(m.group()) break - if len(patterns) > 0: - raise SystemExit( - "ERROR: can't resolve libraries to shared libraries: " + - ", ".join(patterns.keys())) + if len(patterns) > 0: + raise SystemExit( + "ERROR: can't resolve libraries to shared libraries: " + + ", ".join(patterns.keys())) + if basename: + shlibs = list(map(os.path.basename, shlibs)) return shlibs |