Skip to content

Commit

Permalink
Fixup #449: C/C++/Java printer: handle null when escaping strings
Browse files Browse the repository at this point in the history
This is a precaution, should not happen unless there are other bugs
(e.g. the one introduced in d5ba1ac).
  • Loading branch information
andreasabel committed Jul 26, 2023
1 parent b4d755c commit 4e36945
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion source/src/BNFC/Backend/C/CFtoCPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ mkCFile cf groups = concat
[
"void bufEscapeS(const char *s)",
"{",
" while (*s) bufEscapeC(*s++);",
" if (s) while (*s) bufEscapeC(*s++);",
"}",
"void bufEscapeC(const char c)",
"{",
Expand Down
2 changes: 1 addition & 1 deletion source/src/BNFC/Backend/CPP/PrettyPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mkHFile useStl inPackage cf groups = unlines
"",
" void inline bufEscape(const char *s)",
" {",
" while (*s) bufEscape(*s++);",
" if (s) while (*s) bufEscape(*s++);",
" }",
"",
if useStl then render (nest 2 $ bufAppendString "bufEscape") else "",
Expand Down
1 change: 1 addition & 0 deletions source/src/BNFC/Backend/Java/CFtoJavaPrinter15.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ cf2JavaPrinter packageBase packageAbsyn cf =
" private static void printQuoted(String s) { render(\"\\\"\" + escape(s) + \"\\\"\"); }",
"",
" public static String escape(String s) {",
" if (s == null) return null;",
" return s.replace(\"\\\\\", \"\\\\\\\\\")",
" .replace(\"\\t\", \"\\\\t\")",
" .replace(\"\\b\", \"\\\\b\")",
Expand Down

0 comments on commit 4e36945

Please sign in to comment.