Skip to content

Wrong output for ksc-json-output in case of relative paths under Windows. #507

Description

@ams-tschoening

I upgraded ksv to get support for opaque types and with it came a changed interaction with ksc, the new ksc-json-output is used now. This leads to compile time errors in ksv for me, because your generated JSON doesn't seem to handle paths and their different representation in Java under different OS correctly.

C:/[...]/visualizer_main.rb:68:in block (2 levels) in compile_formats': undefined method []' for nil:NilClass (NoMethodError)
from C:/Program Files (x86)/kaitai_struct/visualizer/lib/kaitai/struct/visualizer/visualizer_main.rb:64:in each' from C:/Program Files (x86)/kaitai_struct/visualizer/lib/kaitai/struct/visualizer/visualizer_main.rb:64:in each_with_index'
from C:/Program Files (x86)/kaitai_struct/visualizer/lib/kaitai/struct/visualizer/visualizer_main.rb:64:in `block in compile_formats'

      fns.each_with_index { |fn, idx|
        puts "... processing #{fn} #{idx}"

        log_fn = log[fn]
        if log_fn['errors']
{"record\fmt_clt_recs.ksy": {"firstSpecName": "fmt_clt_recs","output": {"ruby": {"fmt_clt_recs": {"topLevelName": "FmtCltRecs","files": [{"fileName": "fmt_clt_recs.rb"}]},"fmt_oms_rec": {"topLevelName": "FmtOmsRec","files": [{"fileName": "fmt_oms_rec.rb"}]}}}},"record\fmt_oms_rec.ksy": {"firstSpecName": "fmt_oms_rec","output": {"ruby": {"fmt_oms_rec": {"topLevelName": "FmtOmsRec","files": [{"fileName": "fmt_oms_rec.rb"}]}}}}}

There are two problems here:

  1. / to \

I use a command line similar to the following in Eclipse to start ksv:

[...]ksv "${workspace_loc}/[...]/data.bin" "record/fmt_clt_recs.ksy" "record/fmt_oms_rec.ksy"

With the current ksc this leads to \ instead of / in the generated JSON object, because Java internally uses a platform dependent path separator most of the times. The problem is that ksv sees / itself and can't find its own paths in the JSON anymore. I used / in favour of \ simply because I had trouble using \ in the past and / seems to be more common in Ruby, this would work on non-Windows etc.

  1. Improper escaping of \

\ needs to be escaped in JSON, that's why changing / to \ in my invocation above doesn't fix the problem: JSON contains \f in the end instead of \\f which is what Ruby internally would have as the String key.

In my opinion, the best thing to do is simply to not generate \ in JSON at all, so that only one normalized path separator is used across platforms and languages:

  // FIXME: do proper string handling
  def stringToJson(str: String): String =
    "\"%s\"".format(str.replaceAll("\\\\", "/"))

This is somewhat hacky as well of course, but fixes the escape problem and makes interacting with ksv easier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions