Skip to content
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

Encoding bug, still not out of the woods ... #275

Closed
blogh opened this issue Jan 21, 2022 · 2 comments
Closed

Encoding bug, still not out of the woods ... #275

blogh opened this issue Jan 21, 2022 · 2 comments
Labels

Comments

@blogh
Copy link
Collaborator

blogh commented Jan 21, 2022

We had some encoding issues there : #149.
Well we're not out of the woods yet ... there are still some edge cases (at least one) .

In psql :

pcsql -c "SELECT '\It`s gona crash', pg_sleep(10)"

Quickly :

(pgactivity) [benoit@benoit-dalibo pg_activity]$ pg_activity 
Traceback (most recent call last):
  File "/home/benoit/tmp/pgactivity/bin/pg_activity", line 33, in <module>
    sys.exit(load_entry_point('pg-activity', 'console_scripts', 'pg_activity')())
  File "/home/benoit/git/dalibo/pg_activity/pgactivity/cli.py", line 312, in main
    ui.main(term, dataobj, host, options, dsn)
  File "/home/benoit/git/dalibo/pg_activity/pgactivity/ui.py", line 200, in main
    pg_procs.set_items(data.pg_get_activities(ui.duration_mode))
  File "/home/benoit/git/dalibo/pg_activity/pgactivity/data.py", line 278, in pg_get_activities
    cur.execute(
  File "/home/benoit/tmp/pgactivity/lib64/python3.9/site-packages/psycopg2/extras.py", line 146, in execute
    return super(DictCursor, self).execute(query, vars)
psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type bytea

It's because of this part of the queries pg_get_activity*.sql:

convert_from(a.query::bytea, coalesce(pg_catalog.pg_encoding_to_char(b.encoding), 'UTF8')) AS query,

In order for the cast to work, the '' should be escaped:

[local]:5435 postgres@postgres=# SELECT convert_from('\It`s gona crash', 'UTF8');
ERROR:  invalid input syntax for type bytea
LINE 1: SELECT convert_from('\It`s gona crash', 'UTF8');
                            ^
[local]:5435 postgres@postgres=# SELECT convert_from(E'\It`s gona crash', 'UTF8');
  convert_from   
-----------------
 It`s gona crash
(1 row)
[local]:5435 postgres@postgres=# SELECT convert_from('\\It`s gona crash', 'UTF8');
   convert_from   
------------------
 \It`s gona crash
(1 row)
@blogh blogh added the bug label Jan 21, 2022
@ssharunas
Copy link

This bug crashes pg_activity if there are a lot of queries with regexp or newlines.
Example:

select '123' ~ '\d+', pg_sleep(3);
select 'Hello world!\n', pg_sleep(3)

Not sure if it is the best solution, but manually escaping '\' to '\\' in all queries with a cast to ::bytea works.

-- fails:
convert_from(a.query::bytea, coalesce(pg_catalog.pg_encoding_to_char(b.encoding), 'UTF8')) AS query
-- fixes the problem:
convert_from(replace(a.query, '\', '\\')::bytea, coalesce(pg_catalog.pg_encoding_to_char(b.encoding), 'UTF8')) AS query

blogh added a commit to blogh/pg_activity that referenced this issue May 5, 2022
pg_activity crashes when queries use newlines or regexp as show in
issue dalibo#275

This fix replaces single \ characters with double \ in the blocking,
waiting and activity queries.
blogh added a commit to blogh/pg_activity that referenced this issue May 5, 2022
pg_activity crashes when queries use newlines or regexp as show in
issue dalibo#275

This fix replaces single \ characters with double \ in the blocking,
waiting and activity queries.
blogh added a commit to blogh/pg_activity that referenced this issue May 5, 2022
pg_activity crashes when queries use newlines or regexp as show in
issue dalibo#275

This fix replaces single \ characters with double \ in the blocking,
waiting and activity queries.
blogh added a commit that referenced this issue May 5, 2022
pg_activity crashes when queries use newlines or regexp as show in
issue #275

This fix replaces single \ characters with double \ in the blocking,
waiting and activity queries.
@blogh
Copy link
Collaborator Author

blogh commented May 5, 2022

Thanks for the contribution.
I commit-ed your change.

@blogh blogh closed this as completed May 5, 2022
blogh added a commit to blogh/pg_activity that referenced this issue Sep 16, 2022
Breaking change:

* Attr 18.1 is required

Change log:

* Add more information to the header (instance and process stats) (Tests
  by @Krysztophe)
* Add the --refresh option to the cli to set the refresh rate (dalibo#293)
  (Requested by @crysman)
* Add the --debug-file option to enable logging (still mostly unused)
* Add hints about runtime disabled features (dalibo#300) (Reported by @rutchkiwi)
* The SUPERUSER privilege is not longer required (dalibo#277) (Requested by
  @Raymondmax)

Bug fixes:

* Add the --no-walreceiver to disable wal receiver stats for Aurora
  (dalibo#301) (Reported by @grutz)
* Add the --no-tempfiles option to disable temp file statistics and
  add it to the --rds command (dalibo#303) (Reported by @adityabaradwaj)
* Fix server information queries for v12/v13 (Reported and fixed by
  @kmoppel-cognite)
* Fix InvalidTextRepresentation errors (dalibo#275) (Fix proposed by
  @ssharunas)
* Fix sort order for parallel queries (dalibo#297) (Reported and fixed by
  @kmoppel-cognite)
* Doc fixes and packaging improvements (@kianmeng, @Vampouille)
@blogh blogh mentioned this issue Sep 16, 2022
blogh added a commit that referenced this issue Sep 16, 2022
Breaking change:

* Attr 18.1 is required

Change log:

* Add more information to the header (instance and process stats) (Tests
  by @Krysztophe)
* Add the --refresh option to the cli to set the refresh rate (#293)
  (Requested by @crysman)
* Add the --debug-file option to enable logging (still mostly unused)
* Add hints about runtime disabled features (#300) (Reported by @rutchkiwi)
* The SUPERUSER privilege is not longer required (#277) (Requested by
  @Raymondmax)

Bug fixes:

* Add the --no-walreceiver to disable wal receiver stats for Aurora
  (#301) (Reported by @grutz)
* Add the --no-tempfiles option to disable temp file statistics and
  add it to the --rds command (#303) (Reported by @adityabaradwaj)
* Fix server information queries for v12/v13 (Reported and fixed by
  @kmoppel-cognite)
* Fix InvalidTextRepresentation errors (#275) (Fix proposed by
  @ssharunas)
* Fix sort order for parallel queries (#297) (Reported and fixed by
  @kmoppel-cognite)
* Doc fixes and packaging improvements (@kianmeng, @Vampouille)
blogh added a commit to blogh/pg_activity that referenced this issue Sep 16, 2022
Breaking change:

* Attr 18.1 is required

Change log:

* Add more information to the header (instance and process stats) (Tests
  by @Krysztophe)
* Add the --refresh option to the cli to set the refresh rate (dalibo#293)
  (Requested by @crysman)
* Add the --debug-file option to enable logging (still mostly unused)
* Add hints about runtime disabled features (dalibo#300) (Reported by @rutchkiwi)
* The SUPERUSER privilege is not longer required (dalibo#277) (Requested by
  @Raymondmax)

Bug fixes:

* Add the --no-walreceiver to disable wal receiver stats for Aurora
  (dalibo#301) (Reported by @grutz)
* Add the --no-tempfiles option to disable temp file statistics and
  add it to the --rds command (dalibo#303) (Reported by @adityabaradwaj)
* Fix server information queries for v12/v13 (Reported and fixed by
  @kmoppel-cognite)
* Fix InvalidTextRepresentation errors (dalibo#275) (Fix proposed by
  @ssharunas)
* Fix sort order for parallel queries (dalibo#297) (Reported and fixed by
  @kmoppel-cognite)
* Doc fixes and packaging improvements (@kianmeng, @Vampouille)
blogh added a commit to blogh/pg_activity that referenced this issue Jan 10, 2023
This test will ensure we still to handle queries without throwing this
exception :

psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type bytea
blogh added a commit to blogh/pg_activity that referenced this issue Jan 10, 2023
This test will ensure we still to handle queries without throwing this
exception :

psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type bytea
blogh added a commit to blogh/pg_activity that referenced this issue Jan 10, 2023
This test will ensure we still to handle queries without throwing this
exception :

psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type bytea
blogh added a commit that referenced this issue Jan 23, 2023
This test will ensure we still to handle queries without throwing this
exception :

psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type bytea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants