Tweaked code to avoid overflow entirely.

pull/134/head
Project Nayuki 6 years ago
parent e2050c3e03
commit 7fa8becaf7

@ -206,14 +206,12 @@ public final class QrCode {
public String toSvgString(int border) { public String toSvgString(int border) {
if (border < 0) if (border < 0)
throw new IllegalArgumentException("Border must be non-negative"); throw new IllegalArgumentException("Border must be non-negative");
if (size + border * 2L > Integer.MAX_VALUE) long brd = border;
throw new IllegalArgumentException("Border too large");
StringBuilder sb = new StringBuilder() StringBuilder sb = new StringBuilder()
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n") .append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n")
.append(String.format("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 %1$d %1$d\" stroke=\"none\">\n", .append(String.format("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 %1$d %1$d\" stroke=\"none\">\n",
size + border * 2)) size + brd * 2))
.append("\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n") .append("\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n")
.append("\t<path d=\""); .append("\t<path d=\"");
boolean head = true; boolean head = true;
@ -224,7 +222,7 @@ public final class QrCode {
head = false; head = false;
else else
sb.append(" "); sb.append(" ");
sb.append(String.format("M%d,%dh1v1h-1z", x + border, y + border)); sb.append(String.format("M%d,%dh1v1h-1z", x + brd, y + brd));
} }
} }
} }

Loading…
Cancel
Save