Skip to content
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

Regex.unapplySeq not null safe #8787

Closed
scabug opened this issue Aug 8, 2014 · 2 comments
Closed

Regex.unapplySeq not null safe #8787

scabug opened this issue Aug 8, 2014 · 2 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Aug 8, 2014

NullPointerException occurs when Regex.unapplySeq is called with null.
See: https://groups.google.com/forum/#!topic/scala-language/_IAaYPUUL1s

object Main {

  def main(args: Array[String]): Unit = {
    val sdLst = List("10.12.95", null, "", "01.04.97", "1.4.97")

    for (sd <- sdLst; DMY(optYear) = sd; y <- optYear)
      yield y
  }
}

object DMY {
  val df = """(\d\d)\.(\d\d)\.(\d\d)""".r

  def unapply(s: String) = s match {
    case df(d, m, y) => Some(Some(d.toInt, m.toInt, y.toInt))
    case _ => Some(None)
  }
}

The error message:

Exception in thread "main" java.lang.NullPointerException
	at java.util.regex.Matcher.getTextLength(Matcher.java:1234)
	at java.util.regex.Matcher.reset(Matcher.java:308)
	at java.util.regex.Matcher.<init>(Matcher.java:228)
	at java.util.regex.Pattern.matcher(Pattern.java:1088)
	at scala.util.matching.Regex.unapplySeq(Regex.scala:206)
	at course.DMY$.unapply(Main.scala:22)
	at course.Main$$anonfun$main$1.apply(Main.scala:13)
	at course.Main$$anonfun$main$1.apply(Main.scala:13)
	at scala.collection.immutable.List.map(List.scala:276)
	at course.Main$.main(Main.scala:13)
	at course.Main.main(Main.scala)
@scabug
Copy link
Author

scabug commented Aug 8, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8787?orig=1
Reporter: Stefano Di Martino (Stefano)
Affected Versions: 2.11.2

@scabug
Copy link
Author

scabug commented Aug 9, 2014

@som-snytt said:
scala/scala#3923

@scabug scabug closed this as completed Aug 19, 2014
@scabug scabug added this to the 2.11.3 milestone Apr 7, 2017
eed3si9n added a commit to eed3si9n/scala that referenced this issue Apr 1, 2018
Ref scala/bug#4364
This is a general fix for scala/bug#2241 and scala/bug#8787.

scala/bug#4364 raises the question of what should the pattern matcher do when encountering a `null` and an extractor pattern. As it stands, SLS does not say anything about `null` for extractor pattern, which leads:
- Pattern matcher will happily pass `null` into your extractor.
- I can write a `null`-matching extractor MyNull.
- Thus, all extractor authors must check for `null` if they would like to access the passed in argument `x`.

There are potentially two things we can do.
1. Be ok with the status quo. Clarify in SLS that all extractor authors must handle `null`.
2. Not be ok with the status quo. Change SLS and implementation such that `null` is treated as no match (empty).

I've already sent option 1 as scala#6374.
This implements option 2.
eed3si9n added a commit to eed3si9n/scala that referenced this issue Apr 1, 2018
Ref scala/bug#4364
This is a general fix for scala/bug#2241 and scala/bug#8787.

scala/bug#4364 raises the question of what should the pattern matcher do when encountering a `null` and an extractor pattern. As it stands, SLS does not say anything about `null` for extractor pattern, which leads:
- Pattern matcher will happily pass `null` into your extractor.
- I can write a `null`-matching extractor MyNull.
- Thus, all extractor authors must check for `null` if they would like to access the passed in argument `x`.

There are potentially two things we can do.
1. Be ok with the status quo. Clarify in SLS that all extractor authors must handle `null`.
2. Not be ok with the status quo. Change SLS and implementation such that `null` is treated as no match (empty).

I've already sent option 1 as scala#6374.
This implements option 2.
eed3si9n added a commit to eed3si9n/scala that referenced this issue Apr 12, 2018
Ref scala/bug#4364
This is a general fix for scala/bug#2241 and scala/bug#8787.

scala/bug#4364 raises the question of what should the pattern matcher do when encountering a `null` and an extractor pattern. As it stands, SLS does not say anything about `null` for extractor pattern, which leads:
- Pattern matcher will happily pass `null` into your extractor.
- I can write a `null`-matching extractor MyNull.
- Thus, all extractor authors must check for `null` if they would like to access the passed in argument `x`.

There are potentially two things we can do.
1. Be ok with the status quo. Clarify in SLS that all extractor authors must handle `null`.
2. Not be ok with the status quo. Change SLS and implementation such that `null` is treated as no match (empty).

I've already sent option 1 as scala#6374.
This implements option 2.
eed3si9n added a commit to eed3si9n/scala that referenced this issue May 4, 2018
Ref scala/bug#4364
This is a general fix for scala/bug#2241 and scala/bug#8787.

scala/bug#4364 raises the question of what should the pattern matcher do when encountering a `null` and an extractor pattern. As it stands, SLS does not say anything about `null` for extractor pattern, which leads:
- Pattern matcher will happily pass `null` into your extractor.
- I can write a `null`-matching extractor MyNull.
- Thus, all extractor authors must check for `null` if they would like to access the passed in argument `x`.

There are potentially two things we can do.
1. Be ok with the status quo. Clarify in SLS that all extractor authors must handle `null`.
2. Not be ok with the status quo. Change SLS and implementation such that `null` is treated as no match (empty).

I've already sent option 1 as scala#6374.
This implements option 2.
adriaanm pushed a commit to eed3si9n/scala that referenced this issue Jun 1, 2018
Until now, the spec did not say anything about `null` for extractor pattern, so that:
  - The pattern matcher would happily pass `null` into your extractor.
  - One could write a `null`-matching extractor `MyNull`.
  - But all extractor authors must consider `null` as a possible argument value.

No more! The pattern matcher inserts a non-`null` check before invoking an extractor,
so that you don't have to.

This is a general fix for scala/bug#2241, scala/bug#8787. See scala/bug#4364.
eed3si9n added a commit to eed3si9n/scala that referenced this issue Jun 2, 2018
Until now, the spec did not say anything about `null` for extractor pattern, so that:
  - The pattern matcher would happily pass `null` into your extractor.
  - One could write a `null`-matching extractor `MyNull`.
  - But all extractor authors must consider `null` as a possible argument value.

No more! The pattern matcher inserts a non-`null` check before invoking an extractor,
so that you don't have to.

This is a general fix for scala/bug#2241, scala/bug#8787. See scala/bug#4364.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants