Permalink
Browse files

Fix crasher in prim_file:internal_name2native

This BIF needs a redo

1. Figure out how it is supposed to work
2. Make it so.

It seems that this should be pretty much like
iolist_to_binary, except it also needs to 
encode the file name according to a local
os-specific encoding.
  • Loading branch information...
1 parent 2e16819 commit 0978e4acf51279aba1dd01b2d93c4d1e6668900a @krestenkrab krestenkrab committed Jun 5, 2012
Showing with 35 additions and 1 deletion.
  1. +35 −1 src/main/java/erjang/m/prim_file/Native.java
@@ -1,15 +1,20 @@
package erjang.m.prim_file;
+import java.io.IOException;
+import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import erjang.BIF;
+import erjang.CharCollector;
import erjang.EBinary;
import erjang.ENative;
import erjang.EObject;
import erjang.ERT;
import erjang.ESeq;
+import erjang.ESmall;
import erjang.EString;
+import erjang.driver.IO;
public class Native extends ENative
{
@@ -29,12 +34,41 @@ else if (arg.testBinary() != null) {
return new EBinary(outbytes);
}
else {
+ /*
+ try {
+ StringBuilder output = new StringBuilder();
+ CharCollector out = new CharCollector(IO.ISO_LATIN_1, output);
+ arg.collectCharList(out);
+ out.addInteger(0);
+ out.end();
+ return new EBinary(output.toString().getBytes(IO.ISO_LATIN_1));
+ } catch (Exception e) {
+ throw ERT.badarg();
+ }
+ */
ESeq input = arg.testSeq();
List<Byte> bytes = new ArrayList<Byte>();
byte[] out;
+ ESeq all = input;
+
while (!input.isNil()) {
- Integer el = input.head().testSmall().intValue();
+ EObject first = input.head();
+ ESmall small = first.testSmall();
+ if (small == null) {
+ EBinary bin = first.testBinary();
+ if (bin == null) {
+ // it doesn't look like an iolist to me...
+ throw ERT.badarg();
+ } else {
+ for (int i = 0; i < bin.byteSize(); i++) {
+ bytes.add(bin.byteAt(i));
+ }
+ input = input.tail();
+ continue;
+ }
+ }
+ Integer el = small.intValue();
bytes.add(el.byteValue());
input = input.tail();
}

0 comments on commit 0978e4a

Please sign in to comment.