satindergrewal / lightning

c-lightning — a Lightning Network implementation in C
Other
0 stars 2 forks source link

Segmentation fault with "invoice-count" at starting of "lightningd" #5

Closed satindergrewal closed 3 years ago

satindergrewal commented 3 years ago

Just starting lightningd it dumps the backtrace data and gives Segmentation fault error:

root@pangea:~/grewal/lightning# ./lightningd/lightningd --help
invoice-count
lightningd: FATAL SIGNAL 11 (version chipsln.0.0.0)
0x563916cea65a send_backtrace
    common/daemon.c:38
0x563916cea6ed crashdump
    common/daemon.c:51
0x7fc2338bb03f ???
    ???:0
0x563916cb41a2 json_start
    lightningd/jsonrpc.c:600
0x563916cb4240 json_stream_success
    lightningd/jsonrpc.c:606
0x563916cb2753 json_invoice_count
    lightningd/invoice.c:1653
0x563916cb3b2a setup_command_usage
    lightningd/jsonrpc.c:1086
0x563916cb3bbd jsonrpc_command_add_perm
    lightningd/jsonrpc.c:1108
0x563916cb53c5 jsonrpc_setup
    lightningd/jsonrpc.c:1133
0x563916cb5ba7 new_lightningd
    lightningd/lightningd.c:240
0x563916cb65a3 main
    lightningd/lightningd.c:817
0x7fc23389dbf6 ???
    ???:0
0x563916ca4099 ???
    ???:0
0xffffffffffffffff ???
    ???:0
Segmentation fault (core dumped)
root@pangea:~/grewal/lightning#

Tried few things to debug, but the code seems to just go crash at these commands: https://github.com/satindergrewal/lightning/blob/tor/lightningd/invoice.c#L1653

struct json_stream *response;
response = json_stream_success(cmd);
satindergrewal commented 3 years ago

I wasn't sure what to do with this function, so I copied similar matching existing function from the same file, and just renamed it's function name to confirm if there's anything wrong with my code modifications etc. It compiled fine, and excuted without segfault.

Next I tried adding one by one the variables and functions code lines from the existing code and saw if any of that crashes or give segfault after compile. When it did not, I removed the copied functions code and only left it with the one which was supposed to be there. In all of that I found there was one part of the code which was required, something related to label... now that code is there, the function is there with existing code lines picked which should be there and with return statement of the response taken from it. And the lightningd doesn't segfault on executing it :slight_smile: This looked like I was missing some important changes required to add the new RPC APIs to the list of JSON RPC list, and because of that I was getting segfault.... at least that's what I understand from so far troubleshooting tries

function returning struct will return the entire struct - jl777

yes, I see the consistency of that params label set of code lines in the file. Params is part of the struct, and as you pointed it takes entire struct, and I was not handling the const jsmntok_t *params from that struct in my code. Now that it is there, lightningd starts fine :) that params was having options to use required param, or optional params. The example code from other function that I used to test was using p_req, means a required parameter, and I looked at other options in the params.h header file for more info, and found there's p_opt, means optional, so now I made it optional param too.

It is fixed in these 2 git commits: https://github.com/satindergrewal/lightning/commit/83cd24a71346ac9e789d1d260fb9e3acccc8ebd8, https://github.com/satindergrewal/lightning/commit/91ba75331c192a75f7cb848b70061ce0597a1e02