Skip to content

Commit deecc0f

Browse files
author
TechHog8984
committed
add asan option to build to help fix memory issues (foreshadowing); remove unnecessary contains check on node_tag_map; replace strcpy with memcpy on SimplifyResult::asString since we're not working with null-terminated strings (huge memory issue)
1 parent 97c50ea commit deecc0f

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

build.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LUAU_INCLUDE_BUILD=$(echo $LUAU_INCLUDE | sed 's/dependencies\//..\/..\/..\/depe
99
PLATFORM=linux
1010
RELEASE_FLAGS=
1111
STATIC_FLAGS=-static
12+
ASAN_FLAGS=
1213
TEST_DEFINES=
1314

1415
while [[ $# -gt 0 ]]; do
@@ -37,6 +38,10 @@ while [[ $# -gt 0 ]]; do
3738
TEST_DEFINES=-DFAILTESTS
3839
shift
3940
;;
41+
--asan)
42+
ASAN_FLAGS=-fsanitize=address
43+
shift
44+
;;
4045
*)
4146
echo "Unknown option: $1"
4247
exit 1
@@ -94,7 +99,7 @@ mkdir $luauformatbuilddir
9499

95100
echo "building luau-format..."
96101
pushd $luauformatbuilddir
97-
$compiler -std=c++17 -g -Wall $RELEASE_FLAGS -c ../../../src/* -I../../../include $LUAU_INCLUDE_BUILD -L../Luau -lluau
102+
$compiler -std=c++17 -g -Wall $RELEASE_FLAGS $ASAN_FLAGS -c ../../../src/* -I../../../include $LUAU_INCLUDE_BUILD -L../Luau -lluau
98103
ar rcs libluau-format.a *.o
99104
popd
100105
echo "luau-format built"
@@ -104,11 +109,11 @@ if [[ $TEST ]]; then
104109
if [[ ! $TEST_DEFINES = "" ]]; then
105110
echo "NOTE: tests will fail due to --failtests being passed"
106111
fi
107-
$compiler -std=c++17 $STATIC_FLAGS -o $outfile -g -Wall tests/* -Itests -Iinclude $LUAU_INCLUDE $TEST_DEFINES -L$luauformatbuilddir -lluau-format -L$luaubuilddir -lluau
112+
$compiler -std=c++17 $ASAN_FLAGS $STATIC_FLAGS -o $outfile -g -Wall tests/* -Itests -Iinclude $LUAU_INCLUDE $TEST_DEFINES -L$luauformatbuilddir -lluau-format -L$luaubuilddir -lluau
108113
echo "tests built to $outfile"
109114
exit
110115
fi
111116

112117
echo "buildling cli..."
113-
$compiler -std=c++17 -g -Wall $RELEASE_FLAGS $STATIC_FLAGS -o $outfile main.cpp -Iinclude $LUAU_INCLUDE -L$luauformatbuilddir -lluau-format -L$luaubuilddir -lluau
118+
$compiler -std=c++17 -g -Wall $RELEASE_FLAGS $ASAN_FLAGS $STATIC_FLAGS -o $outfile main.cpp -Iinclude $LUAU_INCLUDE -L$luauformatbuilddir -lluau-format -L$luaubuilddir -lluau
114119
echo "cli built to $outfile"

src/formatter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ size_t AstFormatter::appendOptionalSemicolon(std::string& current, std::string&
349349
}
350350

351351
AstFormatter::NodeTag& AstFormatter::getNodeTag(AstNode* node) {
352-
if (node_tag_map.find(node) == node_tag_map.end())
353-
node_tag_map.emplace(node, NodeTag{});
352+
node_tag_map.emplace(node, NodeTag{});
354353

355354
return node_tag_map.at(node);
356355
}

src/simplifier.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,10 +1269,11 @@ std::optional<AstArray<char>> SimplifyResult::asString() {
12691269
switch (type) {
12701270
case Number: {
12711271
std::string str = convertNumber(number_value);
1272-
char* allocated = static_cast<char*>(simplifier->getAllocator().allocate(str.size()));
1273-
strcpy(allocated, str.data());
1272+
const size_t len = str.size();
1273+
char* allocated = static_cast<char*>(simplifier->getAllocator().allocate(len));
1274+
memcpy(allocated, str.data(), len);
12741275
result.data = allocated;
1275-
result.size = str.size();
1276+
result.size = len;
12761277
return result;
12771278
}
12781279
default:

0 commit comments

Comments
 (0)