Remove redundant NULL checks before free()/delete (#2184)

Remove redundant NULL checks before free()/delete

The ISO C and C++ standards guarantee that it's safe to call these
on a NULL pointer, so it's not necessary for the calling code to
also check.

Fixes https://sourceforge.net/p/swig/feature-requests/70/
This commit is contained in:
Olly Betts 2022-01-29 22:03:48 +13:00 • committed by GitHub
commit 7ec2f89fe2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 44 additions and 51 deletions

View file

@ -55,14 +55,14 @@ NAME() {
return new TYPE();
}
~NAME() {
if ($self) delete $self;
delete $self;
}
#else
NAME() {
return (TYPE *) calloc(1,sizeof(TYPE));
}
~NAME() {
if ($self) free($self);
free($self);
}
#endif
}
@ -134,9 +134,9 @@ static TYPE *copy_##NAME(TYPE value) { %}
static void delete_##NAME(TYPE *obj) { %}
#ifdef __cplusplus
%{ if (obj) delete obj; %}
%{ delete obj; %}
#else
%{ if (obj) free(obj); %}
%{ free(obj); %}
#endif
%{}

View file

@ -54,14 +54,14 @@ NAME() {
return new TYPE();
}
~NAME() {
if (self) delete self;
delete self;
}
#else
NAME() {
return (TYPE *) calloc(1,sizeof(TYPE));
}
~NAME() {
if (self) free(self);
free(self);
}
#endif
}
@ -133,9 +133,9 @@ static TYPE *copy_##NAME(TYPE value) { %}
static void delete_##NAME(TYPE *self) { %}
#ifdef __cplusplus
%{ if (self) delete self; %}
%{ delete self; %}
#else
%{ if (self) free(self); %}
%{ free(self); %}
#endif
%{}

View file

