bitcoin/short_channel_id: short_channel_id_from_str accepts trailing garbage, negative numbers, and whitespace

Aperta Adatta ai principianti
#9,700 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@bhuvan-somisetty ci sta già lavorando.

Dal 5/11/2026.

  • #9701 di @bhuvan-somisetty — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
83/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c

Direzione di ricerca

Inizia da short_channel_id_from_str() in bitcoin/short_channel_id.c e segui short_channel_id_dir_from_str(), che delega a questa funzione. Verifica come il parser attuale gestisce gli esempi non validi forniti, quindi assicurati che l’analisi rifiuti gli input malformati e accetti il formato decimale specificato a tre componenti. Il lavoro è completato quando vengono rifiutati gli esempi con caratteri finali, valori negativi, segni o spazi bianchi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Issue and Steps to Reproduce

short_channel_id_from_str() in bitcoin/short_channel_id.c parses short channel ID strings using sscanf:

bool short_channel_id_from_str(const char *str, size_t strlen,
			       struct short_channel_id *dst)
{
	u32 blocknum, txnum;
	u16 outnum;

	if (sscanf(str, "%ux%ux%hu", &blocknum, &txnum, &outnum) == 3)
		return mk_short_channel_id(dst, blocknum, txnum, outnum);
	return false;
}

This implementation allows several invalid string formats to succeed:

  1. Trailing garbage / unparsed characters: sscanf does not check if the entire buffer length (strlen) is consumed. Strings such as "1x2x3garbage", "1x2x3/0", "1x2x3x4", or "1x2x3 4" successfully return true with valid dst filled from the initial match.
  2. Negative outnums: %hu accepts negative numbers (e.g. "-1"), parsing them as 65535 ((u16)-1). In mk_short_channel_id(), (outnum & 0xFFFF) != outnum evaluates to 65535 != 65535 which is false, so "1x2x-1" is accepted as valid channel output 65535.
  3. Leading / internal whitespace and signs: sscanf skips whitespace before numeric conversions and parses leading + signs, allowing strings like " 1x2x3", "1x 2x3", or "+1x2x3".
  4. short_channel_id_dir_from_str pollution: Because short_channel_id_dir_from_str delegates to short_channel_id_from_str(str, slash - str, ...), strings like "1x2x3garbage/0" are also parsed without error.
Steps to reproduce:
struct short_channel_id scid;
assert(short_channel_id_from_str("1x2x3garbage", strlen("1x2x3garbage"), &scid) == false); // Fails (returns true)
assert(short_channel_id_from_str("1x2x-1", strlen("1x2x-1"), &scid) == false);             // Fails (returns true)
Expected behavior:

Per BOLT 7, a short channel ID is uniquely represented by three decimal components delimited by 'x': <blocknum>x<txnum>x<outnum>. short_channel_id_from_str() should strictly parse the entire provided string and reject trailing garbage, negative values, signs, and whitespace.

getinfo output

N/A (observed in bitcoin/short_channel_id.c across all CLN versions).

Lingua principale
C
Stelle
3.1k
Fork
1k
Merge medio
4g 21h
PR unite (30g)
21

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ElementsProject/lightning

Tutte le issue di ElementsProject/lightning

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.