Skip to content

Commit

Permalink
8322218: Better escaping of single and double quotes in annotation to…
Browse files Browse the repository at this point in the history
…String() results

Reviewed-by: mchung
  • Loading branch information
jddarcy committed Feb 6, 2024
1 parent 0f5f3c9 commit 1797efd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -271,23 +271,29 @@ private static String toSourceString(double d) {
private static String toSourceString(char c) {
StringBuilder sb = new StringBuilder(4);
sb.append('\'');
sb.append(quote(c));
sb.append(quote(c, true));
return sb.append('\'') .toString();
}

/**
* Escapes a character if it has an escape sequence or is
* non-printable ASCII. Leaves non-ASCII characters alone.
*/
private static String quote(char ch) {
private static String quote(char ch, boolean charContext) {
/*
* In a char context, single quote (') must be escaped and
* double quote (") need not be escaped. In a non-char
* context, in other words a string context, the reverse is
* true.
*/
switch (ch) {
case '\b': return "\\b";
case '\f': return "\\f";
case '\n': return "\\n";
case '\r': return "\\r";
case '\t': return "\\t";
case '\'': return "\\'";
case '\"': return "\\\"";
case '\'': return (charContext ? "\\'" : "'");
case '\"': return (charContext ? "\"" : "\\\"");
case '\\': return "\\\\";
default:
return (isPrintableAscii(ch))
Expand Down Expand Up @@ -323,7 +329,7 @@ private static String toSourceString(String s) {
StringBuilder sb = new StringBuilder();
sb.append('"');
for (int i = 0; i < s.length(); i++) {
sb.append(quote(s.charAt(i)));
sb.append(quote(s.charAt(i), false));
}
sb.append('"');
return sb.toString();
Expand Down
12 changes: 6 additions & 6 deletions test/jdk/java/lang/annotation/AnnotationToStringTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,7 +23,7 @@

/*
* @test
* @bug 8162817 8168921
* @bug 8162817 8168921 8322218
* @summary Test of toString on normal annotations
*/

Expand Down Expand Up @@ -192,8 +192,8 @@ static class ArrayAnnotationHost {
public short[] f4;

@ExpectedString(
"@CharArray({'a', 'b', 'c', '\\''})")
@CharArray({'a', 'b', 'c', '\''})
"@CharArray({'a', 'b', 'c', '\\'', '\"'})")
@CharArray({'a', 'b', 'c', '\'', '"'})
public char[] f5;

@ExpectedString(
Expand All @@ -209,8 +209,8 @@ static class ArrayAnnotationHost {
public long[] f7;

@ExpectedString(
"@StringArray({\"A\", \"B\", \"C\", \"\\\"Quote\\\"\"})")
@StringArray({"A", "B", "C", "\"Quote\""})
"@StringArray({\"A\", \"B\", \"C\", \"\\\"Quote\\\"\", \"'\", \"\\\"\"})")
@StringArray({"A", "B", "C", "\"Quote\"", "'", "\""})
public String[] f8;

@ExpectedString(
Expand Down

1 comment on commit 1797efd

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.