@ -107,7 +107,7 @@
(size_t VECTORLENINPUT, C_TYPE *VECTORINPUT),
(int LISTLENINPUT, C_TYPE *LISTINPUT),
(size_t LISTLENINPUT, C_TYPE *LISTINPUT)
{if ($2!=NULL) SWIG_free($2);}
{SWIG_free($2);}
%enddef
@ -173,7 +173,7 @@
(int *LISTLENOUTPUT, C_TYPE **LISTOUTPUT),
(size_t *LISTLENOUTPUT, C_TYPE **LISTOUTPUT)
{
if ((*$2)!=NULL) free(*$2);
free(*$2);
}
%enddef
@ -231,7 +231,7 @@ TYPEMAP_LIST_VECTOR_INPUT_OUTPUT(const char *, SWIG_scm2str, SWIG_str02scm, stri
if ((*$2)!=NULL) {
int i;
for (i = 0; i < *$1; i++) {
if ((*$2)[i] != NULL) free((*$2)[i]);
free((*$2)[i]);
}
free(*$2);
}
@ -249,7 +249,7 @@ TYPEMAP_LIST_VECTOR_INPUT_OUTPUT(const char *, SWIG_scm2str, SWIG_str02scm, stri
if (($2)!=NULL) {
int i;
for (i = 0; i< $1; i++)
if (($2)[i] != NULL) free(($2)[i]);
free(($2)[i]);
free($2);
}
}
@ -360,7 +360,7 @@ TYPEMAP_LIST_VECTOR_INPUT_OUTPUT(const char *, SWIG_scm2str, SWIG_str02scm, stri
const C_TYPE *PARALLEL_VECTORINPUT,
C_TYPE *PARALLEL_LISTINPUT,
const C_TYPE *PARALLEL_LISTINPUT
{if ($1!=NULL) SWIG_free($1);}
{SWIG_free($1);}
%enddef
@ -422,7 +422,7 @@ TYPEMAP_LIST_VECTOR_INPUT_OUTPUT(const char *, SWIG_scm2str, SWIG_str02scm, stri
%typemap(freearg) C_TYPE **PARALLEL_VECTOROUTPUT,
C_TYPE **PARALLEL_LISTOUTPUT
{
if ((*$1)!=NULL) free(*$1);
free(*$1);
}
%enddef
@ -471,7 +471,7 @@ TYPEMAP_PARALLEL_LIST_VECTOR_INPUT_OUTPUT(const char *, SWIG_scm2str, SWIG_str02
if (($1)!=NULL) {
int i;
for (i = 0; i<*_global_list_length; i++)
if (($1)[i] != NULL) SWIG_free(($1)[i]);
SWIG_free(($1)[i]);
SWIG_free($1);
}
}
@ -482,7 +482,7 @@ TYPEMAP_PARALLEL_LIST_VECTOR_INPUT_OUTPUT(const char *, SWIG_scm2str, SWIG_str02
if ((*$1)!=NULL) {
int i;
for (i = 0; i<_global_arraylentemp; i++)
if ((*$1)[i] != NULL) free((*$1)[i]);
free((*$1)[i]);
free(*$1);
}
}

View file

@ -19,7 +19,7 @@ std::string SWIG_scm2string(SCM x) {
char* temp;
temp = SWIG_scm2str(x);
std::string s(temp);
if (temp) SWIG_free(temp);
SWIG_free(temp);
return s;
}
%}

View file

@ -30,7 +30,7 @@ namespace std {
if (scm_is_string($input)) {
tempptr = SWIG_scm2str($input);
$1.assign(tempptr);
if (tempptr) SWIG_free(tempptr);
SWIG_free(tempptr);
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}
@ -40,7 +40,7 @@ namespace std {
if (scm_is_string($input)) {
tempptr = SWIG_scm2str($input);
temp.assign(tempptr);
if (tempptr) SWIG_free(tempptr);
SWIG_free(tempptr);
$1 = &temp;
} else {
SWIG_exception(SWIG_TypeError, "string expected");
@ -51,7 +51,7 @@ namespace std {
if (scm_is_string($input)) {
tempptr = SWIG_scm2str($input);
$1 = new $*1_ltype(tempptr);
if (tempptr) SWIG_free(tempptr);
SWIG_free(tempptr);
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}
@ -73,7 +73,7 @@ namespace std {
if (scm_is_string($input)) {
char *tempptr = SWIG_scm2str($input);
$1.assign(tempptr);
if (tempptr) SWIG_free(tempptr);
SWIG_free(tempptr);
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}

View file

@ -322,8 +322,8 @@ SIMPLE_MAP(unsigned long long, scm_to_ulong_long, scm_from_ulong_long, integer);
/* SWIG_scm2str makes a malloc'ed copy of the string, so get rid of it after
the function call. */
%typemap (freearg) char * "if (must_free$argnum && $1) SWIG_free($1);";
%typemap (freearg) char **INPUT, char **BOTH "if (must_free$argnum && (*$1)) SWIG_free(*$1);"
%typemap (freearg) char * "if (must_free$argnum) SWIG_free($1);";
%typemap (freearg) char **INPUT, char **BOTH "if (must_free$argnum) SWIG_free(*$1);"
%typemap (freearg) char **OUTPUT "SWIG_free(*$1);"
/* But this shall not apply if we try to pass a single char by
@ -334,7 +334,7 @@ SIMPLE_MAP(unsigned long long, scm_to_ulong_long, scm_from_ulong_long, integer);
/* If we set a string variable, delete the old result first, unless const. */
%typemap (varin) char * {
if ($1) free($1);
free($1);
$1 = ($1_ltype) SWIG_scm2str($input);
}

View file

@ -192,10 +192,10 @@ int SWIG_read_NAME_num_array(lua_State* L,int index,TYPE *array,int size);
There probably is some compiler that its not true for, so the code is left here just in case.
#ifdef __cplusplus
#define SWIG_ALLOC_ARRAY(TYPE,LEN) new TYPE[LEN]
#define SWIG_FREE_ARRAY(PTR) if(PTR){delete[] PTR;}
#define SWIG_FREE_ARRAY(PTR) delete[] PTR
#else
#define SWIG_ALLOC_ARRAY(TYPE,LEN) (TYPE *)malloc(LEN*sizeof(TYPE))
#define SWIG_FREE_ARRAY(PTR) if(PTR){free(PTR);}
#define SWIG_FREE_ARRAY(PTR) free(PTR)
#endif
*/
%{

View file

@ -47,10 +47,8 @@ static char *SWIG_Scilab_GetFuncName(void) {
return SwigFuncName;
}
static void SWIG_Scilab_SetFuncName(char *funcName) {
if (SwigFuncName != NULL) {
free(SwigFuncName);
SwigFuncName = NULL;
}
free(SwigFuncName);
SwigFuncName = NULL;
if (funcName) {
SwigFuncName = (char *)malloc(strlen(funcName) + 1);
if (SwigFuncName)

View file

@ -55,7 +55,7 @@ typedef struct {
return %new_instance(TYPE);
}
~NAME() {
if ($self) %delete($self);
%delete($self);
}
}
@ -114,7 +114,7 @@ typedef struct {
}
static void delete_##NAME(TYPE *obj) {
if (obj) %delete(obj);
%delete(obj);
}
static void NAME ##_assign(TYPE *obj, TYPE value) {