Skip to content

Integer to string cast in string concatenation generates invalid C code causing segfault #96

@DeanHnter

Description

@DeanHnter

Describe the bug
When using cast("str", integer_value) within string concatenation expressions, the Yaksha transpiler generates
invalid C code that casts integer values directly to string pointers instead of calling proper integer-to-string conversion functions. This results in segmentation faults when the string manipulation functions attempt to use these invalid pointers.

To Reproduce
Please provide minimal code that produce bug in a self contained file.

  import libs.console

  struct TestPoint:
      x: i32
      y: i32

  def main() -> int:
      point: TestPoint
      point.x = 100
      point.y = 200

      # This line causes a segfault due to invalid C code generation
      println("Point: x=" + cast("str", point.x) + ", y=" + cast("str", point.y))

      return 0

Expected behaviour
The code should compile to valid C that properly converts integers to strings and concatenate them, producing output like:

Point: x=100, y=200

Desktop (please complete the following information):

  • OS: macOS
  • OS Flavour: Darwin
  • Version: 24.4.0
  • CPU: ARM64

Additional context
The generated C code shows the problem:

  yk__sds t__0 = yk__concat_lit_sds("Point: x=", 9, ((yk__sds)yy__point.x));
  yk__sds t__1 = yk__concat_sds_lit(t__0, ", y=", 4);
  yk__sds t__2 = yk__sdscatsds(yk__sdsdup(t__1), ((yk__sds)yy__point.y));

The issue is ((yk__sds)yy__point.x) and ((yk__sds)yy__point.y) - these cast integer values (100, 200) directly to string pointers, creating invalid memory addresses. The string functions then try to operate on these invalid pointers, causing segfaults.

The transpiler should generate calls to integer-to-string conversion functions instead of direct pointer casts. Using print() instead of println() works fine, suggesting the issue is specifically with string concatenation handling of integer-to-string casts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions