From 48ef7e5a0f28c21f5cda5d7ee06b1ed1ae8b17de Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Wed, 28 Sep 2022 14:42:57 +0100 Subject: [PATCH] [openpyxl]: fix exception control for internal error (#8616) Fix exception handling for issues. 48488 (https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=48488) 48506 (https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=48506)y --- projects/openpyxl/fuzz_comment.py | 17 +++++++++++++---- projects/openpyxl/fuzz_formulae.py | 19 +++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/projects/openpyxl/fuzz_comment.py b/projects/openpyxl/fuzz_comment.py index e332f86d2..3db9ef96d 100644 --- a/projects/openpyxl/fuzz_comment.py +++ b/projects/openpyxl/fuzz_comment.py @@ -24,20 +24,29 @@ def TestInput(data): wb = Workbook() ws = wb.active - + #Set Comment c1 = ws['A1'].comment - c1 = Comment(fdp.ConsumeString(10),fdp.ConsumeString(10)) + c1 = Comment(fdp.ConsumeUnicodeNoSurrogates(10), + fdp.ConsumeUnicodeNoSurrogates(10)) c1.width = 300 c1.height = 50 ws['A1'].comment = c1 #Double assign comment - c2 = Comment(fdp.ConsumeString(10),fdp.ConsumeString(10)) + c2 = Comment(fdp.ConsumeUnicodeNoSurrogates(10), + fdp.ConsumeUnicodeNoSurrogates(10)) ws['B1'].comment = c2 ws['C1'].comment = c2 - wb.save('%s.xlsx'%fdp.ConsumeString(10)) + try: + wb.save('%s.xlsx'%fdp.ConsumeUnicodeNoSurrogates(10)) + except ValueError as e: + if "embedded null byte" not in str(e): + raise e + except FileNotFoundError: + # Expected for corrupted file name + pass def main(): atheris.Setup(sys.argv, TestInput, enable_python_coverage=True) diff --git a/projects/openpyxl/fuzz_formulae.py b/projects/openpyxl/fuzz_formulae.py index 845e9e484..e1a034fa8 100644 --- a/projects/openpyxl/fuzz_formulae.py +++ b/projects/openpyxl/fuzz_formulae.py @@ -17,17 +17,24 @@ import atheris import sys with atheris.instrument_imports(): - from openpyxl.formula import Tokenizer - from openpyxl.formula.translate import Translator + from openpyxl.formula.tokenizer import * + from openpyxl.formula.translate import * def TestInput(data): fdp = atheris.FuzzedDataProvider(data) - #Initial tokenizer for random string and process it - Tokenizer(fdp.ConsumeString(200)) + try: + #Initial tokenizer for random string and process it + Tokenizer(fdp.ConsumeString(200)) - #Translate random string formulae - Translator(fdp.ConsumeString(200), origin="A1").translate_formula("B2") + #Translate random string formulae + Translator(fdp.ConsumeString(200), origin="A1").translate_formula("B2") + except IndexError as e: + if 'empty list' not in str(e): + raise e + except (TokenizerError, TranslatorError): + # Internal error handling is expected + pass def main(): atheris.Setup(sys.argv, TestInput, enable_python_coverage=True)