Skip to content

Bug Report: String Passing Between Node.js and C Fails, Returning Garbage Values #542

@RohanKrMahato

Description

@RohanKrMahato

🐛 Bug Report

Description:

When trying to pass a string from Node.js to C, the string does not get passed correctly as an argument. Instead, a garbage value is received in C. Similarly, when returning a string from C to Node.js, the returned value is also a garbage value, not the expected string.

Note: When passing a single character from Node.js to C ( char ), it works correctly. Likewise, when a character is returned from C, Node.js receives the corresponding ASCII value.


Expected Behavior:

  • Strings passed from Node.js to C should be properly received as arguments.
  • Strings returned from C to Node.js should be properly returned and not as garbage values.

Current Behavior:

  • Strings passed from Node.js to C result in garbage values instead of valid strings.
  • Strings returned from C to Node.js also result in garbage values instead of valid strings.

Steps to Reproduce:

1. Testing the Return Value from C to Node.js

Image

C file (textpro.c):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char* process_text() {
    char* input = "hello";
    return input;
}

Node.js file (testing.js):

const { metacall_load_from_file, metacall } = require('metacall');
metacall_load_from_file('c', ['textpro.c']);

console.log(metacall('process_text'));

Steps:

  1. Build and install Metacall, ensuring that the C loader is enabled.
  2. Write the C file textpro.c and Node.js file testing.js as shown above.
  3. Export the necessary script and library environment path.
  4. Run the script with metacallcli testing.js.
  5. Observe the garbage value returned instead of the expected string.

2. Testing Argument Passing from Node.js to C

Image

C file (textpro.c):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void process_text(char* input) {
    printf("%s", input);
}

Node.js file (testing.js):

const { metacall_load_from_file, metacall } = require('metacall');
metacall_load_from_file('c', ['textpro.c']);

console.log(metacall('process_text', 'test_test'));

Steps:

  1. Build and install Metacall, ensuring that the C loader is enabled.
  2. Write the C file textpro.c and Node.js file testing.js as shown above.
  3. Export the necessary script and library environment path.
  4. Run the script with metacallcli testing.js.
  5. Observe a segmentation fault (due to the invalid memory location being used by printf).

Conclusion:

  • When passing a string from Node.js to C, the string is not properly passed, resulting in an invalid or garbage value.
  • When returning a string from C to Node.js, the returned value is not a string, but a garbage value instead.
  • These issues appear when passing strings, but single characters work as expected.

Activity

viferga

viferga commented on Jul 10, 2025

@viferga
Member

@RohanKrMahato thank you so much for reporting, I solved the issue already:
1d14941
2cf32ca

I used your tests as a base for making it.

RohanKrMahato

RohanKrMahato commented on Jul 11, 2025

@RohanKrMahato
Author

@viferga I’m happy I could help you. I’ll be returning and contributing actively to Metacall starting in September.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @viferga@RohanKrMahato

        Issue actions

          Bug Report: String Passing Between Node.js and C Fails, Returning Garbage Values · Issue #542 · metacall/core