Refactoring: avoid create an empty strings.

* (name_quote): return NULL instead of an empty string.
  * (fake_name_quote): likewise.
  * (expand_format): likewise.
  * Related changes.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2023-10-14 18:25:29 +03:00
parent 44b5accbf7
commit d4e59aa022
9 changed files with 155 additions and 64 deletions

View File

@ -256,6 +256,9 @@ name_quote (const char *s, gboolean quote_percent)
{
GString *ret;
if (s == NULL || *s == '\0')
return NULL;
ret = g_string_sized_new (64);
if (*s == '-')
@ -305,7 +308,7 @@ name_quote (const char *s, gboolean quote_percent)
g_string_append_c (ret, *s);
}
return g_string_free (ret, FALSE);
return g_string_free (ret, ret->len == 0);
}
/* --------------------------------------------------------------------------------------------- */
@ -314,7 +317,8 @@ char *
fake_name_quote (const char *s, gboolean quote_percent)
{
(void) quote_percent;
return g_strdup (s);
return (s == NULL || *s == '\0' ? NULL : g_strdup (s));
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -262,15 +262,20 @@ static char *
edit_get_filter (const vfs_path_t * filename_vpath)
{
int i;
char *p, *quoted_name;
char *quoted_name;
char *p = NULL;
i = edit_find_filter (filename_vpath);
if (i < 0)
return NULL;
quoted_name = name_quote (vfs_path_as_str (filename_vpath), FALSE);
p = g_strdup_printf (all_filters[i].read, quoted_name);
g_free (quoted_name);
if (quoted_name != NULL)
{
p = g_strdup_printf (all_filters[i].read, quoted_name);
g_free (quoted_name);
}
return p;
}
@ -1827,7 +1832,8 @@ edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * f
{
int i;
const char *write_name;
char *p, *write_name_quoted;
char *write_name_quoted;
char *p = NULL;
i = edit_find_filter (filename_vpath);
if (i < 0)
@ -1835,8 +1841,11 @@ edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * f
write_name = vfs_path_get_last_path_str (write_name_vpath);
write_name_quoted = name_quote (write_name, FALSE);
p = g_strdup_printf (all_filters[i].write, write_name_quoted);
g_free (write_name_quoted);
if (write_name_quoted != NULL)
{
p = g_strdup_printf (all_filters[i].write, write_name_quoted);
g_free (write_name_quoted);
}
return p;
}

View File

@ -631,15 +631,28 @@ static void
pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
{
FILE *p = 0;
char *s;
char *s = NULL;
to = name_quote (to, FALSE);
subject = name_quote (subject, FALSE);
cc = name_quote (cc, FALSE);
s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
g_free (to);
g_free (subject);
g_free (cc);
if (to != NULL)
{
subject = name_quote (subject, FALSE);
if (subject != NULL)
{
cc = name_quote (cc, FALSE);
if (cc == NULL)
s = g_strdup_printf ("mail -s %s %s", subject, to);
else
{
s = g_strdup_printf ("mail -s %s -c %s %s", subject, cc, to);
g_free (cc);
}
g_free (subject);
}
g_free (to);
}
if (s != NULL)
{

View File

@ -139,8 +139,11 @@ enter (WInput * lc_cmdline)
char *s;
s = expand_format (NULL, cmd[++i], TRUE);
g_string_append (command, s);
g_free (s);
if (s != NULL)
{
g_string_append (command, s);
g_free (s);
}
}
}
@ -248,8 +251,11 @@ command_insert (WInput * in, const char *text, gboolean insert_extra_space)
char *quoted_text;
quoted_text = name_quote (text, TRUE);
input_insert (in, quoted_text, insert_extra_space);
g_free (quoted_text);
if (quoted_text != NULL)
{
input_insert (in, quoted_text, insert_extra_space);
g_free (quoted_text);
}
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -331,15 +331,19 @@ exec_make_shell_string (const char *lc_data, const vfs_path_t * filename_vpath)
}
}
if (!is_cd)
g_string_append (shell_string, text);
else
if (text != NULL)
{
strcpy (pbuffer, text);
pbuffer = strchr (pbuffer, 0);
if (!is_cd)
g_string_append (shell_string, text);
else
{
strcpy (pbuffer, text);
pbuffer = strchr (pbuffer, '\0');
}
g_free (text);
}
g_free (text);
written_nonspace = TRUE;
}
}
@ -593,12 +597,15 @@ get_popen_information (const char *cmd_file, const char *args, char *buf, int bu
static int
get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
{
char *tmp;
int ret;
char *filename_quoted;
int ret = 0;
tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
g_free (tmp);
filename_quoted = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
if (filename_quoted != NULL)
{
ret = get_popen_information (FILE_CMD, filename_quoted, buf, buflen);
g_free (filename_quoted);
}
return ret;
}
@ -613,18 +620,28 @@ get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
static int
get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
{
char *tmp, *lang, *args;
int ret;
char *filename_quoted;
int ret = 0;
tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
lang = name_quote (autodetect_codeset, FALSE);
args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
filename_quoted = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
if (filename_quoted != NULL)
{
char *lang;
ret = get_popen_information ("enca", args, buf, buflen);
lang = name_quote (autodetect_codeset, FALSE);
if (lang != NULL)
{
char *args;
g_free (args);
g_free (lang);
g_free (tmp);
args = g_strdup_printf (" -L %s -i %s", lang, filename_quoted);
g_free (lang);
ret = get_popen_information ("enca", args, buf, buflen);
g_free (args);
}
g_free (filename_quoted);
}
return ret;
}

View File

@ -2923,6 +2923,7 @@ static gboolean
do_enter_on_file_entry (WPanel * panel, file_entry_t * fe)
{
const char *fname = fe->fname->str;
char *fname_quoted;
vfs_path_t *full_name_vpath;
gboolean ok;
@ -2973,12 +2974,14 @@ do_enter_on_file_entry (WPanel * panel, file_entry_t * fe)
return confirm_execute || (ret == 0);
}
fname_quoted = name_quote (fname, FALSE);
if (fname_quoted != NULL)
{
char *tmp, *cmd;
char *cmd;
cmd = g_strconcat ("." PATH_SEP_STR, fname_quoted, (char *) NULL);
g_free (fname_quoted);
tmp = name_quote (fname, FALSE);
cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
g_free (tmp);
shell_execute (cmd, 0);
g_free (cmd);
}

View File

@ -89,6 +89,9 @@ strip_ext (char *ss)
char *s;
char *e = NULL;
if (ss == NULL)
return NULL;
for (s = ss; *s != '\0'; s++)
{
if (*s == '.')
@ -100,7 +103,7 @@ strip_ext (char *ss)
if (e != NULL)
*e = '\0';
return ss;
return (*ss == '\0' ? NULL : ss);
}
/* --------------------------------------------------------------------------------------------- */
@ -502,8 +505,11 @@ execute_menu_command (const Widget * edit_widget, const char *commands, gboolean
char *tmp;
tmp = name_quote (parameter, FALSE);
fputs (tmp, cmd_file);
g_free (tmp);
if (tmp != NULL)
{
fputs (tmp, cmd_file);
g_free (tmp);
}
}
else
fputs (parameter, cmd_file);
@ -529,8 +535,11 @@ execute_menu_command (const Widget * edit_widget, const char *commands, gboolean
char *text;
text = expand_format (edit_widget, *commands, do_quote);
fputs (text, cmd_file);
g_free (text);
if (text != NULL)
{
fputs (text, cmd_file);
g_free (text);
}
}
}
else if (*commands == '%')
@ -780,7 +789,7 @@ expand_format (const Widget * edit_widget, char c, gboolean do_quote)
else
{
if (get_other_type () != view_listing)
return g_strdup ("");
return NULL;
panel = other_panel;
}
@ -801,7 +810,7 @@ expand_format (const Widget * edit_widget, char c, gboolean do_quote)
default:
/* other modes don't use formats */
return g_strdup ("");
return NULL;
}
if (do_quote)
@ -912,31 +921,35 @@ expand_format (const Widget * edit_widget, char c, gboolean do_quote)
case 't':
case 'u':
{
GString *block;
GString *block = NULL;
int i;
if (panel == NULL)
{
result = g_strdup ("");
result = NULL;
goto ret;
}
block = g_string_sized_new (16);
for (i = 0; i < panel->dir.len; i++)
if (panel->dir.list[i].f.marked != 0)
{
char *tmp;
tmp = quote_func (panel->dir.list[i].fname->str, FALSE);
g_string_append (block, tmp);
g_string_append_c (block, ' ');
g_free (tmp);
if (tmp != NULL)
{
if (block == NULL)
block = g_string_new (tmp);
else
g_string_append (block, tmp);
g_string_append_c (block, ' ');
g_free (tmp);
}
if (c_lc == 'u')
do_file_mark (panel, i, 0);
}
result = g_string_free (block, FALSE);
result = block == NULL ? NULL : g_string_free (block, block->len == 0);
goto ret;
} /* sub case block */
default:

View File

@ -918,8 +918,7 @@ extfs_cmd (const char *str_extfs_cmd, const struct extfs_super_t *archive,
char *quoted_file;
char *quoted_localname;
char *archive_name, *quoted_archive_name;
const extfs_plugin_info_t *info;
char *cmd;
char *cmd = NULL;
int retval = 0;
GError *error = NULL;
mc_pipe_t *pip;
@ -928,20 +927,45 @@ extfs_cmd (const char *str_extfs_cmd, const struct extfs_super_t *archive,
quoted_file = name_quote (file, FALSE);
g_free (file);
if (quoted_file == NULL)
{
message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\nwrong file name"));
return (-1);
}
/* Skip leading "./" (if present) added in name_quote() */
file = extfs_skip_leading_dotslash (quoted_file);
archive_name = extfs_get_archive_name (archive);
quoted_archive_name = name_quote (archive_name, FALSE);
g_free (archive_name);
if (quoted_archive_name == NULL)
{
message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\nwrong archive name"));
return (-1);
}
quoted_localname = name_quote (localname, FALSE);
info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype);
cmd = g_strconcat (info->path, info->prefix, str_extfs_cmd,
quoted_archive_name, " ", file, " ", quoted_localname, (char *) NULL);
if (quoted_localname != NULL)
{
const extfs_plugin_info_t *info;
info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype);
cmd = g_strconcat (info->path, info->prefix, str_extfs_cmd,
quoted_archive_name, " ", file, " ", quoted_localname, (char *) NULL);
g_free (quoted_localname);
}
g_free (quoted_file);
g_free (quoted_localname);
g_free (quoted_archive_name);
if (cmd == NULL)
{
message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\ncannot build command"));
return (-1);
}
/* don't read stdout */
pip = mc_popen (cmd, FALSE, TRUE, &error);
g_free (cmd);

View File

@ -91,6 +91,8 @@ static const struct data_source2
const char *expected_string;
} data_source2[] =
{
{NULL, NULL},
{"", NULL},
{"-", "./-"},
{"blabla-", "blabla-"},
{"\r\n\t", "\\\r\\\n\\\t"},