Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h5Hrv-00079x-Lt for pgsql-hackers@arkaria.postgresql.org; Sat, 16 Mar 2019 22:33:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h5Hru-0000RT-A4 for pgsql-hackers@arkaria.postgresql.org; Sat, 16 Mar 2019 22:33:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h5Hru-0000RL-1s for pgsql-hackers@lists.postgresql.org; Sat, 16 Mar 2019 22:33:30 +0000 Received: from anastigmatix.net ([68.171.219.55]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h5Hrn-0006mM-UA for pgsql-hackers@postgresql.org; Sat, 16 Mar 2019 22:33:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=anastigmatix.net; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:Cc:References:To:Subject:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=v1e9I2MhZYkgj2PcgKVKEHI6dfhH7yDBtMNQ+RkM9ao=; b=CYdyCl144oDTxVwMR374mcBlsk xQidLb8iiM2dwdqQomF9Xu/XDFi/7uH+ADA3D59rNVN3vYvdSXzpk0WcAlZGs6srQSg+LNM+QaKfM PraC1F7Qk+32h/tvIGcgYwrVF/hjmcT6z14oVqOxWSN7ahn7Y4rH/ZkwC8vFF1dERRmMamTIzOdZk F/kAQq2g+VIEYSirD/2biWFSzP2gRMYZXE0uUDZF3cEPg7Oix0D8+y6i1fEgTaLuk8lGnV44S9nKa HPXQXfzg6GglBCcB9v1u8+Q15pl0WLBb4yIvtCm8SHW0IbqO/PWM4yGzQBJtFYst3VuaQNfxZuGUW ttqyvM+A==; Received: from [184.19.63.78] (port=45651) by bay.acenet.us with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.91) (envelope-from ) id 1h5Hrk-00H8Gz-Iz; Sat, 16 Mar 2019 18:33:21 -0400 Subject: Re: Fix XML handling with DOCTYPE To: Tom Lane References: <5C8D5D16.7090206@anastigmatix.net> <23319.1552769738@sss.pgh.pa.us> <5C8D6681.4070504@anastigmatix.net> <24203.1552771272@sss.pgh.pa.us> Cc: Ryan Lambert , pgsql-hackers@postgresql.org From: Chapman Flack X-Enigmail-Draft-Status: N1110 Message-ID: <5C8D79AF.7010204@anastigmatix.net> Date: Sat, 16 Mar 2019 18:33:19 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 MIME-Version: 1.0 In-Reply-To: <24203.1552771272@sss.pgh.pa.us> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-OutGoing-Spam-Status: No, score=-95.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bay.acenet.us X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - anastigmatix.net X-Get-Message-Sender-Via: bay.acenet.us: authenticated_id: chap@anastigmatix.net X-Authenticated-Sender: bay.acenet.us: chap@anastigmatix.net X-Source: X-Source-Args: X-Source-Dir: X-From-Rewrite: unmodified, already matched List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 03/16/19 17:21, Tom Lane wrote: > Chapman Flack writes: >> On 03/16/19 16:55, Tom Lane wrote: >>> What do you think of the idea I just posted about parsing off the DOCTYPE >>> thing for ourselves, and not letting libxml see it? > >> The principled way of doing that would be to pre-parse to find a DOCTYPE, >> and if there is one, leave it there and parse the input as we do for >> 'document'. Per XML, if there is a DOCTYPE, the document must satisfy >> the 'document' syntax requirements, and per SQL/XML:2006-and-later, >> 'content' is a proper superset of 'document', so if we were asked for >> 'content' and can successfully parse it as 'document', we're good, >> and if we see a DOCTYPE and yet it incurs a parse error as 'document', >> well, that's what needed to happen. > > Hm, so, maybe just > > (1) always try to parse as document. If successful, we're done. > > (2) otherwise, if allowed by xmloption, try to parse using our > current logic for the CONTENT case. What I don't like about that is that (a) the input could be arbitrarily long and complex to parse (not that you can't imagine a database populated with lots of short little XML snippets, but at the same time, a query could quite plausibly deal in yooge ones), and (b), step (1) could fail at the last byte of the input, followed by total reparsing as (2). I think the safer structure is clearly that of the current patch, modulo whether the "has a DOCTYPE" test is done by libxml itself (with the assumptions you don't like) or by a pre-scan. So the current structure is: restart: asked for document? parse as document, or fail else asked for content: parse as content failed? because DOCTYPE? restart as if document else fail and a pre-scan structure could be very similar: restart: asked for document? parse as document, or fail else asked for content: pre-scan finds DOCTYPE? restart as if document else parse as content, or fail The pre-scan is a simple linear search and will ordinarily say yes or no within a couple dozen characters--you could *have* an input with 20k of leading whitespace and comments, but it's hardly the norm. Just trying to parse as 'document' first could easily parse a large fraction of the input before discovering it's followed by something that can't follow a document element. Regards, -Chap