The 64-bit bug that broke my Telegram bot
How a Telegram user ID ended up stored as 6123456789.0, and why it only affected some people.
· 3 min read · About Lounge Booking
In February I was reworking the lounge booking Mini App for my residential college, and before letting everyone use it, I asked some friends to make a few test bookings. A few of them came back saying their bookings weren't showing up.
The bookings had been saved, and I could see them in the database, but the app just couldn't find them. So I started querying the database and working backwards from what was stored, and it turned out the people affected all had one thing in common: big Telegram IDs. That was about 40% of users. (I'll use 6123456789 as the example here, which isn't a real person's ID.)
How it happened
Telegram IDs can be big
Telegram's docs say a user ID can need more than 32 bits, though never more than 52, so a 64-bit integer or a double can always hold one. Plenty of real IDs are bigger than 2,147,483,647, which is the largest number a signed 32-bit integer can hold.
JavaScript handles them fine
A JavaScript number is a 64-bit float, which stores whole numbers exactly up to 253, so in Node, 6123456789 is still exactly 6123456789.
The SQLite driver treats them differently
When node-sqlite3 passes a JavaScript number into a query, it first checks whether the number fits in a 32-bit integer. If it does, it's sent as an integer, and if it doesn't, it's sent as a floating-point number instead (the logic is in src/statement.cc).
// node-sqlite3, simplified from src/statement.cc
if (fitsInInt32(value)) bindAsInteger(value) // 123456789
else bindAsFloat(value) // 6123456789.0That meant small IDs reached SQLite as integers, while big ones reached it as floats.
The column was text
The telegram_id column was declared as text, which was the right idea, but SQLite converts whatever it gets into the column's type, and a float with nothing after the decimal point becomes text ending in ".0". You can reproduce it in a few lines of Python:
db.execute("CREATE TABLE users (telegram_id TEXT UNIQUE)")
db.execute("INSERT INTO users VALUES (?)", (123456789,)) # integer
db.execute("INSERT INTO users VALUES (?)", (6123456789.0,)) # float
db.execute("SELECT telegram_id FROM users").fetchall()
# [('123456789',), ('6123456789.0',)]The lookup came from the URL
The "my bookings" endpoint read the ID from the URL, /api/bookings/user/6123456789, and anything from a URL is a string, so the query compared "6123456789" with the stored "6123456789.0" and never found a match. For anyone whose ID fit in 32 bits, every step went through cleanly, but for everyone else it broke, and that was the 40%.
The fix
The fix I shipped turns the ID into a plain string everywhere it enters the backend: when a user is created, when bookings are looked up and when one is cancelled.
const cleanId = String(telegramUser.id).split('.')[0];It works, but it's a fix at every entry point, and if I were starting again, I'd turn the ID into a string once, as soon as it arrives from Telegram.
Honestly, it felt pretty good to crack, and if my friends hadn't tried it first, I'm not sure when I would have found it. The rest of the project is in the Lounge Booking write-up.