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

@ -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);
}