Skip to content
This repository
Browse code

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...
commit 0978e4acf51279aba1dd01b2d93c4d1e6668900a 1 parent 2e16819
Kresten Krab Thorup krestenkrab authored

Showing 1 changed file with 35 additions and 1 deletion. Show diff stats Hide diff stats

  1. +35 1 src/main/java/erjang/m/prim_file/Native.java
36 src/main/java/erjang/m/prim_file/Native.java
... ... @@ -1,15 +1,20 @@
1 1 package erjang.m.prim_file;
2 2
  3 +import java.io.IOException;
  4 +import java.nio.charset.Charset;
3 5 import java.util.ArrayList;
4 6 import java.util.List;
5 7
6 8 import erjang.BIF;
  9 +import erjang.CharCollector;
7 10 import erjang.EBinary;
8 11 import erjang.ENative;
9 12 import erjang.EObject;
10 13 import erjang.ERT;
11 14 import erjang.ESeq;
  15 +import erjang.ESmall;
12 16 import erjang.EString;
  17 +import erjang.driver.IO;
13 18
14 19 public class Native extends ENative
15 20 {
@@ -29,12 +34,41 @@ else if (arg.testBinary() != null) {
29 34 return new EBinary(outbytes);
30 35 }
31 36 else {
  37 + /*
  38 + try {
  39 + StringBuilder output = new StringBuilder();
  40 + CharCollector out = new CharCollector(IO.ISO_LATIN_1, output);
  41 + arg.collectCharList(out);
  42 + out.addInteger(0);
  43 + out.end();
  44 + return new EBinary(output.toString().getBytes(IO.ISO_LATIN_1));
  45 + } catch (Exception e) {
  46 + throw ERT.badarg();
  47 + }
  48 + */
32 49 ESeq input = arg.testSeq();
33 50 List<Byte> bytes = new ArrayList<Byte>();
34 51 byte[] out;
35 52
  53 + ESeq all = input;
  54 +
36 55 while (!input.isNil()) {
37   - Integer el = input.head().testSmall().intValue();
  56 + EObject first = input.head();
  57 + ESmall small = first.testSmall();
  58 + if (small == null) {
  59 + EBinary bin = first.testBinary();
  60 + if (bin == null) {
  61 + // it doesn't look like an iolist to me...
  62 + throw ERT.badarg();
  63 + } else {
  64 + for (int i = 0; i < bin.byteSize(); i++) {
  65 + bytes.add(bin.byteAt(i));
  66 + }
  67 + input = input.tail();
  68 + continue;
  69 + }
  70 + }
  71 + Integer el = small.intValue();
38 72 bytes.add(el.byteValue());
39 73 input = input.tail();
40 74 }

0 comments on commit 0978e4a

Please sign in to comment.
Something went wrong with that request. Please try again.