Skip to content

Commit

Permalink
Merge branch 'hotfix/invalid-utf8-crash'
Browse files Browse the repository at this point in the history
  • Loading branch information
MinoMino committed Nov 17, 2015
2 parents 645a070 + 52ade61 commit 0955da0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ void __cdecl My_SV_SetConfigstring(int index, char* value) {
return;
}

if (!value) value = "";
char* res = SetConfigstringDispatcher(index, value);
// NULL means stop the event.
if (res)
Expand Down
1 change: 0 additions & 1 deletion python/minqlx/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import datetime
import os.path
import logging
import shutil
import shlex
import sys
import os
Expand Down
2 changes: 1 addition & 1 deletion python/minqlx/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def handle_console_print(text):
"""
try:
text = text.decode(errors="ignore").rstrip()
text = text.rstrip()
if not text:
return

Expand Down
3 changes: 2 additions & 1 deletion python/minqlx/_zmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
we get stats from it. It polls the ZMQ socket approx. every 0.25 seconds."""

import minqlx
import json
import zmq

class StatsListener():
Expand Down Expand Up @@ -59,7 +60,7 @@ def keep_receiving(self):
if self.done:
return
while True: # Will throw an expcetion if no more data to get.
stats = self.socket.recv_json(zmq.NOBLOCK)
stats = json.loads(self.socket.recv(zmq.NOBLOCK).decode(errors="ignore"))
minqlx.EVENT_DISPATCHERS["stats"].dispatch(stats)

if stats["TYPE"] == "MATCH_STARTED":
Expand Down
16 changes: 12 additions & 4 deletions python_dispatchers.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ char* ClientCommandDispatcher(int client_id, char* cmd) {

PyGILState_STATE gstate = PyGILState_Ensure();

PyObject* result = PyObject_CallFunction(client_command_handler, "is", client_id, cmd);
PyObject* cmd_string = PyUnicode_DecodeUTF8(cmd, strlen(cmd), "ignore");
PyObject* result = PyObject_CallFunction(client_command_handler, "iO", client_id, cmd_string);

if (result == NULL)
DebugError("PyObject_CallFunction() returned NULL.\n",
Expand All @@ -22,6 +23,7 @@ char* ClientCommandDispatcher(int client_id, char* cmd) {
else if (PyUnicode_Check(result))
ret = PyUnicode_AsUTF8(result);

Py_XDECREF(cmd_string);
Py_XDECREF(result);

PyGILState_Release(gstate);
Expand All @@ -35,7 +37,8 @@ char* ServerCommandDispatcher(int client_id, char* cmd) {

PyGILState_STATE gstate = PyGILState_Ensure();

PyObject* result = PyObject_CallFunction(server_command_handler, "is", client_id, cmd);
PyObject* cmd_string = PyUnicode_DecodeUTF8(cmd, strlen(cmd), "ignore");
PyObject* result = PyObject_CallFunction(server_command_handler, "iO", client_id, cmd_string);

if (result == NULL)
DebugError("PyObject_CallFunction() returned NULL.\n",
Expand All @@ -45,6 +48,7 @@ char* ServerCommandDispatcher(int client_id, char* cmd) {
else if (PyUnicode_Check(result))
ret = PyUnicode_AsUTF8(result);

Py_XDECREF(cmd_string);
Py_XDECREF(result);

PyGILState_Release(gstate);
Expand Down Expand Up @@ -163,7 +167,8 @@ char* SetConfigstringDispatcher(int index, char* value) {

PyGILState_STATE gstate = PyGILState_Ensure();

PyObject* result = PyObject_CallFunction(set_configstring_handler, "is", index, value);
PyObject* value_string = PyUnicode_DecodeUTF8(value, strlen(value), "ignore");
PyObject* result = PyObject_CallFunction(set_configstring_handler, "iO", index, value_string);

if (result == NULL)
DebugError("PyObject_CallFunction() returned NULL.\n",
Expand All @@ -173,6 +178,7 @@ char* SetConfigstringDispatcher(int index, char* value) {
else if (PyUnicode_Check(result))
ret = PyUnicode_AsUTF8(result);

Py_XDECREF(value_string);
Py_XDECREF(result);

PyGILState_Release(gstate);
Expand Down Expand Up @@ -202,7 +208,8 @@ char* ConsolePrintDispatcher(char* text) {

PyGILState_STATE gstate = PyGILState_Ensure();

PyObject* result = PyObject_CallFunction(console_print_handler, "y", text);
PyObject* text_string = PyUnicode_DecodeUTF8(text, strlen(text), "ignore");
PyObject* result = PyObject_CallFunction(console_print_handler, "O", text_string);

if (result == NULL)
DebugError("PyObject_CallFunction() returned NULL.\n",
Expand All @@ -212,6 +219,7 @@ char* ConsolePrintDispatcher(char* text) {
else if (PyUnicode_Check(result))
ret = PyUnicode_AsUTF8(result);

Py_XDECREF(text_string);
Py_XDECREF(result);

PyGILState_Release(gstate);
Expand Down
9 changes: 5 additions & 4 deletions python_embed.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static PyObject* makePlayerDict(int client_id) {
Py_DECREF(state);

// USERINFO
PyObject* userinfo = PyUnicode_FromString(svs->clients[client_id].userinfo);
PyObject* userinfo = PyUnicode_DecodeUTF8(svs->clients[client_id].userinfo, strlen(svs->clients[client_id].userinfo), "ignore");
if (PyDict_SetItemString(ret, "userinfo", userinfo) == -1) {
DebugError("Failed to add 'userinfo' to the dictionary.\n",
__FILE__, __LINE__, __func__);
Expand All @@ -109,7 +109,8 @@ static PyObject* makePlayerDict(int client_id) {

if (g_entities[client_id].client) {
// NAME
PyObject* name = PyUnicode_FromString(g_entities[client_id].client->pers.netname);
PyObject* name = PyUnicode_DecodeUTF8(g_entities[client_id].client->pers.netname,
strlen(g_entities[client_id].client->pers.netname), "ignore");
if (PyDict_SetItemString(ret, "name", name) == -1) {
DebugError("Failed to add 'name' to the dictionary.\n",
__FILE__, __LINE__, __func__);
Expand Down Expand Up @@ -210,7 +211,7 @@ static PyObject* PyMinqlx_GetUserinfo(PyObject* self, PyObject* args) {
else if (allow_free_client != i && svs->clients[i].state == CS_FREE)
Py_RETURN_NONE;

return PyUnicode_FromString(svs->clients[i].userinfo);
return PyUnicode_DecodeUTF8(svs->clients[i].userinfo, strlen(svs->clients[i].userinfo), "ignore");
}

/*
Expand Down Expand Up @@ -419,7 +420,7 @@ static PyObject* PyMinqlx_GetConfigstring(PyObject* self, PyObject* args) {
}

SV_GetConfigstring(i, csbuffer, sizeof(csbuffer));
return PyUnicode_FromString(csbuffer);
return PyUnicode_DecodeUTF8(csbuffer, strlen(csbuffer), "ignore");
}

/*
Expand Down

0 comments on commit 0955da0

Please sign in to comment.