Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #44 to Warn if Integer Would Lose Precision #695

Closed
wants to merge 38 commits into from

Conversation

robminer6
Copy link
Contributor

@robminer6 robminer6 commented Apr 20, 2022

This pull request implements a new warning which warns if an integer literal is used which cannot be accurately represented by JavaScript's "Number" type and would lose precision. This is done by checking the number of digits in the number and using std::stold. Future implementations could expand this to warn about decimal numbers and numbers in binary/octal/hexadecimal.

robminer6 and others added 6 commits April 14, 2022 10:32
Correctly reports E0179 when returning an html tag on a new line. Added unit tests as well.
Co-authored-by: strager <strager.nds@gmail.com>
Fixes the nitpicks strager had and is ready to merge.
A first try at fixing quick-lint#44. Issues with the code include it only working with integers written in decimal and using multiple type conversions.
@robminer6
Copy link
Contributor Author

Oops, let me fix this first.

Copy link
Collaborator

@strager strager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must fix: Fix merge conflicts. You'll need to run ./tools/update-translator-sources, but that script is broken (my fault). I'll fix it ASAP.

Must fix: Your code causes quick-lint-js to crash given the following input:

999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

src/quick-lint-js/diagnostic-types.h Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/quick-lint-js/diagnostic-types.h Outdated Show resolved Hide resolved
@strager
Copy link
Collaborator

strager commented Apr 20, 2022

You'll need to run ./tools/update-translator-sources, but that script is broken (my fault). I'll fix it ASAP.

I fixed the script in commit aa889de (which is on master now).

Copy link
Collaborator

@strager strager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This task is more complicated than I thought. =O

Due to the complexity of the implementation, I think two tests is not enough. Let's add more tests, particularly around those magic numbers (15, 309, 310).

test/test-lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
robminer6 and others added 5 commits April 26, 2022 09:13
Co-authored-by: strager <strager.nds@gmail.com>
Co-authored-by: strager <strager.nds@gmail.com>
Co-authored-by: strager <strager.nds@gmail.com>
Co-authored-by: strager <strager.nds@gmail.com>
Co-authored-by: strager <strager.nds@gmail.com>
@strager
Copy link
Collaborator

strager commented Apr 28, 2022

you want me to add some constants. Where should I define them?

You can define them in the function which uses them.

Is there any way that I can test that my code conforms to the style guidelines before I submit it?

I assume you mean compiler warnings and code formatting.

Compiler warnings: You'd need to build with a bunch of different compilers to get full coverage. I don't bother doing this locally; I have CI do it. If CI reports an issue, and it's not obvious how to fix it, I build locally with that compiler. (Sometimes Docker helps.)

Code formatting: See https://github.com/quick-lint/quick-lint-js/blob/a4f2dea1827d6a5bac15db62582366d3a2133a7c/docs/CONTRIBUTING.md#clang-format

@strager
Copy link
Collaborator

strager commented Apr 28, 2022

You can add -Wall -Wextra to CMAKE_CXX_FLAGS in build/CMakeCache.txt to enable more compiler warnings locally. Maybe that's what you're asking about.

I'm getting some weird errors and the tests I wrote are failing. Also, what's the best way to style the super long strings?
commit a4f2dea
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Thu Apr 28 02:33:42 2022 -0700

    refactor(chocolatey): remove extra whitespace

commit f9e8e87
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Wed Apr 27 17:10:04 2022 -0700

    feat(debian): create empty changelog for downstream contribution

    Create a changelog with only one entry, per feedback from
    bage@debian.org:

    > The changelog must have one entry for the initial version that closes
    > your ITP.
    > It has to have a "-1" revision. Get rid of all old versions because
    > they have never been in the Debian archive.

    https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1010040#12

