Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wJvXi-000QAf-27 for pgsql-hackers@arkaria.postgresql.org; Mon, 04 May 2026 15:49:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wJvXf-00800d-1m for pgsql-hackers@arkaria.postgresql.org; Mon, 04 May 2026 15:49:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wJvXe-00800V-39 for pgsql-hackers@lists.postgresql.org; Mon, 04 May 2026 15:49:07 +0000 Received: from fhigh-b7-smtp.messagingengine.com ([202.12.124.158]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wJvXb-00000000AKb-05F6 for pgsql-hackers@lists.postgresql.org; Mon, 04 May 2026 15:49:05 +0000 Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfhigh.stl.internal (Postfix) with ESMTP id 5D0B27A00CD; Mon, 4 May 2026 11:49:02 -0400 (EDT) Received: from phl-imap-15 ([10.202.2.104]) by phl-compute-05.internal (MEProxy); Mon, 04 May 2026 11:49:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=partin.io; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1777909742; x=1777996142; bh=/TQ/2XMDPccSeKODWkirV5Lak66yzt2RxW9eM8D+VUg=; b= IvYxPzj+GF6/pCGrxgCtsKPPvvKbnyERbHdknjLZx3bTsNKcavncA4zyxa1aHa2G yyo1Q1Yzw53ezvE3FYyLE7+0nH56chMUG61bQ+v1tiScUGUvSfksWfjcu1LfOUMI epOELnOZ2vjGz3KZWIt2AFJOtchj25GeQiv4c53vNrZ+XVfyc8IxgX4DDbOZ9Hta vowMscfdb2BHxiFruEZm4QLbFqJv06u8g7k2iIJ9+UxsataHw7i/GoYvSBCXzSkM oWj/cphAp90ytlBy1gF4RRfsgKe090MB7HGtvEM3fSyI/i9O61QmW1c127SBQGvm 3bywJ7b/VQsO0yyfkhbohQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1777909742; x= 1777996142; bh=/TQ/2XMDPccSeKODWkirV5Lak66yzt2RxW9eM8D+VUg=; b=D LT6uQUWLfzmBlgNMIM2m1VJaYIPKbd07nX6OTyKIb1Up277bQvWd1Kt0Q8dVsh8N j6rGQYSU1GgQn26n2qOyprqljGyFug6w48SaGD2ce5hwjImv1hKk5E8QQNl4E/xY 3xpZjrs6ioXQzdrDrHlcDF9mHfGM1zNDZ1pb9yJOj9J2cALteZZ2LHL5WX1+BkOX 6rwAVS1xCENRbPzSd2TLAu+obgA7f7+2D6hFkF6QSP/X7FAG2JdbLDpZk/UGvCFf WbC60L7FqAj3vbP4H/VHey0pG3avKpgIUkm0SzLUChqNf4ohVB+AUJJdyDnOwFmH Z6gCExNDiDsb2Pyde0oDA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefhedrtddtgdelledvgecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefoggfgtgffkfevuffvhffofhgjsehtqhertdertdejnecuhfhrohhmpedfvfhrihhs thgrnhcurfgrrhhtihhnfdcuoehtrhhishhtrghnsehprghrthhinhdrihhoqeenucggtf frrghtthgvrhhnpeejiefhfeefiedvheehheegiedthfdtudehueelheekleeuvdfhveeh kefhleegteenucffohhmrghinheprghmrgiiohhnrdgtohhmnecuvehluhhsthgvrhfuih iivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepthhrihhsthgrnhesphgrrhhtihhn rdhiohdpnhgspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoh epshhulhgrmhhulhesghhmrghilhdrtghomhdprhgtphhtthhopehpghhsqhhlqdhhrggt khgvrhhssehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: idd01497b:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 9E904780070; Mon, 4 May 2026 11:49:01 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 04 May 2026 15:49:00 +0000 Message-Id: Cc: "pgsql-hackers" Subject: Re: Cleanup: Replace sscanf with strtol/strtoul in snapmgr To: "Amul Sul" From: "Tristan Partin" X-Mailer: aerc 0.21.0 References: In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon Apr 20, 2026 at 12:07 AM CDT, Amul Sul wrote: > Hi, > > The attached patch replaces sscanf with strtol and strtoul in the > ImportSnapshot helpers (parseIntFromText, parseXidFromText, and > parseVxidFromText) to improve reliability and efficiency. By utilizing > the end pointer, we can locate the next line without re-scanning the > entire string. > > Additionally, this change aligns the snapshot code with the rest of > the Postgres backend, which already favors these functions for safer > parsing. Hey Amul, The patch generally looks good. One comment: > @@ -1359,17 +1365,36 @@ parseVxidFromText(const char *prefix, char **s, c= onst char *filename, > { > char *ptr =3D *s; > int prefixlen =3D strlen(prefix); > + long lval; > + unsigned long ulval; Perhaps better variable names would be procNumber and=20 localTransactionId. > + char *endptr; >=20 > if (strncmp(ptr, prefix, prefixlen) !=3D 0) > ereport(ERROR, > (errcode(ERRCODE_INVALID_TEXT_REPRESENTAT= ION), > errmsg("invalid snapshot data in file \"= %s\"", filename))); > ptr +=3D prefixlen; > - if (sscanf(ptr, "%d/%u", &vxid->procNumber, &vxid->localTransacti= onId) !=3D 2) > + > + /* Parse procNumber (the signed integer before '/') */ > + errno =3D 0; > + lval =3D strtol(ptr, &endptr, 10); > + if (endptr =3D=3D ptr || errno !=3D 0 || lval < INT_MIN || lval >= INT_MAX || > + *endptr !=3D '/') > ereport(ERROR, > (errcode(ERRCODE_INVALID_TEXT_REPRESENTAT= ION), > errmsg("invalid snapshot data in file \"= %s\"", filename))); > - ptr =3D strchr(ptr, '\n'); > + vxid->procNumber =3D (ProcNumber) lval; > + ptr =3D endptr + 1; /* skip the '/' separat= or */ > + > + /* Parse localTransactionId (the unsigned integer after '/') */ > + errno =3D 0; > + ulval =3D strtoul(ptr, &endptr, 10); > + if (endptr =3D=3D ptr || errno !=3D 0 || ulval > UINT_MAX) > + ereport(ERROR, > + (errcode(ERRCODE_INVALID_TEXT_REPRESENTAT= ION), > + errmsg("invalid snapshot data in file \"= %s\"", filename))); > + vxid->localTransactionId =3D (LocalTransactionId) ulval; > + ptr =3D strchr(endptr, '\n'); > if (!ptr) > ereport(ERROR, > (errcode(ERRCODE_INVALID_TEXT_REPRESENTAT= ION), Otherwise, this looks committable to me. In reviewing, I learned that=20 sscanf() will parse a string like " 45" as 45, so doesn't seem like we=20 will have any behavioral differences using strto*(). --=20 Tristan Partin PostgreSQL Contributors Team AWS (https://aws.amazon.com)