Skip to content

Extra zero decimal digits after decoding NUMERIC (DECIMAL) type #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vemikhaylov opened this issue Dec 7, 2019 · 1 comment · Fixed by #522 or sthagen/MagicStack-asyncpg#2
Closed

Comments

@vemikhaylov
Copy link

  • asyncpg version: 0.18.3
  • PostgreSQL version: PostgreSQL 10.10 / PostgreSQL 11.5
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : Yes, problem is reproduced using the PostgreSQL provided by AWS and a local install
  • Python version: 3.7
  • Platform: Linux / Darwin 18.6.0 x86_64
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes
  • If you built asyncpg locally, which version of Cython did you use?: No
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : Yes

I have encountered that asyncpg decodes values of NUMERIC type with extra zero decimal digits despite the explicitly specified scale.

This code:

import asyncio
import asyncpg


async def main():
    conn = await asyncpg.connect()
    await conn.execute('''
        CREATE TABLE test_numeric(
            id serial PRIMARY KEY,
            value NUMERIC(1000, 8)
        )
    ''')
    await conn.execute('''
        INSERT INTO test_numeric(value) VALUES('0.00003000')
    ''')

    row = await conn.fetchrow('SELECT * FROM test_numeric;')
    print(row)
    await conn.close()

asyncio.get_event_loop().run_until_complete(main())

Produces the following output (12 digits right to the point while the scale of 8 was specified):

<Record id=1 value=Decimal('0.000030000000')>

However, in the database everything looks fine:

test=# SELECT * FROM test_numeric;
 id |   value    
----+------------
  1 | 0.00003000
elprans added a commit that referenced this issue Jan 8, 2020
The size of the output buffer in numeric decoder is computed incorrectly
which may lead to stack corruption or access to unitialized memory.

This also fixes incorrect rendering of trailing zeros in some cases.

Fixes: #520
Fixes: #514
@1st1 1st1 closed this as completed in #522 Jan 9, 2020
1st1 pushed a commit that referenced this issue Jan 9, 2020
The size of the output buffer in numeric decoder is computed incorrectly
which may lead to stack corruption or access to unitialized memory.

This also fixes incorrect rendering of trailing zeros in some cases.

Fixes: #520
Fixes: #514
@niranjan94
Copy link

Thanks @elprans for the speedy fix 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants