Skip to content

Commit 294c2e7

Browse files
rootcursoragent
andcommitted
fix: preserve 16-bit pid for RPC handshake wire compatibility
- Keep pid as unsigned short in process_id (Telegram DC expects 32-byte packet) - Remove assert for pid>65535, use implicit truncation instead - Revert tl-parse memcpy to 12 bytes (was sizeof for 32-bit pid) Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a4ea3b1 commit 294c2e7

3 files changed

Lines changed: 7 additions & 6 deletions

File tree

common/pid.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ npid_t PID;
3838

3939
void init_common_PID (void) {
4040
if (!PID.pid) {
41-
PID.pid = (unsigned) getpid ();
41+
int p = getpid ();
42+
PID.pid = p; /* implicit truncate to 16-bit; removed assert for pid>65535 */
4243
}
4344
if (!PID.utime) {
4445
PID.utime = time (0);
@@ -78,8 +79,8 @@ int process_id_is_newer (struct process_id *a, struct process_id *b) {
7879
assert (!memcmp (a, b, 6));
7980
if (a->utime < b->utime) { return 0; }
8081
if (a->utime > b->utime) { return 1; }
81-
unsigned x = (a->pid - b->pid) & 0x7fffffff;
82-
if (x && x <= 0x3fffffff) { return 1; }
82+
int x = (a->pid - b->pid) & 0x7fff;
83+
if (x && x <= 0x3fff) { return 1; }
8384
return 0;
8485
}
8586

common/pid.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
struct process_id {
3030
unsigned ip;
3131
short port;
32-
unsigned pid; /* was unsigned short; getpid() returns 32-bit on modern systems */
32+
unsigned short pid;
3333
int utime;
3434
};
3535

3636
struct process_id_ext {
3737
unsigned ip;
3838
short port;
39-
unsigned pid;
39+
unsigned short pid;
4040
int utime;
4141
int actor_id;
4242
};

common/tl-parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ static inline int __tl_store_init (struct tl_out_state *tlio_out, void *out, voi
770770

771771
int tls_init_raw_msg (struct tl_out_state *tlio_out, struct process_id *pid, long long qid) {
772772
if (pid) {
773-
memcpy (&tlio_out->out_pid_buf, pid, sizeof (struct process_id));
773+
memcpy (&tlio_out->out_pid_buf, pid, 12);
774774
TL_OUT_PID = &tlio_out->out_pid_buf;
775775
} else {
776776
TL_OUT_PID = 0;

0 commit comments

Comments
 (0)