sigsegv compilation error of simple kotlin code

275 Views Asked by At

Following code generates error which looks like an error in compiler, its has nothing to do with the kotlin code, or maybe I am missing something? I have JDK error on macos and some more informative error (like pasted at the end of this post) from online compiler. How can I fix it?

Code: https://pl.kotl.in/CfdXHeDyn

import kotlin.math.pow

fun calculateNumberOfContainers(data: String): Int {
    val containerSizes = mutableListOf<Int>()
    data.lines().forEach { containerSizes.add(it.toInt()) }
    
    var foundCorrect = 0
    val maxSubSets = (2.0).pow(20).toULong()
    
    for (n in 0UL until maxSubSets) {
        var total = 0
    
        for (k in 0 until 20) {
            val mask = 1UL shl k
            if ((mask and n) != 0UL) {
                total += containerSizes[k]
                if (total > 150)
                    break
            }
        }
        if (total == 150)
            foundCorrect++
    }
    return foundCorrect
}

fun runcode() {
    val data = 
            """
33
14
18
20
45
35
16
35
1
13
18
13
50
44
48
6
24
41
30
42
        """.trimIndent()
    calculateNumberOfContainers(data)
}

fun main() {
    runcode()
}

Error:

Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unexpected character ('#' (code 35)): was expecting double-quote to start field name
 at [Source: (String)"{#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007fc646ef2eba, pid=1900, tid=0x00007fc6307f7700
#
# JRE version: OpenJDK Runtime Environment (8.0_201-b09) (build 1.8.0_201-b09)
# Java VM: OpenJDK 64-Bit Server VM (25.201-b09 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.so+0x382eba]
#
# Core dump written. Default location: /var/task/core or core.1900
#
# An error report file with more information is saved as:
# /tmp/"[truncated 195 chars]; line: 1, column: 3]
 at com.fasterxml.jackson.core.JsonParser._constructError (JsonParser.java:1851) 
 at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError (ParserMinimalBase.java:707) 
 at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar (ParserMinimalBase.java:632) 

Update:

it works with 1.4.30, fails with 1.5.21, so its a bug in compiler

1

There are 1 best solutions below

0
mike On

I changed from Unsigned Long to Unsigned Int and problem went away, code after changes looks as follows:

https://pl.kotl.in/XedacixZK

import kotlin.math.pow

fun calculateNumberOfContainers(data: String): Int {
    val containerSizes = mutableListOf<Int>()
    data.lines().forEach { containerSizes.add(it.toInt()) }
    
    var foundCorrect = 0
    val maxSubSets = (2.0).pow(20).toUInt()
    
    for (n in 0U until maxSubSets) {
        var total = 0
    
        for (k in 0 until 20) {
            val mask = 1U shl k
            if ((mask and n) != 0U) {
                total += containerSizes[k]
                if (total > 150)
                    break
            }
        }
        if (total == 150)
            foundCorrect++
    }
    return foundCorrect
}

fun runcode() {
    val data = 
            """
33
14
18
20
45
35
16
35
1
13
18
13
50
44
48
6
24
41
30
42
        """.trimIndent()
    print(calculateNumberOfContainers(data))
}

fun main() {
    runcode()
}