-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Properly handle index-out-of-bounds on JS #537
Changes from all commits
cb92b27
f95d31e
6e59435
e0cb94e
63bf0cc
b8c70f7
c9bf5a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2012 Typelevel | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package org.typelevel.jawn | ||
|
||
private object Platform { | ||
final val isJvm = false | ||
final val isJs = true | ||
final val isNative = false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2012 Typelevel | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package org.typelevel.jawn | ||
|
||
private object Platform { | ||
final val isJvm = true | ||
final val isJs = false | ||
final val isNative = false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2012 Typelevel | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package org.typelevel.jawn | ||
|
||
private object Platform { | ||
final val isJvm = false | ||
final val isJs = false | ||
final val isNative = true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,11 +40,29 @@ final class ByteArrayParser[J](src: Array[Byte]) extends SyncParser[J] with Byte | |
context: FContext[J], | ||
stack: List[FContext[J]] | ||
): Unit = {} | ||
final protected[this] def byte(i: Int): Byte = src(i) | ||
final protected[this] def at(i: Int): Char = src(i).toChar | ||
|
||
final protected[this] def at(i: Int, k: Int): CharSequence = | ||
final protected[this] def byte(i: Int): Byte = { | ||
if (Platform.isJs) { | ||
if (i < 0 || i >= src.length) throw new ArrayIndexOutOfBoundsException | ||
} | ||
src(i) | ||
} | ||
Comment on lines
+44
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided that platform-specific guards would be the best way to implement this. Because The other option is that we split these sources across platform but I think that is worse for readability. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
til. Is this eliding part of the Scala 2 inliner / Scala 3 Its ifs the former maybe it makes sense to explicitly inline using In any case maybe it makes sense to add this as a brief comment to all of the |
||
|
||
final protected[this] def at(i: Int): Char = { | ||
if (Platform.isJs) { | ||
if (i < 0 || i >= src.length) throw new ArrayIndexOutOfBoundsException | ||
} | ||
src(i).toChar | ||
} | ||
|
||
final protected[this] def at(i: Int, k: Int): CharSequence = { | ||
if (Platform.isJs) { | ||
if (i < 0 || i >= src.length) throw new ArrayIndexOutOfBoundsException | ||
if (k < 0 || k > src.length) throw new ArrayIndexOutOfBoundsException | ||
if (i > k) throw new ArrayIndexOutOfBoundsException | ||
} | ||
new String(src, i, k - i, utf8) | ||
} | ||
|
||
final protected[this] def atEof(i: Int): Boolean = i >= src.length | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.4.20") | ||
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.4") | ||
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.1") | ||
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.1") | ||
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.1") | ||
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.12") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,9 @@ package object util { | |
var inverseSign: Long = -1L | ||
var i: Int = 0 | ||
|
||
if (cs.length == 0) | ||
throw InvalidLong(cs.toString) | ||
Comment on lines
+48
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an existing bug that also applies to JVM and Native. |
||
|
||
if (cs.charAt(0) == '-') { | ||
inverseSign = 1L | ||
i = 1 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although in production mode these are undefined behaviors, in development mode Scala.js raises fatal errors to help detect these issues. As a workaround, we were previously disabling this to silence the fatal errors (my bad 😅 ).