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 1ian2I-0003K2-JZ for pgsql-bugs@arkaria.postgresql.org; Fri, 29 Nov 2019 20:38:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ian2H-000083-DQ for pgsql-bugs@arkaria.postgresql.org; Fri, 29 Nov 2019 20:38:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ian2G-00005O-Tp for pgsql-bugs@lists.postgresql.org; Fri, 29 Nov 2019 20:38:41 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ian2E-0006Fd-3t for pgsql-bugs@lists.postgresql.org; Fri, 29 Nov 2019 20:38:39 +0000 Received: by mail-wm1-x344.google.com with SMTP id s14so9647293wmh.4 for ; Fri, 29 Nov 2019 12:38:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=qgogYVDaMsiG4GWf+VleArZbTzukoKBf9OFXE6RpWsQ=; b=vzDMexEDh2XgAsYtKdlpbE3p530gsAi1VlhslbnkM9C80OQSmWdm1v7mqwOqi4CU3X F7w0Dmd+NNQK4k/rnHH3vl71WR2ebNGyWeusZUWQUqmZD00NMFK5NIRgRWbyo0k0CcoZ kogcLslNuPjSWgWWA6mPmd96k1ChlMiL+m/W6+pnwVvTRYx6u2Uv9o75U/4UKm8GEuKk /+SRdmVnzqF5jffl4sCDeVRNBTzFmpArQRqwRdrKZl4k1cp26uBjjwWgKbPFxwr9dgCX sdPSx5S/njj2u6QpCJMR7Ju9nLvzs3U8e6WUnOceo5AfWXqiDOwloyVz0lEj2/koBgt0 3YdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=qgogYVDaMsiG4GWf+VleArZbTzukoKBf9OFXE6RpWsQ=; b=U4iI5stLetzBgjXxCYxLxmf0ibvFGfwBOdLKN0oG+u91iVOLs2eY+h5sinG4h57qKR rCWnqA6cQm2AtG5Gs9hXYdNFDIVgOtejKHGKjg5BY2xdyBmQRkUAnlWAu1mOrwAnw9iH VIoBAdSe0h55MlQZoAlFVQckvyQ6XLs+83vx3HnXDd2QVt6IeZlAwdejxA05/XxPt+VF CdCHCg+pITEyX82FQRd3uaa6HcOhhb3rP/qkKDgVFknweYUAIoBULoUa1gBpFzmrrC+x 4XgXhGR9cBfUHsZRuLTTeKPr/Gae8f23tJ0h88ZfXiIZswAfB4FeDnvTdA1ljpRxHNyp z3Pg== X-Gm-Message-State: APjAAAVVA+6stoguYi7zIiJ8AQzP0K0YU/rtXpDlyX12H8v2SnPbLuI0 ElwuYknEOo5Oifrvl3r3zDrmyGlmxG4= X-Google-Smtp-Source: APXvYqxwAGyZQN0xQ8emYlABTbc7yqCqKp9Zlj1aOw/kT9Km+sdX5JfH5i7S9R+ZciRSCPUS5XZD2w== X-Received: by 2002:a1c:40c1:: with SMTP id n184mr17539484wma.116.1575059916068; Fri, 29 Nov 2019 12:38:36 -0800 (PST) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id g207sm16058470wmg.40.2019.11.29.12.38.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Nov 2019 12:38:34 -0800 (PST) Date: Fri, 29 Nov 2019 21:38:32 +0100 From: Tomas Vondra To: paul@intekon.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16143: PGTYPEStimestamp_fmt_asc() returns the incorrect month when the format specifier %b is used. Message-ID: <20191129203832.xybuswyoa455mwy2@development> References: <16143-0d861eb8688d3fef@postgresql.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="xz7rkmjkptwaiibh" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <16143-0d861eb8688d3fef@postgresql.org> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --xz7rkmjkptwaiibh Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit On Fri, Nov 29, 2019 at 07:40:37PM +0000, PG Bug reporting form wrote: >The following bug has been logged on the website: > >Bug reference: 16143 >Logged by: Paul Spencer >Email address: paul@intekon.com >PostgreSQL version: 11.5 >Operating system: Redhat and Debian >Description: > >PGTYPEStimestamp_fmt_asc() returns the incorrect month when the format >specifier %b is used. The returned month is one greater then the expected >month. If the expected month is “Dec”, the application may crash with a >segment fault. The format specifier %B has a similar issue. > >** Investigation Notes >- The month is increased by one at line 143 in timestamp2tm() defined in >timestamp.c 
https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/interfaces/ecpg/pgtypeslib/timestamp.c;h=810dd06ee68b9e39bfbb8d1fb4b58b8205f24246;hb=HEAD >- The month number is converted to the abbreviation at line 337 in >dttofmtasc_replace() defined in timestamp.c 
https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/interfaces/ecpg/pgtypeslib/timestamp.c;h=810dd06ee68b9e39bfbb8d1fb4b58b8205f24246;hb=HEAD >- Month abbreviations are defined at line 499 in dt_common.c 
 >https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/interfaces/ecpg/pgtypeslib/dt_common.c;h=c1a3a3e2cb7e2d4f375a3b1a2e858f7347a867ea;hb=HEAD > Yeah, seems like a simple off-by-one mistake. Our tm->tm_mon is 1-based, but dttofmtasc_replace uses it directly to access elements of arrays with month names. Hence the "next" month is returned, and crash for the last month (access out of bounds). The attached patch should fix this, I believe - both for %b and %B. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --xz7rkmjkptwaiibh Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="ecpg-month-index-fix.patch" diff --git a/src/interfaces/ecpg/pgtypeslib/timestamp.c b/src/interfaces/ecpg/pgtypeslib/timestamp.c index 810dd06ee6..ddb82a1ad6 100644 --- a/src/interfaces/ecpg/pgtypeslib/timestamp.c +++ b/src/interfaces/ecpg/pgtypeslib/timestamp.c @@ -334,13 +334,13 @@ dttofmtasc_replace(timestamp * ts, date dDate, int dow, struct tm *tm, /* XXX should be locale aware */ case 'b': case 'h': - replace_val.str_val = months[tm->tm_mon]; + replace_val.str_val = months[tm->tm_mon - 1]; replace_type = PGTYPES_TYPE_STRING_CONSTANT; break; /* the full name of the month */ /* XXX should be locale aware */ case 'B': - replace_val.str_val = pgtypes_date_months[tm->tm_mon]; + replace_val.str_val = pgtypes_date_months[tm->tm_mon - 1]; replace_type = PGTYPES_TYPE_STRING_CONSTANT; break; --xz7rkmjkptwaiibh--