Blob Blame History Raw
From 7bff444a2b06a4a2ef5eb05f9206d0672f73053a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Tue, 14 Aug 2018 15:57:54 +0200
Subject: [PATCH] Avoid open-coded strdup that causes a warning

/home/zbyszek/python/moose/moose-core/pymoose/mfield.cpp: In function 'int moose_Field_init(_Field*, PyObject*, PyObject*)':
/home/zbyszek/python/moose/moose-core/pymoose/mfield.cpp:122:12: warning: 'char* strncpy(char*, const char*, size_t)' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]
     strncpy(name, fieldName, size);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
/home/zbyszek/python/moose/moose-core/pymoose/mfield.cpp:120:25: note: length computed here
     size_t size = strlen(fieldName);
                   ~~~~~~^~~~~~~~~~~

Add error handling while at it.
---
 moose-core/pymoose/mfield.cpp | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/moose-core/pymoose/mfield.cpp b/moose-core/pymoose/mfield.cpp
index dd452890f2..e16837c741 100644
--- a/pymoose/mfield.cpp
+++ b/pymoose/mfield.cpp
@@ -116,11 +116,12 @@ int moose_Field_init(_Field * self, PyObject * args, PyObject * kwargs)
     }
     self->owner = ((_ObjId*)owner);
     Py_INCREF(self->owner);
-    ObjId tmp = ((_ObjId*)owner)->oid_;
-    size_t size = strlen(fieldName);
-    char * name = (char*)calloc(size+1, sizeof(char));
-    strncpy(name, fieldName, size);
-    self->name = name;
+    self->name = strdup(fieldName);
+    if (!self->name) {
+        PyErr_NoMemory();
+        return -1;
+    }
+
     // In earlier version I tried to deallocate the existing
     // self->name if it is not NULL. But it turns out that it
     // causes a SIGABRT. In any case it should not be an issue as