Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.

Exception During Initialization (Windows) #87

Open
m4dc4p opened this issue Jan 18, 2018 · 3 comments
Open

Exception During Initialization (Windows) #87

m4dc4p opened this issue Jan 18, 2018 · 3 comments

Comments

@m4dc4p
Copy link

m4dc4p commented Jan 18, 2018

Kalium with libsodium 1.0.15 & 1.0.16 is broken, due to incorrect string handling in jnr-ffi.

As a result, sodium_lib_version returns "1.0.15\0\0xsalsa20", and checkVersion fails because it tries to convert 15\0\0xsalsa20 to an integer.

Short of fixing jnr-ffi, Kalium could update its regex when parsing the input string to avoid this bug.

@m4dc4p
Copy link
Author

m4dc4p commented Jan 18, 2018

The exception thrown is:

Exception in thread "main" java.lang.NumberFormatException: For input string: "16  xsalsa20"
at java.lang.NumberFormatException.forInputString(Unknown Source)
 at java.lang.Integer.parseInt(Unknown Source)
 at java.lang.Integer.<init>(Unknown Sour
 at org.abstractj.kalium.NaCl.checkVersion(NaCl.java:66)
 at org.abstractj.kalium.NaCl.sodium(NaCl.java:30)

Here is a short test program that demonstrates the problem (requires using 1.0.16 on Windows). Assumes jUnit:

import org.abstractj.kalium.NaCl;
import org.abstractj.kalium.NaCl.Sodium;
import org.junit.*;
import static org.junit.Assert.*;

class Test {
  @Test
  public void testSodiumVersion() {
    Sodium x = jnr.ffi.LibraryLoader.create(Sodium.class).load("libsodium");
    assertEquals(x.sodium_version_string(), "1.0.16");
  }
}

@m4dc4p
Copy link
Author

m4dc4p commented Jan 18, 2018

Can be fixed by adding an @Encoding annotation (from jnr-ffi) to the method:

        @Encoding("US-ASCII")
        String sodium_version_string();

m4dc4p pushed a commit to m4dc4p/kalium that referenced this issue Jan 19, 2018
Added an annotation to the sodium_version_string() method so it
is always read as an ASCII string. Avoids some issues on Windows
with reading multi-byte strings by default.

Added a test to make sure version string matches the expected
pattern.
@Amraneze
Copy link

@m4dc4p Works like a charm. It fixed an issue with parsing a Json response from C/C++ library

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants