Permalink
Browse files

Don't crash in StackTraceHelper.convertJsStackTrace

Summary:
Don't crash in `StackTraceHelper.convertJsStackTrace` on missing `lineNumber`

I've seen the native Android app crash upon displaying a stack trace because of this bug.

Incidental and unrelated change: Refactor `StackTraceHelper.formatFrameSource` to use `StringBuilder`
Closes #12920

Differential Revision: D4709273

fbshipit-source-id: 95b3eb303f259460e33d8ee32a2db652360c645d
  • Loading branch information...
petterh authored and facebook-github-bot committed Mar 14, 2017
1 parent 87137ca commit 957b55c769661edeee691109e45dc99a06f87bc4
Showing with 20 additions and 9 deletions.
  1. +20 −9 ReactAndroid/src/main/java/com/facebook/react/devsupport/StackTraceHelper.java
@@ -22,6 +22,9 @@
*/
public class StackTraceHelper {
public static final java.lang.String COLUMN_KEY = "column";
public static final java.lang.String LINE_NUMBER_KEY = "lineNumber";
/**
* Represents a generic entry in a stack trace, be it originally from JS or Java.
*/
@@ -101,10 +104,13 @@ public String getFileName() {
ReadableMap frame = stack.getMap(i);
String methodName = frame.getString("methodName");
String fileName = frame.getString("file");
int lineNumber = frame.getInt("lineNumber");
int lineNumber = -1;
if (frame.hasKey(LINE_NUMBER_KEY) && !frame.isNull(LINE_NUMBER_KEY)) {
lineNumber = frame.getInt(LINE_NUMBER_KEY);
}
int columnNumber = -1;
if (frame.hasKey("column") && !frame.isNull("column")) {
columnNumber = frame.getInt("column");
if (frame.hasKey(COLUMN_KEY) && !frame.isNull(COLUMN_KEY)) {
columnNumber = frame.getInt(COLUMN_KEY);
}
result[i] = new StackFrameImpl(fileName, methodName, lineNumber, columnNumber);
}
@@ -132,12 +138,17 @@ public String getFileName() {
* Format a {@link StackFrame} to a String (method name is not included).
*/
public static String formatFrameSource(StackFrame frame) {
String lineInfo = "";
final int column = frame.getColumn();
// If the column is 0, don't show it in red box.
final String columnString = column <= 0 ? "" : ":" + column;
lineInfo += frame.getFileName() + ":" + frame.getLine() + columnString;
return lineInfo;
StringBuilder lineInfo = new StringBuilder();
lineInfo.append(frame.getFileName());
final int line = frame.getLine();
if (line > 0) {
lineInfo.append(":").append(line);
final int column = frame.getColumn();
if (column > 0) {
lineInfo.append(":").append(column);
}
}
return lineInfo.toString();
}
/**

3 comments on commit 957b55c

@haggholm

This comment has been minimized.

Show comment
Hide comment
@haggholm

haggholm Apr 4, 2017

Thank you for fixing this. It gave me a bit of a start, as I was digging into the codebase to fix this as it’s a blocker for me; and imagine my surprise to find that (a) it was fixed in the latest pre-release, and (b) it was fixed by “petterh”.

-- Petter H[äggholm]

Thank you for fixing this. It gave me a bit of a start, as I was digging into the codebase to fix this as it’s a blocker for me; and imagine my surprise to find that (a) it was fixed in the latest pre-release, and (b) it was fixed by “petterh”.

-- Petter H[äggholm]

@petterh

This comment has been minimized.

Show comment
Hide comment
@petterh

petterh Apr 5, 2017

Contributor

:-)

Contributor

petterh replied Apr 5, 2017

:-)

@yanhuo2008

This comment has been minimized.

Show comment
Hide comment

:-)

Please sign in to comment.