Skip to content

Commit 2911fd7

Browse files
authored
Fix misuse of lstrip() (#14353)
Although `lstrip('lib')` will often appear to work it doesn't do what the callsite was expecting it to do. Introduce a new strip_prefix helper that that can be used in place of this.
1 parent bdaee66 commit 2911fd7

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

emcc.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from tools import colored_logger, diagnostics, building
4343
from tools.shared import unsuffixed, unsuffixed_basename, WINDOWS, safe_copy
4444
from tools.shared import run_process, read_and_preprocess, exit_with_error, DEBUG
45-
from tools.shared import do_replace
45+
from tools.shared import do_replace, strip_prefix
4646
from tools.response_file import substitute_response_files
4747
from tools.minimal_runtime_shell import generate_minimal_runtime_html
4848
import tools.line_endings
@@ -335,7 +335,7 @@ def standardize_setting_change(key, value):
335335
# and we can't just flip them, so leave them as-is to be
336336
# handled in a special way later)
337337
if key.startswith('NO_') and value in ('0', '1'):
338-
key = key[3:]
338+
key = strip_prefix(key, 'NO_')
339339
value = str(1 - int(value))
340340
return key, value
341341

@@ -357,7 +357,7 @@ def standardize_setting_change(key, value):
357357

358358
filename = None
359359
if value and value[0] == '@':
360-
filename = value[1:]
360+
filename = strip_prefix(value, '@')
361361
if not os.path.exists(filename):
362362
exit_with_error('%s: file not found parsing argument: %s=%s' % (filename, key, value))
363363
value = open(filename).read().strip()
@@ -618,7 +618,7 @@ def is_dash_s_for_emcc(args, i):
618618
return False
619619
arg = args[i + 1]
620620
else:
621-
arg = args[i][2:]
621+
arg = strip_prefix(args[i], '-s')
622622
arg = arg.split('=')[0]
623623
return arg.isidentifier() and arg.isupper()
624624

@@ -714,7 +714,7 @@ def parse_s_args(args):
714714
key = args[i + 1]
715715
args[i + 1] = ''
716716
else:
717-
key = args[i][2:]
717+
key = strip_prefix(args[i], '-s')
718718
args[i] = ''
719719

720720
# If not = is specified default to 1
@@ -1244,7 +1244,7 @@ def phase_setup(options, state, newargs, settings_map):
12441244
# For shared libraries that are neither bitcode nor wasm, assuming its local native
12451245
# library and attempt to find a library by the same name in our own library path.
12461246
# TODO(sbc): Do we really need this feature? See test_other.py:test_local_link
1247-
libname = unsuffixed_basename(arg).lstrip('lib')
1247+
libname = strip_prefix(unsuffixed_basename(arg), 'lib')
12481248
state.libs.append((i, libname))
12491249
else:
12501250
input_files.append((i, arg))
@@ -2294,7 +2294,7 @@ def get_language_mode(args):
22942294
return_next = True
22952295
continue
22962296
if item.startswith('-x'):
2297-
return item[2:]
2297+
return strip_prefix(item, '-x')
22982298
return ''
22992299

23002300
language_mode = get_language_mode(newargs)
@@ -2729,7 +2729,7 @@ def consume_arg_file():
27292729

27302730
if arg.startswith('-O'):
27312731
# Let -O default to -O2, which is what gcc does.
2732-
options.requested_level = arg[2:] or '2'
2732+
options.requested_level = strip_prefix(arg, '-O') or '2'
27332733
if options.requested_level == 's':
27342734
options.requested_level = 2
27352735
settings.SHRINK_LEVEL = 1
@@ -2784,7 +2784,7 @@ def consume_arg_file():
27842784
settings.DEBUG_LEVEL = max(1, settings.DEBUG_LEVEL)
27852785
elif arg.startswith('-g'):
27862786
options.requested_debug = arg
2787-
requested_level = arg[2:] or '3'
2787+
requested_level = strip_prefix(arg, '-g') or '3'
27882788
if is_int(requested_level):
27892789
# the -gX value is the debug level (-g1, -g2, etc.)
27902790
settings.DEBUG_LEVEL = validate_arg_level(requested_level, 4, 'Invalid debug level: ' + arg)
@@ -2959,7 +2959,7 @@ def consume_arg_file():
29592959
elif arg == '-frtti':
29602960
settings.USE_RTTI = 1
29612961
elif arg.startswith('-jsD'):
2962-
key = arg[4:]
2962+
key = strip_prefix(arg, '-jsD')
29632963
if '=' in key:
29642964
key, value = key.split('=')
29652965
else:
@@ -2975,7 +2975,7 @@ def consume_arg_file():
29752975
elif check_arg('-o'):
29762976
options.output_file = consume_arg()
29772977
elif arg.startswith('-o'):
2978-
options.output_file = arg[2:]
2978+
options.output_file = strip_prefix(arg, '-o')
29792979
newargs[i] = ''
29802980
elif arg == '-mllvm':
29812981
# Ignore the next argument rather than trying to parse it. This is needed

emscripten.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
from tools import shared
2626
from tools import gen_struct_info
2727
from tools import webassembly
28-
from tools.shared import WINDOWS, path_from_root, exit_with_error, asmjs_mangle, treat_as_user_function
28+
from tools.shared import WINDOWS, path_from_root, exit_with_error, asmjs_mangle
29+
from tools.shared import treat_as_user_function, strip_prefix
2930
from tools.settings import settings
3031

3132
logger = logging.getLogger('emscripten')
@@ -798,7 +799,7 @@ def create_invoke_wrappers(invoke_funcs):
798799
"""Asm.js-style exception handling: invoke wrapper generation."""
799800
invoke_wrappers = ''
800801
for invoke in invoke_funcs:
801-
sig = invoke[len('invoke_'):]
802+
sig = strip_prefix(invoke, 'invoke_')
802803
invoke_wrappers += '\n' + shared.JS.make_invoke(sig) + '\n'
803804
return invoke_wrappers
804805

tools/shared.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,11 @@ def unsuffixed_basename(name):
742742
return os.path.basename(unsuffixed(name))
743743

744744

745+
def strip_prefix(string, prefix):
746+
assert string.startswith(prefix)
747+
return string[len(prefix):]
748+
749+
745750
def safe_copy(src, dst):
746751
logging.debug('copy: %s -> %s', src, dst)
747752
src = os.path.abspath(src)

0 commit comments

Comments
 (0)