1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-06-23 21:45:30 +00:00
profanity/src
John Hernandez db96d13453
Improve string convertion error handling
Before the change, profanity has shown
`TypeError: prof_pre_chat_message_display() missing 3 required positional arguments: 'barejid',
           'resource', and 'message'`
error for received messages with incorrect encoding
instead of proper error handling for string convertion.

That is a temporary fix, correct fix probably would be
passing potentially problematic char* as `bytes` (using `y` instead of `s` format),
but this would require API changes and hence correction of plugins to
handle new input parameters properly (likely with "decode" function).

A bit hard to reproduce. You need to add a plugin with the following code
(or any other plugin with `prof_pre_chat_message_display` present, such as emoticons.py):
```python
def prof_pre_chat_message_display(barejid, resource, message):
    return message
```

and then receive/send message with incorrectly encoded character, it can be any
invalid UTF8 symbol. You'll see error in console, errors don't represent actual
errors in plugins, but rather Profanity's shortcoming, though it make appear so
that the problem is in plugin.

Actual proper handling would likely be using `y` instead of `s` format
(see [reference](https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue))

> s ([str](https://docs.python.org/3/library/stdtypes.html#str) or None) [const char *]
>    Convert a null-terminated C string to a Python [str](https://docs.python.org/3/library/stdtypes.html#str)
object using 'utf-8' encoding. If the C string pointer is NULL, None is used.

to

> y ([bytes](https://docs.python.org/3/library/stdtypes.html#bytes)) [const char *]
>     This converts a C string to a Python [bytes](https://docs.python.org/3/library/stdtypes.html#bytes)
> object. If the C string pointer is NULL, None is returned.

since there is a [problem](https://docs.python.org/3/c-api/arg.html) with `s`:
> s ([str](https://docs.python.org/3/library/stdtypes.html#str)) [const char *]
>    Convert a Unicode object to a C pointer to a character string.
> A pointer to an existing string is stored in the character pointer variable whose address you pass.
> ... Unicode objects are converted to C strings using 'utf-8' encoding.
> **If this conversion fails**, a [UnicodeError](https://docs.python.org/3/library/exceptions.html#UnicodeError) is raised.

In python such problem can be handled using `errors='ignore'` in [bytes.decode](https://docs.python.org/3/library/stdtypes.html#bytes.decode) or in a more sophisticated manner, depending on needs and realization.
2023-11-22 09:29:31 +01:00
..
command Separate new tabmode in a setting 2023-11-20 15:03:59 +01:00
config Fix mem leak in conf_string_list_add 2023-11-17 21:13:32 +01:00
event Fix crash on quick /reconnect now usage (double free) 2023-10-15 23:23:01 +02:00
omemo Ensure instances of auto types are initialized 2023-11-06 22:22:35 +01:00
otr g_free() to auto_gfree, introduce auto_guchar 2023-07-13 17:04:59 +02:00
pgp Introduce a shared implementation for keyfile loading 2023-09-04 09:59:09 +02:00
plugins Improve string convertion error handling 2023-11-22 09:29:31 +01:00
tools Add emptiness check for /url save and /plugins install 2023-11-10 20:05:01 +01:00
ui Merge pull request #1912 from H3rnand3zzz/feature/dynamic-statusbar 2023-11-20 15:20:21 +01:00
xmpp Merge pull request #1902 from H3rnand3zzz/feature/improved-history 2023-11-06 22:41:36 +01:00
chatlog.c Fix my email address 2023-07-31 16:51:58 +02:00
chatlog.h Split log.c into log.c and chatlog.c 2022-06-28 20:08:38 +02:00
common.c Introduce a shared implementation for keyfile loading 2023-09-04 09:59:09 +02:00
common.h Introduce a shared implementation for keyfile loading 2023-09-04 09:59:09 +02:00
database.c Do not check for unique stanza-id for MAM messages 2023-11-13 09:34:53 +01:00
database.h Fix my email address 2023-07-31 16:51:58 +02:00
log.c Fix some more char* -> gchar*. 2023-08-31 23:14:02 +02:00
log.h Fix some more char* -> gchar*. 2023-08-31 23:14:02 +02:00
main.c Add helper function to create version string 2023-07-25 20:58:15 +02:00
profanity.c Add helper function to create version string 2023-07-25 20:58:15 +02:00
profanity.h Update copyright year 2023-01-10 10:37:25 +01:00