core icon indicating copy to clipboard operation
core copied to clipboard

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

Open RohanKrMahato opened this issue 8 months ago • 0 comments

🐛 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.

RohanKrMahato avatar Feb 07 '25 13:02 RohanKrMahato