diff --git a/indra/newview/fslslpreproc.cpp b/indra/newview/fslslpreproc.cpp
index b2e815df0a..12f46fa2eb 100644
--- a/indra/newview/fslslpreproc.cpp
+++ b/indra/newview/fslslpreproc.cpp
@@ -1540,6 +1540,22 @@ void FSLSLPreprocessor::start_process()
}
}
}
+ else
+ {
+ // FIRE-31718: Preprocessor crashes viewer on recursive #include
+
+ // Truncate the resulting preprocessed script to something the text field can handle without
+ // freezing for so long the viewer disconnects. The usual script source code limit is 64kB so
+ // let's play it safe and allow twice as much here. The script is most likely already unusable
+ // at this point due to the preprocessor bailing out with an error earlier, so a truncated
+ // version doesn't hurt more than it already did.
+ if (output.size() > 128 * 1024)
+ {
+ output.resize(128 * 1024);
+ display_error(LLTrans::getString("fs_preprocessor_truncated"));
+ }
+ }
+
if (!errored)
{
if (preprocessor_enabled && use_compression)
diff --git a/indra/newview/skins/default/xui/en/strings.xml b/indra/newview/skins/default/xui/en/strings.xml
index 722bc66f90..4ecb7e2744 100644
--- a/indra/newview/skins/default/xui/en/strings.xml
+++ b/indra/newview/skins/default/xui/en/strings.xml
@@ -3153,6 +3153,7 @@ Your current position: [AVATAR_POS]
Caching completed for '[FILENAME]'
Error: script named '[FILENAME]' isn't safe to copy to the filesystem. This include will fail.
Error caching included file '[FILENAME]'
+ Warning: Preprocessor output truncated due to excessive script text size. This script will most likely not work.