mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 09:54:16 +01:00
e81bf8b89f
Fixes nullability errors detected by UBSan: wire/fromwire.c:173:46: runtime error: null pointer passed as argument 1, which is declared to never be null external/libwally-core/src/secp256k1/include/secp256k1.h:432:3: note: nonnull attribute specified here #0 0x65214a in fromwire_secp256k1_ecdsa_signature wire/fromwire.c:173:6 #1 0x659500 in printwire_secp256k1_ecdsa_signature devtools/print_wire.c:331:1 #2 0x646ba2 in printwire_channel_update wire/peer_printgen.c:1900:7 #3 0x637182 in printpeer_wire_message wire/peer_printgen.c:128:11 #4 0x65a097 in main devtools/decodemsg.c:85:10
118 lines
3.0 KiB
C
118 lines
3.0 KiB
C
#include "config.h"
|
|
#include <ccan/err/err.h>
|
|
#include <ccan/opt/opt.h>
|
|
#include <ccan/tal/grab_file/grab_file.h>
|
|
#include <common/setup.h>
|
|
#include <unistd.h>
|
|
#include <wire/onion_printgen.h>
|
|
#include <wire/peer_printgen.h>
|
|
|
|
static char *opt_set_tlvname(const char *arg,
|
|
bool (**printwire)(const char *fieldname,
|
|
const u8 **cursor,
|
|
size_t *plen))
|
|
{
|
|
for (size_t i = 0; tlvs_printpeer_wire_byname[i].name; i++) {
|
|
if (streq(arg, tlvs_printpeer_wire_byname[i].name)) {
|
|
*printwire = tlvs_printpeer_wire_byname[i].print;
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
for (size_t i = 0; tlvs_printonion_wire_byname[i].name; i++) {
|
|
if (streq(arg, tlvs_printonion_wire_byname[i].name)) {
|
|
*printwire = tlvs_printonion_wire_byname[i].print;
|
|
return NULL;
|
|
}
|
|
}
|
|
return "Unknown tlv name";
|
|
}
|
|
|
|
static char *opt_set_onionprint(bool (**printwire)(const u8 *msg))
|
|
{
|
|
*printwire = printonion_wire_message;
|
|
return NULL;
|
|
}
|
|
|
|
static char *opt_list_tlvnames(void *unused)
|
|
{
|
|
for (size_t i = 0; tlvs_printpeer_wire_byname[i].name; i++)
|
|
printf(" %s\n", tlvs_printpeer_wire_byname[i].name);
|
|
|
|
for (size_t i = 0; tlvs_printonion_wire_byname[i].name; i++)
|
|
printf(" %s\n", tlvs_printonion_wire_byname[i].name);
|
|
exit(0);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
const u8 *m;
|
|
bool (*printtlv)(const char *fieldname, const u8 **cursor, size_t *plen) = NULL;
|
|
bool (*printwire)(const u8 *msg) = printpeer_wire_message;
|
|
bool ok = true;
|
|
|
|
common_setup(argv[0]);
|
|
setup_locale();
|
|
|
|
opt_register_noarg("--onion", opt_set_onionprint, &printwire,
|
|
"Decode an error message instead of a peer message");
|
|
opt_register_arg("--tlv", opt_set_tlvname, NULL, &printtlv,
|
|
"Decode a TLV of this type instead of a peer message");
|
|
opt_register_noarg("--list-tlvs", opt_list_tlvnames, NULL,
|
|
"List all --tlv names supported");
|
|
opt_register_noarg("--help|-h", opt_usage_and_exit,
|
|
"[<hexmsg>]"
|
|
"Decode a lightning spec wire message from hex, or a series of messages from stdin",
|
|
"Print this message.");
|
|
|
|
opt_parse(&argc, argv, opt_log_stderr_exit);
|
|
if (argc > 2)
|
|
opt_usage_and_exit("Too many arguments");
|
|
|
|
if (argc == 2) {
|
|
/* Arg is hex string */
|
|
m = tal_hexdata(NULL, argv[1], strlen(argv[1]));
|
|
if (!m)
|
|
errx(1, "'%s' is not valid hex", argv[1]);
|
|
|
|
if (printtlv) {
|
|
size_t len = tal_bytelen(m);
|
|
ok &= printtlv("", &m, &len);
|
|
} else {
|
|
ok &= printwire(m);
|
|
}
|
|
} else {
|
|
u8 *f = grab_fd(NULL, STDIN_FILENO);
|
|
size_t off = 0;
|
|
|
|
while (off != tal_count(f)) {
|
|
be16 len;
|
|
|
|
if (off + sizeof(len) > tal_count(f)) {
|
|
warnx("Truncated file");
|
|
ok = false;
|
|
break;
|
|
}
|
|
memcpy(&len, f + off, sizeof(len));
|
|
off += sizeof(len);
|
|
if (off + be16_to_cpu(len) > tal_count(f)) {
|
|
warnx("Truncated file");
|
|
ok = false;
|
|
break;
|
|
}
|
|
m = tal_dup_arr(f, u8, f + off, be16_to_cpu(len), 0);
|
|
if (printtlv) {
|
|
size_t mlen = tal_bytelen(m);
|
|
ok &= printtlv("", &m, &mlen);
|
|
} else {
|
|
ok &= printwire(m);
|
|
}
|
|
off += be16_to_cpu(len);
|
|
tal_free(m);
|
|
}
|
|
}
|
|
printf("\n");
|
|
|
|
return ok ? 0 : 1;
|
|
}
|