diff options
author | Dmitry Antipov <dantipov@cloudlinux.com> | 2023-03-31 08:06:44 +0300 |
---|---|---|
committer | Andrew Bartlett <abartlet@samba.org> | 2023-04-03 03:56:35 +0000 |
commit | 8720a25d57819ea51c304c9f76f84c6aa18fb2ae (patch) | |
tree | fedef244a57de1555daedb7d2424a8f1d80c98bc /source4/libnet | |
parent | 9b6f49d4b946ae436ee4d5f20613508b368f14b0 (diff) | |
download | samba-8720a25d57819ea51c304c9f76f84c6aa18fb2ae.tar.gz |
s4:libnet: cleanup py_net_time()
Fix size of buffer passed to and always check the value returned
from strftime(), raise PyErr_NoMemory() and return NULL if zero,
or use it with PyUnicode_FromStringAndSize() (thus avoiding extra
internal call to strlen()) otherwise.
Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com>
Reviewed-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Diffstat (limited to 'source4/libnet')
-rw-r--r-- | source4/libnet/py_net.c | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/source4/libnet/py_net.c b/source4/libnet/py_net.c index fe5979e7a57..42eee509c6b 100644 --- a/source4/libnet/py_net.c +++ b/source4/libnet/py_net.c @@ -310,6 +310,7 @@ static PyObject *py_net_time(py_net_Object *self, PyObject *args, PyObject *kwar char timestr[64]; PyObject *ret; struct tm *tm; + size_t len; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", discard_const_p(char *, kwnames), &r.generic.in.server_name)) @@ -335,12 +336,16 @@ static PyObject *py_net_time(py_net_Object *self, PyObject *args, PyObject *kwar ZERO_STRUCT(timestr); tm = localtime(&r.generic.out.time); - strftime(timestr, sizeof(timestr)-1, "%c %Z",tm); - - ret = PyUnicode_FromString(timestr); - talloc_free(mem_ctx); + len = strftime(timestr, sizeof(timestr), "%c %Z", tm); + if (len == 0) { + PyErr_NoMemory(); + ret = NULL; + } else { + ret = PyUnicode_FromStringAndSize(timestr, (Py_ssize_t)len); + } + talloc_free(mem_ctx); return ret; } |