commit ced0288
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Wed Apr 27 11:52:23 2022 -0700

    fix(website): fix &lt; in error list

    The error list (https://quick-lint-js.com/errors/) shows the titles of
    error files from Markdown. Some titles contain HTML or other Markdown,
    causing titles such as the following to be shown to the user:

    * E0187: mismatched JSX tags; expected &lt;/foo>
    * E0195: missing parentheses around operand of `typeof`; `typeof`
      operator cannot be used before `**` without parentheses

    Fix the parsing of HTML and Markdown by HTMLifying the titles before
    storing into titleErrorDescription. Rename titleErrorDescription to
    titleErrorDescriptionHTML to make it clearer that it now stores HTML.

    This commit should not affect individual error pages which already
    HTMLified the title.

commit cb67a02
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Wed Apr 27 11:47:11 2022 -0700

    refactor(website): stop tracking title for non-title nodes

    The 'inTitle' variable is never set to false, so we keep accumulating
    data into 'currentBlock', even though 'currentBlock' won't be
    referenced. Avoid needless string concatenations by setting 'inTitle' to
    false when 'currentBlock' won't be used anymore.

commit 13e66b0
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Wed Apr 27 01:42:26 2022 -0700

    refactor(website): remove unused check for /*TODO*/

    This /* TODO */ comment check is dead code. Delete it.

commit 8841c72
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Wed Apr 27 01:40:58 2022 -0700

    chore(docs): update changelog

commit ab055fb
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Wed Apr 27 01:36:14 2022 -0700

    refactor(website): add 'Async' to name of async functions

commit 24fdcd5
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Wed Apr 27 00:52:46 2022 -0700

    refactor: fix line endings in char8-debug.cpp

    DOS (CRLF) -> UNIX (LF)

commit 0d2d7bb
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Wed Apr 27 00:51:47 2022 -0700

    refactor: simplify char8.cpp implementation

    Remove duplicate implementations by disabling warnings about useless
    casts.

commit 5c19e4e
Author: Nico Sonack <nsonack@herrhotzenplotz.de>
Date:   Mon Apr 25 16:17:55 2022 +0200

    tools: Add translation testing tool

    This still contains a few bugs as it doesn't scrape all the files correctly.
    This will be fixed in the future.

commit dcc5f82
Author: Nico Sonack <nsonack@herrhotzenplotz.de>
Date:   Mon Apr 25 16:03:46 2022 +0200

    po: Update German translations

    - Update po/de.po
    - Regenerate translation tables

commit 563cee5
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Tue Apr 26 00:55:13 2022 -0700

    refactor: inline is_arrow_kind helper function

commit 576ade9
Author: Matthew Glazar <strager.nds@gmail.com>
Date:   Mon Apr 25 16:52:39 2022 -0700

    feat(website): document installing with winget

commit 4930cfc
Author: Matthew Glazar <strager.nds@gmail.com>
Date:   Mon Apr 25 16:45:31 2022 -0700

    fix(website): fix vertical alignment of breadcrumbs

commit 00d5f12
Author: Matthew Glazar <strager.nds@gmail.com>
Date:   Mon Apr 25 16:42:18 2022 -0700

    refactor(website): deduplicate breadcrumb HTML

commit 9ada865
Author: Matthew Glazar <strager.nds@gmail.com>
Date:   Mon Apr 25 16:34:42 2022 -0700

    fix(website): fix build on Windows

    'yarn build' fails on Windows with the following error:

        Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'

    This is because we are passing a file path to 'import()', which expects
    a URL.

    Give 'import()' a URL instead of a path, fixing the error and making
    'yarn build' succeed.

commit 8239f0b
Author: Matthew Glazar <strager.nds@gmail.com>
Date:   Mon Apr 25 16:28:14 2022 -0700

    fix(website): fix loading dusty.svg on Windows

    On Windows, dusty.svg is a symbolic link pointing to another file. In my
    Git checkout, the core.symlinks Git config is set to false, making Git
    represent symbolic links as text files containing the target's path, not
    as a copy or as a Windows link.

    Work around Git's symlink behavior by following the symlink manually if
    needed.
Copy link
Collaborator

@strager strager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting some weird errors and the tests I wrote are failing.

I'll look at this later.

test/test-lex.cpp Show resolved Hide resolved
test/test-lex.cpp Outdated Show resolved Hide resolved
test/test-lex.cpp Outdated Show resolved Hide resolved
@strager
Copy link
Collaborator

strager commented Apr 29, 2022

Here's the test failure I get on my machine:

[ RUN      ] test_lex.fail_lex_integer_loses_precision
/home/strager/Projects/quicklint-js/test/test-lex.cpp:351: Failure
Value of: errors
Expected: has 1 element that is a variant<> with value of type 'the element type' and the value (is an object whose field `characters` has begin-end offset 0-16) and (is an object whose field `rounded_val` is equal to { U+0039, U+0030, U+0030, U+0037, U+0031, U+0039, U+0039, U+0032, U+0035, U+0034, U+0037, U+0034, U+0030, U+0039, U+0039, U+0032 })
  Actual: { diag_number_literal_will_lose_precision }, whose element #0 doesn't match, whose value diag_number_literal_will_lose_precision doesn't match, whose field `rounded_val` is { U+0039, U+0030, U+0030, U+0037, U+0031, U+0039, U+0039, U+0032, U+0035, U+0034, U+0037, U+0034, U+0030, U+0039, U+0039, U+0032, U+0020, U+003E, U+0017, U+0001 }
Trace/breakpoint trap (core dumped)

The second (actual) array is identical to the first (expected) array, except the second array has the following extra items at the end: U+0020, U+003E, U+0017, U+0001

Perhaps the problem is that we're reading uninitialized memory?

@strager
Copy link
Collaborator

strager commented Apr 29, 2022

I ran the test with GCC's ASAN (CMAKE_CXX_FLAGS=-fsanitizer=address) and it reported the following:

[ RUN      ] test_lex.fail_lex_integer_loses_precision
=================================================================
==61044==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6210002a9100 at pc 0x55dc4e67fd89 bp 0x7ffcc1909f70 sp 0x7ffcc1909f60
READ of size 1 at 0x6210002a9100 thread T0
    #0 0x55dc4e67fd88 in std::char_traits<char8_t>::eq(char8_t const&, char8_t const&) /usr/include/c++/9/bits/char_traits.h:530
    #1 0x55dc4e680003 in std::char_traits<char8_t>::length(char8_t const*) /usr/include/c++/9/bits/char_traits.h:558
    #2 0x55dc4e6830b0 in std::basic_string_view<char8_t, std::char_traits<char8_t> >::basic_string_view(char8_t const*) /usr/include/c++/9/string_view:124
    #3 0x55dc505bf380 in quick_lint_js::lexer::check_precision_loss(char8_t const*, char8_t const*) /home/strager/Projects/quicklint-js/src/lex.cpp:1325
    #4 0x55dc505c0575 in quick_lint_js::lexer::parse_number() /home/strager/Projects/quicklint-js/src/lex.cpp:1384
    #5 0x55dc505aa1b3 in quick_lint_js::lexer::try_parse_current_token() /home/strager/Projects/quicklint-js/src/lex.cpp:217
    #6 0x55dc505a96ba in quick_lint_js::lexer::parse_current_token() /home/strager/Projects/quicklint-js/src/lex.cpp:181
    #7 0x55dc505a8dbb in quick_lint_js::lexer::lexer(quick_lint_js::padded_string_view, quick_lint_js::diag_reporter*) /home/strager/Projects/quicklint-js/src/lex.cpp:156
    #8 0x55dc4ed900d2 in void __gnu_cxx::new_allocator<quick_lint_js::lexer>::construct<quick_lint_js::lexer, quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&>(quick_lint_js::lexer*, quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&) /usr/include/c++/9/ext/new_allocator.h:146
    #9 0x55dc4ed761c1 in void std::allocator_traits<std::allocator<quick_lint_js::lexer> >::construct<quick_lint_js::lexer, quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&>(std::allocator<quick_lint_js::lexer>&, quick_lint_js::lexer*, quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&) /usr/include/c++/9/bits/alloc_traits.h:483
    #10 0x55dc4ed4f0b1 in quick_lint_js::lexer& std::__cxx1998::deque<quick_lint_js::lexer, std::allocator<quick_lint_js::lexer> >::emplace_back<quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&>(quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&) /usr/include/c++/9/bits/deque.tcc:168
    #11 0x55dc4ed2a121 in quick_lint_js::lexer& std::__debug::deque<quick_lint_js::lexer, std::allocator<quick_lint_js::lexer> >::emplace_back<quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&>(quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&) /usr/include/c++/9/debug/deque:419
    #12 0x55dc4ec6060f in make_lexer /home/strager/Projects/quicklint-js/test/test-lex.cpp:92
    #13 0x55dc4ed299c0 in lex_to_eof /home/strager/Projects/quicklint-js/test/test-lex.cpp:3274
    #14 0x55dc4ed288fd in check_tokens_with_errors /home/strager/Projects/quicklint-js/test/test-lex.cpp:3252
    #15 0x55dc4ed27cf0 in check_tokens_with_errors /home/strager/Projects/quicklint-js/test/test-lex.cpp:3240
    #16 0x55dc4ec76de7 in TestBody /home/strager/Projects/quicklint-js/test/test-lex.cpp:343
    #17 0x55dc5030c79e in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2607
    #18 0x55dc502e5877 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2662
    #19 0x55dc50276ecb in testing::Test::Run() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2682
    #20 0x55dc50278dfb in testing::TestInfo::Run() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2861
    #21 0x55dc5027c223 in testing::TestSuite::Run() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:3015
    #22 0x55dc502ae17c in testing::internal::UnitTestImpl::RunAllTests() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:5855
    #23 0x55dc50314008 in bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2607
    #24 0x55dc502eaf7f in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2662
    #25 0x55dc502a5d3a in testing::UnitTest::Run() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:5438
    #26 0x55dc502506f6 in RUN_ALL_TESTS() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/include/gtest/gtest.h:2490
    #27 0x55dc502507ca in main /home/strager/Projects/quicklint-js/vendor/googletest/googlemock/src/gmock_main.cc:70
    #28 0x7fced52920b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x240b2)
    #29 0x55dc4e548fad in _start (/home/strager/Projects/quicklint-js/build-debug/test/quick-lint-js-test+0x6048fad)

0x6210002a9100 is located 0 bytes to the right of 4096-byte region [0x6210002a8100,0x6210002a9100)
allocated by thread T0 here:
    #0 0x7fced624d587 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:104
    #1 0x55dc4e725ab7 in quick_lint_js::linked_bump_allocator<8ul>::chunk_header::new_chunk(unsigned long, quick_lint_js::linked_bump_allocator<8ul>::chunk_header*) /home/strager/Projects/quicklint-js/src/./quick-lint-js/linked-bump-allocator.h:255
    #2 0x55dc4e724ce9 in quick_lint_js::linked_bump_allocator<8ul>::append_chunk(unsigned long) /home/strager/Projects/quicklint-js/src/./quick-lint-js/linked-bump-allocator.h:324
    #3 0x55dc4e722e30 in quick_lint_js::linked_bump_allocator<8ul>::allocate_bytes(unsigned long) /home/strager/Projects/quicklint-js/src/./quick-lint-js/linked-bump-allocator.h:293
    #4 0x55dc50542dc1 in char8_t* quick_lint_js::linked_bump_allocator<8ul>::allocate_uninitialized_array<char8_t>(unsigned long) /home/strager/Projects/quicklint-js/src/./quick-lint-js/linked-bump-allocator.h:147
    #5 0x55dc505bf241 in quick_lint_js::lexer::check_precision_loss(char8_t const*, char8_t const*) /home/strager/Projects/quicklint-js/src/lex.cpp:1321
    #6 0x55dc505c0575 in quick_lint_js::lexer::parse_number() /home/strager/Projects/quicklint-js/src/lex.cpp:1384
    #7 0x55dc505aa1b3 in quick_lint_js::lexer::try_parse_current_token() /home/strager/Projects/quicklint-js/src/lex.cpp:217
    #8 0x55dc505a96ba in quick_lint_js::lexer::parse_current_token() /home/strager/Projects/quicklint-js/src/lex.cpp:181
    #9 0x55dc505a8dbb in quick_lint_js::lexer::lexer(quick_lint_js::padded_string_view, quick_lint_js::diag_reporter*) /home/strager/Projects/quicklint-js/src/lex.cpp:156
    #10 0x55dc4ed900d2 in void __gnu_cxx::new_allocator<quick_lint_js::lexer>::construct<quick_lint_js::lexer, quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&>(quick_lint_js::lexer*, quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&) /usr/include/c++/9/ext/new_allocator.h:146
    #11 0x55dc4ed761c1 in void std::allocator_traits<std::allocator<quick_lint_js::lexer> >::construct<quick_lint_js::lexer, quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&>(std::allocator<quick_lint_js::lexer>&, quick_lint_js::lexer*, quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&) /usr/include/c++/9/bits/alloc_traits.h:483
    #12 0x55dc4ed4f0b1 in quick_lint_js::lexer& std::__cxx1998::deque<quick_lint_js::lexer, std::allocator<quick_lint_js::lexer> >::emplace_back<quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&>(quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&) /usr/include/c++/9/bits/deque.tcc:168
    #13 0x55dc4ed2a121 in quick_lint_js::lexer& std::__debug::deque<quick_lint_js::lexer, std::allocator<quick_lint_js::lexer> >::emplace_back<quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&>(quick_lint_js::padded_string_view&, quick_lint_js::diag_collector*&) /usr/include/c++/9/debug/deque:419
    #14 0x55dc4ec6060f in make_lexer /home/strager/Projects/quicklint-js/test/test-lex.cpp:92
    #15 0x55dc4ed299c0 in lex_to_eof /home/strager/Projects/quicklint-js/test/test-lex.cpp:3274
    #16 0x55dc4ed288fd in check_tokens_with_errors /home/strager/Projects/quicklint-js/test/test-lex.cpp:3252
    #17 0x55dc4ed27cf0 in check_tokens_with_errors /home/strager/Projects/quicklint-js/test/test-lex.cpp:3240
    #18 0x55dc4ec76de7 in TestBody /home/strager/Projects/quicklint-js/test/test-lex.cpp:343
    #19 0x55dc5030c79e in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2607
    #20 0x55dc502e5877 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2662
    #21 0x55dc50276ecb in testing::Test::Run() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2682
    #22 0x55dc50278dfb in testing::TestInfo::Run() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2861
    #23 0x55dc5027c223 in testing::TestSuite::Run() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:3015
    #24 0x55dc502ae17c in testing::internal::UnitTestImpl::RunAllTests() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:5855
    #25 0x55dc50314008 in bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2607
    #26 0x55dc502eaf7f in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:2662
    #27 0x55dc502a5d3a in testing::UnitTest::Run() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/src/gtest.cc:5438
    #28 0x55dc502506f6 in RUN_ALL_TESTS() /home/strager/Projects/quicklint-js/vendor/googletest/googletest/include/gtest/gtest.h:2490
    #29 0x55dc502507ca in main /home/strager/Projects/quicklint-js/vendor/googletest/googlemock/src/gmock_main.cc:70

SUMMARY: AddressSanitizer: heap-buffer-overflow /usr/include/c++/9/bits/char_traits.h:530 in std::char_traits<char8_t>::eq(char8_t const&, char8_t const&)
Shadow bytes around the buggy address:
  0x0c428004d1d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c428004d1e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c428004d1f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c428004d200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c428004d210: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c428004d220:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c428004d230: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c428004d240: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c428004d250: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c428004d260: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c428004d270: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==61044==ABORTING

@strager
Copy link
Collaborator

strager commented Apr 29, 2022

I think I see the bug:

        char8* rounded_val =
            this->allocator_.allocate_uninitialized_array<char8>(
                result_string_view.size());
        std::copy(result_string_view.begin(), result_string_view.end(),
                  rounded_val);
        this->diag_reporter_->report(diag_number_literal_will_lose_precision{
            .characters = source_code_span(number_begin, input),
            .rounded_val = rounded_val,
        });

.rounded_val = rounded_val, is implicitly construct a string8_view. The constructor assumes that rounded_val (char8*) is null-terminated. You are not null-terminating the string, so string8_view's constructor reads out of bounds.

I think you should fix this by creating a string8_view with the correct size. (No null-termination needed.)

@strager
Copy link
Collaborator

strager commented Apr 30, 2022

I'm having trouble figuring out solutions to the Clang and error docs CI test fails. Could you check them out? Each only has one error and they seem simple

macOS Clang

[ 82%] Building CXX object test/CMakeFiles/quick-lint-js-test.dir/test-lex.cpp.o
/Users/runner/work/quick-lint-js/quick-lint-js/test/test-lex.cpp:355:29: error: unused parameter 'input' [-Werror,-Wunused-parameter]
      [](padded_string_view input, const auto& errors) {
                            ^
/Users/runner/work/quick-lint-js/quick-lint-js/test/test-lex.cpp:360:51: error: unused parameter 'input' [-Werror,-Wunused-parameter]
      {token_type::number}, [](padded_string_view input, const auto& errors) {
                                                  ^
2 errors generated.

Solution: Remove the parameter names. E.g. [](padded_string_view, const auto& errors)

check error docs

Error: found problems in error documents:
/__w/quick-lint-js/quick-lint-js/docs/errors/E0044.md: error: expected only E0044 errors in first code block but found E0212
    at reportProblemsInDocumentsAsync (file:///__w/quick-lint-js/quick-lint-js/website/src/error-documentation.mjs:324:11)
    at async mainAsync (file:///__w/quick-lint-js/quick-lint-js/website/check-error-documentation.mjs:26:3)

Look at docs/errors/E0044.md. quick-lint-js is now reporting E0212 for the code example. If this is intended, then update the code example so it only reports E0044. If this is incorrect, fix your E0212-reporting code.

Copy link
Collaborator

@strager strager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your patch looks good! It was more complicated than I originally thought it'd be. =S

Please update the PR's title and description. Then tell me when you want me to merge this change.

src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
test/test-lex.cpp Outdated Show resolved Hide resolved
robminer6 and others added 5 commits April 30, 2022 21:01
Co-authored-by: strager <strager.nds@gmail.com>
Co-authored-by: strager <strager.nds@gmail.com>
Co-authored-by: strager <strager.nds@gmail.com>
Co-authored-by: strager <strager.nds@gmail.com>
Co-authored-by: strager <strager.nds@gmail.com>
@robminer6 robminer6 changed the title Preliminary fix #44 Fix #44 to Warn if May 1, 2022
@robminer6 robminer6 changed the title Fix #44 to Warn if Fix #44 to Warn if Integer Would Lose Precision May 1, 2022
@robminer6
Copy link
Contributor Author

Ready to merge!

src/lex.cpp Outdated Show resolved Hide resolved
src/lex.cpp Outdated Show resolved Hide resolved
@robminer6
Copy link
Contributor Author

Ready to merge again if the new comments look good.

Comment on lines +1303 to +1304
// equal to 2^1023 × (1 + (1 − 2^−52)) ≈ 1.7976931348623157 × 10^308, which is
// 309 digits long.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@strager
Copy link
Collaborator

strager commented May 1, 2022

Landed as commit eb7418e.

@strager strager closed this May 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants