1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-25 23:21:54 +03:00

introduce SAFE_FREE() macro as suggested by andreas moroder.

(This used to be commit b7edd55885)
This commit is contained in:
Simo Sorce 2001-09-17 00:47:40 +00:00
parent b0c3915d5e
commit e2d393bd24
2 changed files with 24 additions and 25 deletions

View File

@ -365,11 +365,7 @@ functions for do_list_queue
*/ */
static void reset_do_list_queue(void) static void reset_do_list_queue(void)
{ {
if (do_list_queue) SAFE_FREE(do_list_queue);
{
free(do_list_queue);
}
do_list_queue = 0;
do_list_queue_size = 0; do_list_queue_size = 0;
do_list_queue_start = 0; do_list_queue_start = 0;
do_list_queue_end = 0; do_list_queue_end = 0;
@ -707,7 +703,7 @@ static void do_get(char *rname,char *lname)
rname, (long)nread)); rname, (long)nread));
} }
free(data); SAFE_FREE(data);
if (!cli_close(cli, fnum)) { if (!cli_close(cli, fnum)) {
d_printf("Error %s closing remote file\n",cli_errstr(cli)); d_printf("Error %s closing remote file\n",cli_errstr(cli));
@ -1044,13 +1040,13 @@ static void do_put(char *rname,char *lname)
if (!cli_close(cli, fnum)) { if (!cli_close(cli, fnum)) {
d_printf("%s closing remote file %s\n",cli_errstr(cli),rname); d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
x_fclose(f); x_fclose(f);
if (buf) free(buf); SAFE_FREE(buf);
return; return;
} }
x_fclose(f); x_fclose(f);
if (buf) free(buf); SAFE_FREE(buf);
{ {
struct timeval tp_end; struct timeval tp_end;
@ -1138,8 +1134,8 @@ static void free_file_list (struct file_list * list)
{ {
tmp = list; tmp = list;
DLIST_REMOVE(list, list); DLIST_REMOVE(list, list);
if (tmp->file_path) free(tmp->file_path); SAFE_FREE(tmp->file_path);
free(tmp); SAFE_FREE(tmp);
} }
} }
@ -1209,7 +1205,7 @@ static int file_find(struct file_list **list, const char *directory,
} }
if (ret == -1) { if (ret == -1) {
free(path); SAFE_FREE(path);
closedir(dir); closedir(dir);
return -1; return -1;
} }
@ -1224,7 +1220,7 @@ static int file_find(struct file_list **list, const char *directory,
entry->isdir = isdir; entry->isdir = isdir;
DLIST_ADD(*list, entry); DLIST_ADD(*list, entry);
} else { } else {
free(path); SAFE_FREE(path);
} }
} }
@ -1260,7 +1256,7 @@ static void cmd_mput(void)
for (temp_list = file_list; temp_list; for (temp_list = file_list; temp_list;
temp_list = temp_list->next) { temp_list = temp_list->next) {
if (lname) free(lname); SAFE_FREE(lname);
if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
continue; continue;
trim_string(lname, "./", "/"); trim_string(lname, "./", "/");
@ -1269,7 +1265,7 @@ static void cmd_mput(void)
if (temp_list->isdir) { if (temp_list->isdir) {
/* if (!recurse) continue; */ /* if (!recurse) continue; */
if (quest) free(quest); SAFE_FREE(quest);
asprintf(&quest, "Put directory %s? ", lname); asprintf(&quest, "Put directory %s? ", lname);
if (prompt && !yesno(quest)) { /* No */ if (prompt && !yesno(quest)) { /* No */
/* Skip the directory */ /* Skip the directory */
@ -1277,7 +1273,7 @@ static void cmd_mput(void)
if (!seek_list(temp_list, lname)) if (!seek_list(temp_list, lname))
break; break;
} else { /* Yes */ } else { /* Yes */
if (rname) free(rname); SAFE_FREE(rname);
asprintf(&rname, "%s%s", cur_dir, lname); asprintf(&rname, "%s%s", cur_dir, lname);
dos_format(rname); dos_format(rname);
if (!cli_chkpath(cli, rname) && if (!cli_chkpath(cli, rname) &&
@ -1291,13 +1287,13 @@ static void cmd_mput(void)
} }
continue; continue;
} else { } else {
if (quest) free(quest); SAFE_FREE(quest);
asprintf(&quest,"Put file %s? ", lname); asprintf(&quest,"Put file %s? ", lname);
if (prompt && !yesno(quest)) /* No */ if (prompt && !yesno(quest)) /* No */
continue; continue;
/* Yes */ /* Yes */
if (rname) free(rname); SAFE_FREE(rname);
asprintf(&rname, "%s%s", cur_dir, lname); asprintf(&rname, "%s%s", cur_dir, lname);
} }
@ -1306,9 +1302,9 @@ static void cmd_mput(void)
do_put(rname, lname); do_put(rname, lname);
} }
free_file_list(file_list); free_file_list(file_list);
if (quest) free(quest); SAFE_FREE(quest);
if (lname) free(lname); SAFE_FREE(lname);
if (rname) free(rname); SAFE_FREE(rname);
} }
} }
@ -1872,7 +1868,7 @@ static char **completion_fn(char *text, int start, int end)
} }
if (count == 2) { if (count == 2) {
free(matches[0]); SAFE_FREE(matches[0]);
matches[0] = strdup(matches[1]); matches[0] = strdup(matches[1]);
} }
matches[count] = NULL; matches[count] = NULL;
@ -2009,7 +2005,7 @@ struct cli_state *do_connect(const char *server, const char *share)
d_printf("session request to %s failed (%s)\n", d_printf("session request to %s failed (%s)\n",
called.name, cli_errstr(c)); called.name, cli_errstr(c));
cli_shutdown(c); cli_shutdown(c);
free(c); SAFE_FREE(c);
if ((p=strchr_m(called.name, '.'))) { if ((p=strchr_m(called.name, '.'))) {
*p = 0; *p = 0;
goto again; goto again;
@ -2026,7 +2022,7 @@ struct cli_state *do_connect(const char *server, const char *share)
if (!cli_negprot(c)) { if (!cli_negprot(c)) {
d_printf("protocol negotiation failed\n"); d_printf("protocol negotiation failed\n");
cli_shutdown(c); cli_shutdown(c);
free(c); SAFE_FREE(c);
return NULL; return NULL;
} }
@ -2046,7 +2042,7 @@ struct cli_state *do_connect(const char *server, const char *share)
!cli_session_setup(c, "", "", 0, "", 0, workgroup)) { !cli_session_setup(c, "", "", 0, "", 0, workgroup)) {
d_printf("session setup failed: %s\n", cli_errstr(c)); d_printf("session setup failed: %s\n", cli_errstr(c));
cli_shutdown(c); cli_shutdown(c);
free(c); SAFE_FREE(c);
return NULL; return NULL;
} }
d_printf("Anonymous login successful\n"); d_printf("Anonymous login successful\n");
@ -2071,7 +2067,7 @@ struct cli_state *do_connect(const char *server, const char *share)
password, strlen(password)+1)) { password, strlen(password)+1)) {
d_printf("tree connect failed: %s\n", cli_errstr(c)); d_printf("tree connect failed: %s\n", cli_errstr(c));
cli_shutdown(c); cli_shutdown(c);
free(c); SAFE_FREE(c);
return NULL; return NULL;
} }

View File

@ -37,6 +37,9 @@
#define IS_DOS_SYSTEM(test_mode) (((test_mode) & aSYSTEM) != 0) #define IS_DOS_SYSTEM(test_mode) (((test_mode) & aSYSTEM) != 0)
#define IS_DOS_HIDDEN(test_mode) (((test_mode) & aHIDDEN) != 0) #define IS_DOS_HIDDEN(test_mode) (((test_mode) & aHIDDEN) != 0)
/* free memory if the pointer is valid and zero the pointer */
#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
/* zero a structure */ /* zero a structure */
#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x)) #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))