Skip to content

Commit

Permalink
Improve the integer overflow servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tamura committed Feb 25, 2017
1 parent fefa413 commit 4d33cb2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Locale;

import javax.servlet.ServletException;
Expand All @@ -23,21 +24,28 @@ public class IntegerOverflowServlet extends HttpServlet {
private static Logger log = LoggerFactory.getLogger(IntegerOverflowServlet.class);

protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
int days = -1;
int hours = -1;
int times = -1;
BigDecimal thickness = null;
BigDecimal thicknessM = null;
BigDecimal thicknessKm = null;
PrintWriter writer = null;
try {
Locale locale = req.getLocale();
String strDays = req.getParameter("days");
if (strDays != null) {
String strTimes = req.getParameter("times");
if (strTimes != null) {
try {
days = Integer.parseInt(strDays);
times = Integer.parseInt(strTimes);
} catch (NumberFormatException e) {
// ignore
}
if (days >= 0) {
// days * 24 => hours
hours = days * 24;
int multipleNumber = 1;
if (times >= 0) {
for (int i = 0; i < times; i++) {
multipleNumber = multipleNumber * 2;
}
thickness = new BigDecimal(multipleNumber).divide(new BigDecimal(10)); // mm
thicknessM = thickness.divide(new BigDecimal(1000)); // m
thicknessKm = thicknessM.divide(new BigDecimal(1000)); // km
}
}

Expand All @@ -46,22 +54,24 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append(MessageUtils.getMsg("msg.enter.positive.number", locale));
bodyHtml.append("<br>");
bodyHtml.append("<br>");
if (days >= 0) {
bodyHtml.append("<input type=\"text\" name=\"days\" size=\"8\" maxlength=\"8\" value=" + strDays + ">");
if (times >= 0) {
bodyHtml.append(
"<input type=\"text\" name=\"times\" size=\"2\" maxlength=\"2\" value=" + strTimes + ">");
} else {
bodyHtml.append("<input type=\"text\" name=\"days\" size=\"8\" maxlength=\"8\">");
bodyHtml.append("<input type=\"text\" name=\"times\" size=\"2\" maxlength=\"2\">");
}
bodyHtml.append(" " + MessageUtils.getMsg("label.multiplication.sign", locale) + " 24 = ");
if (days >= 0) {
bodyHtml.append(hours + " ");
bodyHtml.append(MessageUtils.getMsg("label.times", locale) + " : ");
if (times >= 0) {
bodyHtml.append(thickness + " mm");
bodyHtml.append(thicknessM.intValue() >= 1 && thicknessKm.intValue() < 1 ? " = " + thicknessM + " m" : "");
bodyHtml.append(thicknessKm.intValue() >= 1 ? " = " + thicknessKm + " km" : "");
}
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.positive.number",
new String[] { String.valueOf(Integer.MAX_VALUE / 24) }, locale));
bodyHtml.append(MessageUtils.getMsg("msg.note.positive.number", locale));
bodyHtml.append("</form>");

HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.integer.overflow.page", locale),
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ label.desc=desc
label.execution.result=Execution Result:
label.json.string=JSON String
label.name=Name
label.multiplication.sign=*
label.obelus=/
label.order=Order
label.password=Password
label.reversed.name=Reversed Name
label.string=String
label.submit=Submit
label.times=times
label.update=Update
msg.c.heap.space.leak.occur=Memory leak occurs in C heap space every time you load this page.<BR><BR> \
<span class="glyphicon glyphicon-info-sign"></span>&nbsp; If keeping on loading this page, OutOfMemoryError is finally thrown.
Expand All @@ -27,7 +27,7 @@ msg.deadlock.occurs=A lock could not be obtained due to a deadlock.
msg.enter.json.string=Please enter JSON string.
msg.enter.name.and.passwd=If you enter your name and password, then your secret number is shown.
msg.enter.name=Please enter your name.
msg.enter.positive.number=Please enter a positive number.
msg.enter.positive.number=How many times would you have to fold a piece of paper (thickness 0.1mm) for it to be thick enough to reach the moon (384,400 km)?
msg.enter.decimal.value=Please enter a decimal number less than 1.
msg.enter.word=Please enter a string.
msg.error.user.not.exist=User does not exist or password does not match.
Expand All @@ -51,7 +51,7 @@ Truncation error occurs if you enter 3 or 7 or 9.
msg.note.enter.decimal.value=<span class="glyphicon glyphicon-info-sign"></span>&nbsp; \
Loss of trailing digits occurs if you enter 0.0000000000000001.
msg.note.positive.number=<span class="glyphicon glyphicon-info-sign"></span>&nbsp; \
Integer overflow occurs if you enter a large number greater than {0}.
Integer overflow occurs if you enter a number greater than or equal to 31. FYI, the answer is ... you can see it if you fix this bug.
msg.note.slow.regular.expression=<span class="glyphicon glyphicon-info-sign"></span>&nbsp; \
If you set string to aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\u3042, parse processing will take several tens of seconds<br> \
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you set string to aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\u3042, then ...
Expand All @@ -77,7 +77,7 @@ msg.valid.json=Valid JSON!
msg.warn.select.asc.or.desc=Please select "asc" or "desc" and click the Update button.
msg.warn.enter.name.and.passwd=Please enter your name and password.
title.endless.waiting.page=Execute batch
title.integer.overflow.page=Convert days to hours
title.integer.overflow.page=The distance from Earth to the moon
title.loss.of.trailing.digits.page=Decimal addition
title.parse.json=Parse JSON
title.round.off.error.page=Easy subtraction
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/messages_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ label.desc=\u964d\u9806
label.execution.result=\u5b9f\u884c\u7d50\u679c:
label.json.string=JSON\u6587\u5b57\u5217
label.name=\u540d\u524d
label.multiplication.sign=\u00d7
label.obelus=\u00f7
label.order=\u9806\u5e8f
label.password=\u30d1\u30b9\u30ef\u30fc\u30c9
label.reversed.name=\u9006\u8ee2\u3057\u305f\u540d\u524d
label.submit=\u9001\u4fe1
label.string=\u6587\u5b57\u5217
label.times=\u56de
label.update=\u66f4\u65b0
msg.c.heap.space.leak.occur=\u3053\u306e\u30da\u30fc\u30b8\u3092\u8aad\u307f\u8fbc\u3080\u305f\u3073\u306b\u3001C\u30d2\u30fc\u30d7\u9818\u57df\u306e\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u304c\u767a\u751f\u3057\u307e\u3059\u3002<BR><BR> \
<span class="glyphicon glyphicon-info-sign"></span>&nbsp; \u753b\u9762\u3092\u30ed\u30fc\u30c9\u3057\u7d9a\u3051\u308b\u3068\u3001\u6700\u7d42\u7684\u306bOutOfMemoryError\u304c\u30b9\u30ed\u30fc\u3055\u308c\u307e\u3059\u3002
Expand All @@ -27,7 +27,7 @@ msg.deadlock.occurs=\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306b\u3088\u308a\u30ed
msg.enter.json.string=JSON\u6587\u5b57\u5217\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
msg.enter.name.and.passwd=\u540d\u524d\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u6697\u8a3c\u756a\u53f7\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002
msg.enter.name=\u540d\u524d\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
msg.enter.positive.number=\u6b63\u306e\u6574\u6570\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
msg.enter.positive.number=0.1mm\u306e\u539a\u3055\u306e\u7d19\u3092\u4f55\u56de\u6298\u308a\u305f\u305f\u3080\u3068\u3001\u5730\u7403\u304b\u3089\u6708\u306e\u8ddd\u96e2(384,400 km)\u306b\u5230\u9054\u3059\u308b\u3067\u3057\u3087\u3046\u304b\uff1f
msg.enter.decimal.value=1\u672a\u6e80\u306e\u5c0f\u6570\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
msg.enter.word=\u6587\u5b57\u5217\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
msg.error.user.not.exist=\u30e6\u30fc\u30b6\u30fc\u304c\u5b58\u5728\u3057\u306a\u3044\u304b\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u4e00\u81f4\u3057\u307e\u305b\u3093\u3002
Expand All @@ -49,7 +49,7 @@ msg.note.enter.specific.nembers=<span class="glyphicon glyphicon-info-sign"></sp
msg.note.enter.decimal.value=<span class="glyphicon glyphicon-info-sign"></span>&nbsp; \
0.0000000000000001\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u60c5\u5831\u6b20\u843d\u304c\u767a\u751f\u3057\u307e\u3059\u3002
msg.note.positive.number=<span class="glyphicon glyphicon-info-sign"></span>&nbsp; \
{0}\u3088\u308a\u5927\u304d\u3044\u6570\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u6574\u6570\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002
31\u4ee5\u4e0a\u306e\u6570\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u6574\u6570\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002\u3061\u306a\u307f\u306b\u7b54\u3048\u306f... \u3053\u306e\u30d0\u30b0\u3092\u76f4\u305b\u3070\u5206\u304b\u308a\u307e\u3059\u3002
msg.note.sql.deadlock=<span class="glyphicon glyphicon-info-sign"></span>&nbsp; \
\u300c\u964d\u9806\u300d\u3092\u9078\u629e\u3057\u3066\u300c\u66f4\u65b0\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u76f4\u5f8c\u306b\u3001\u300c\u6607\u9806\u300d\u3092\u9078\u629e\u3057\u3066\
\u300c\u66f4\u65b0\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u304c\u767a\u751f\u3057\u307e\u3059\u3002
Expand Down Expand Up @@ -77,7 +77,7 @@ msg.valid.json=\u6b63\u3057\u3044JSON\u6587\u5b57\u5217\u3067\u3059\u3002
msg.warn.select.asc.or.desc=\u300c\u6607\u9806\u300d\u307e\u305f\u306f\u300c\u964d\u9806\u300d\u3092\u9078\u629e\u3057\u3066\u3001\u66f4\u65b0\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u4e0b\u3055\u3044\u3002
msg.warn.enter.name.and.passwd=\u540d\u524d\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
title.endless.waiting.page=\u30d0\u30c3\u30c1\u306e\u5b9f\u884c
title.integer.overflow.page=\u65e5\u2192\u6642\u9593\u5909\u63db
title.integer.overflow.page=\u6708\u307e\u3067\u306e\u8ddd\u96e2
title.loss.of.trailing.digits.page=\u5c0f\u6570\u306e\u8db3\u3057\u7b97
title.parse.json=JSON\u306e\u89e3\u6790
title.round.off.error.page=\u7c21\u5358\u306a\u5f15\u304d\u7b97
Expand Down

0 comments on commit 4d33cb2

Please sign in to comment.