Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lyTVQ-0005eN-D5 for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Jun 2021 06:15:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lyTVN-0006Bu-Cy for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Jun 2021 06:15:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lyTVM-0006Bm-1r for pgsql-hackers@lists.postgresql.org; Wed, 30 Jun 2021 06:15:25 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lyTVI-0007wN-7G for pgsql-hackers@lists.postgresql.org; Wed, 30 Jun 2021 06:15:23 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id D6E635C005F for ; Wed, 30 Jun 2021 02:15:16 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 30 Jun 2021 02:15:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:subject:message-id:mime-version:content-type; s= fm2; bh=n66u3fhoffWOBpgBc14iFavm3xbHNUXnOjZXOfAYvzQ=; b=PHhTHy/f U4Jq2KaWqgtB0ZaFyHf79x9hnAW8AH4Gc+Fr8oyjC/dk5sTk5hl6kvQKOq1CNyHV YhPQFvjpGv3O9Bcc0z7B03KdVGIdqIVawnGsLIc7HO8wxyXuos/xiI829swTCKJr 34qHg3/eoy+yOUoTfVJAtjOqEL75R7fc4MJnt0Rv7i9Q1ll9rpDZX5AVdsEBEqBj ksULW+PfK0QfwotHVl0ummm1y4vRkRbhxPl85Ouxu7pU95uOzJj1mUkUAoyommm7 hO/Z+b4l2xE5q+jBh58vH00lWnPEppGQ6/l8mVBfSasTq/eMLrBjD7wUkgxoHtox eJ4CyyagtPE9RA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=n66u3fhoffWOBpgBc14iFavm3xbHN UXnOjZXOfAYvzQ=; b=WXfneTRG5VsMXCySA0OqbH0FojGjgqOFI93UadqCXHtZd GbVPLT9457dj0kt1gGEwPzuT46rWVErwLAt/+/c06DKrXCPXqCpOclmNrQMm0tdG OELz00xXkqbNiv8qVZtYvqEOFwPaTJeEd/t9J8wyg+aSoxTPpuQ1ozU4WniQg2hU wNaXaNLgptI2wtXPMw/dyRlrNSGLUN+wdX78R0v00LDzbkKptUblfmDpfszaUSF+ NgFvN7gzlgKwXZObwyLYn+8vKkzPXggMjcbWXyYOB/T1hZ3EVrpo24UPjWKdVjsw JGOYQ+VlNGEMdqmX+ihwzfcQQ34uZb3CUg+SHTU7g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrfeeiuddgleekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdlfeehmdenucfjughrpeffhffvuffkgggtugesghdtreertddtvden ucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprghquh hivghrrdighiiiqeenucggtffrrghtthgvrhhnpeeihedvveekjeejudduheegjeeivdfh hfefgeeftdejieelgedtgeffhfetvdevtdenucffohhmrghinhepphhoshhtghhrvghsqh hlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhho mhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiihii X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Wed, 30 Jun 2021 02:15:15 -0400 (EDT) Date: Wed, 30 Jun 2021 15:15:10 +0900 From: Michael Paquier To: Postgres hackers Subject: Dependency to logging in jsonapi.c Message-ID: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ifMa4Lw0QHZJHEgx" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --ifMa4Lw0QHZJHEgx Content-Type: multipart/mixed; boundary="wBiHjkpyo3uaUe40" Content-Disposition: inline --wBiHjkpyo3uaUe40 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi all, jsonapi.c includes the following code bits to enforce the use of logging: #ifdef FRONTEND #define check_stack_depth() #define json_log_and_abort(...) \ do { pg_log_fatal(__VA_ARGS__); exit(1); } while(0) #else #define json_log_and_abort(...) elog(ERROR, __VA_ARGS__) #endif This has been mentioned here: https://www.postgresql.org/message-id/YNfXpFeBVfU2HsVe@paquier.xyz This requires any tools in the frontend to use pg_logging_init(), which is recommended, but not enforced. Perhaps that's fine in itself to require frontends to register to the central logging APIs, but json_log_and_abort() gets only called when dealing with incorrect error codes even if we rely on JsonParseErrorType in all the places doing error handling with the JSON parsing. And requiring a dependency on logging just for unlikely-to-happen cases seems a bit crazy to me. Attached is a suggestion of patch to rework that a bit. Some extra elog()s could be added for the backend, as well as a new error code to use as default of report_parse_error(), but that does not seem to gain much. And this item looks independent of switching this code to use pqexpbuffer.h to be more portable with issues like OOM problems. Thoughts? -- Michael --wBiHjkpyo3uaUe40 Content-Type: text/x-csrc; charset=us-ascii Content-Disposition: attachment; filename="jsonapi-cleanup.c" Content-Transfer-Encoding: quoted-printable diff --git a/src/common/jsonapi.c b/src/common/jsonapi.c index d376ab152d..624b300994 100644 --- a/src/common/jsonapi.c +++ b/src/common/jsonapi.c @@ -20,20 +20,10 @@ #include "common/jsonapi.h" #include "mb/pg_wchar.h" =20 -#ifdef FRONTEND -#include "common/logging.h" -#else +#ifndef FRONTEND #include "miscadmin.h" #endif =20 -#ifdef FRONTEND -#define check_stack_depth() -#define json_log_and_abort(...) \ - do { pg_log_fatal(__VA_ARGS__); exit(1); } while(0) -#else -#define json_log_and_abort(...) elog(ERROR, __VA_ARGS__) -#endif - /* * The context of the parser is maintained by the recursive descent * mechanism, but is passed explicitly to the error reporting routine @@ -378,7 +368,9 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) JsonTokenType tok; JsonParseErrorType result; =20 +#ifndef FRONTEND check_stack_depth(); +#endif =20 if (ostart !=3D NULL) (*ostart) (sem->semstate); @@ -478,7 +470,9 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action aend =3D sem->array_end; JsonParseErrorType result; =20 +#ifndef FRONTEND check_stack_depth(); +#endif =20 if (astart !=3D NULL) (*astart) (sem->semstate); @@ -1047,7 +1041,6 @@ report_parse_error(JsonParseContext ctx, JsonLexConte= xt *lex) * unhandled enum values. But this needs to be here anyway to cover the * possibility of an incorrect input. */ - json_log_and_abort("unexpected json parse state: %d", (int) ctx); return JSON_SUCCESS; /* silence stupider compilers */ } =20 @@ -1115,8 +1108,7 @@ json_errdetail(JsonParseErrorType error, JsonLexConte= xt *lex) * unhandled enum values. But this needs to be here anyway to cover the * possibility of an incorrect input. */ - json_log_and_abort("unexpected json parse error type: %d", (int) error); - return NULL; /* silence stupider compilers */ + return psprintf("unexpected json parse error type: %d", (int) error); } =20 /* --wBiHjkpyo3uaUe40-- --ifMa4Lw0QHZJHEgx Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmDcC+4ACgkQnvQgOdby QH0hBQ//Z29WJBbhBXTHR5abYlpocZTvGSXLy+NFtyf283nx3jyDBKdAcrSlCGnI IJXPkPwNcaG85XL8O4EfOoBfJoGfEC6YHMb+f07E0svXkZTzAwB0pkao409akmHN RJVPJvX+/Bgg+/rnX/7vybd5uJnFhAmgbTeXU9Nta38hVLauj3zuMY9giNhMoKqz ETvcUD29wfz5ILd+LhCOdYc5Al9kcdanaOYs6J6Wd+ErnYAnXNvz+sNuenqXCmZs gkWzcuaMHFnKWB5GFfXGz7cAYVMTz77xpaciGGWpW8PemLHQVD797YJC9ZljGY87 RZjBmg2bmxVnFQeipZ1jdj6POkSnoTCmZKhVBTefwnBUafkFJubUqJQYHPUyi7i0 Y85eczzb5gPjlBafgDr5V0etAWnJI37eYT7SA6XWoL7QRBLO+LA4D/XfWhSKXOPG yq/wyY2XLKC3i9H+egQrTfIu+p71kNx+C0IvdZGBlqKQqZvW88l7uSB9iUOGaDfW VCc3LIXlvIxunNgfvZjSxn9p91fOfUDrVCkY/KXhr8ogtwy4QAAAOFqQlutgGXSz 3Qi+fOKfJNd1r5r3L6HsKs4ZYRs+hI7a/3ck3d6yJOY2CBQ4ogYGdhdWBQOaXiLq y4g+1hk2/RbdyyURNsri8wVVVeFKJxnzf8rHQsBJ0c995yhMIOk= =Z+UR -----END PGP SIGNATURE----- --ifMa4Lw0QHZJHEgx--