Skip to content

Commit

Permalink
Merge branch '4600_filter_segfault'
Browse files Browse the repository at this point in the history
* 4600_filter_segfault:
  src/filemanager/{achown,chattr,chmod,chown}.c: get rid of code duplication.
  (panel_callback) [MSG_FOCUS]: remove self-draw here.
  Disable all cursor movements if file panel is empty.
  (start_search): don't start quick search if file panel is empty.
  Clarify panel->currrent if file panel is empty.
  Ticket #4600: fix segfault on panel filter.
  • Loading branch information
aborodin committed Oct 19, 2024
2 parents 65fee2d + b232229 commit b46e813
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 173 deletions.
11 changes: 11 additions & 0 deletions src/diffviewer/ydiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -3460,6 +3460,12 @@ dview_diff_cmd (const void *f0, const void *f1)
const file_entry_t *fe0, *fe1;

fe0 = panel_current_entry (panel0);
if (fe0 == NULL)
{
message (D_ERROR, MSG_ERROR, "%s", _("File name is empty!"));
goto ret;
}

file0 = vfs_path_append_new (panel0->cwd_vpath, fe0->fname->str, (char *) NULL);
is_dir0 = S_ISDIR (fe0->st.st_mode);
if (is_dir0)
Expand All @@ -3470,6 +3476,11 @@ dview_diff_cmd (const void *f0, const void *f1)
}

fe1 = panel_current_entry (panel1);
if (fe1 == NULL)
{
message (D_ERROR, MSG_ERROR, "%s", _("File name is empty!"));
goto ret;
}
file1 = vfs_path_append_new (panel1->cwd_vpath, fe1->fname->str, (char *) NULL);
is_dir1 = S_ISDIR (fe1->st.st_mode);
if (is_dir1)
Expand Down
20 changes: 4 additions & 16 deletions src/filemanager/achown.c
Original file line number Diff line number Diff line change
Expand Up @@ -831,17 +831,6 @@ advanced_chown_done (gboolean need_update)

/* --------------------------------------------------------------------------------------------- */

static const GString *
next_file (const WPanel *panel)
{
while (panel->dir.list[current_file].f.marked == 0)
current_file++;

return panel->dir.list[current_file].fname;
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
try_advanced_chown (const vfs_path_t *p, mode_t m, uid_t u, gid_t g)
{
Expand Down Expand Up @@ -957,7 +946,7 @@ apply_advanced_chowns (WPanel *panel, vfs_path_t *vpath, struct stat *sf)
{
const GString *fname;

fname = next_file (panel);
fname = panel_find_marked_file (panel, &current_file);
vpath = vfs_path_from_str (fname->str);
ok = (mc_stat (vpath, sf) == 0);

Expand Down Expand Up @@ -1017,10 +1006,9 @@ advanced_chown_cmd (WPanel *panel)
need_update = FALSE;
end_chown = FALSE;

if (panel->marked != 0)
fname = next_file (panel); /* next marked file */
else
fname = panel_current_entry (panel)->fname; /* single file */
fname = panel_get_marked_file (panel, &current_file);
if (fname == NULL)
break;

vpath = vfs_path_from_str (fname->str);

Expand Down
20 changes: 4 additions & 16 deletions src/filemanager/chattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,17 +1087,6 @@ chattr_done (gboolean need_update)

/* --------------------------------------------------------------------------------------------- */

static const GString *
next_file (const WPanel *panel)
{
while (panel->dir.list[current_file].f.marked == 0)
current_file++;

return panel->dir.list[current_file].fname;
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
try_chattr (const vfs_path_t *p, unsigned long m)
{
Expand Down Expand Up @@ -1173,7 +1162,7 @@ chattr_apply_mask (WPanel *panel, vfs_path_t *vpath, unsigned long m)
{
const GString *fname;

fname = next_file (panel);
fname = panel_find_marked_file (panel, &current_file);
vpath = vfs_path_from_str (fname->str);
ok = (mc_fgetflags (vpath, &m) == 0);

Expand Down Expand Up @@ -1224,10 +1213,9 @@ chattr_cmd (WPanel *panel)
need_update = FALSE;
end_chattr = FALSE;

if (panel->marked != 0)
fname = next_file (panel); /* next marked file */
else
fname = panel_current_entry (panel)->fname; /* single file */
fname = panel_get_marked_file (panel, &current_file);
if (fname == NULL)
break;

vpath = vfs_path_from_str (fname->str);

Expand Down
20 changes: 4 additions & 16 deletions src/filemanager/chmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,6 @@ chmod_done (gboolean need_update)

/* --------------------------------------------------------------------------------------------- */

static const GString *
next_file (const WPanel *panel)
{
while (panel->dir.list[current_file].f.marked == 0)
current_file++;

return panel->dir.list[current_file].fname;
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
try_chmod (const vfs_path_t *p, mode_t m)
{
Expand Down Expand Up @@ -496,7 +485,7 @@ apply_mask (WPanel *panel, vfs_path_t *vpath, struct stat *sf)
{
const GString *fname;

fname = next_file (panel);
fname = panel_find_marked_file (panel, &current_file);
vpath = vfs_path_from_str (fname->str);
ok = (mc_stat (vpath, sf) == 0);

Expand Down Expand Up @@ -549,10 +538,9 @@ chmod_cmd (WPanel *panel)
need_update = FALSE;
end_chmod = FALSE;

if (panel->marked != 0)
fname = next_file (panel); /* next marked file */
else
fname = panel_current_entry (panel)->fname; /* single file */
fname = panel_get_marked_file (panel, &current_file);
if (fname == NULL)
break;

vpath = vfs_path_from_str (fname->str);

Expand Down
20 changes: 4 additions & 16 deletions src/filemanager/chown.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,6 @@ chown_done (gboolean need_update)

/* --------------------------------------------------------------------------------------------- */

static const GString *
next_file (const WPanel *panel)
{
while (panel->dir.list[current_file].f.marked == 0)
current_file++;

return panel->dir.list[current_file].fname;
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
try_chown (const vfs_path_t *p, uid_t u, gid_t g)
{
Expand Down Expand Up @@ -366,7 +355,7 @@ apply_chowns (WPanel *panel, vfs_path_t *vpath, uid_t u, gid_t g)
const GString *fname;
struct stat sf;

fname = next_file (panel);
fname = panel_find_marked_file (panel, &current_file);
vpath = vfs_path_from_str (fname->str);
ok = (mc_stat (vpath, &sf) == 0);

Expand Down Expand Up @@ -418,10 +407,9 @@ chown_cmd (WPanel *panel)
need_update = FALSE;
end_chown = FALSE;

if (panel->marked != 0)
fname = next_file (panel); /* next marked file */
else
fname = panel_current_entry (panel)->fname; /* single file */
fname = panel_get_marked_file (panel, &current_file);
if (fname == NULL)
break;

vpath = vfs_path_from_str (fname->str);

Expand Down
52 changes: 41 additions & 11 deletions src/filemanager/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ do_view_cmd (WPanel *panel, gboolean plain_view)
const file_entry_t *fe;

fe = panel_current_entry (panel);
if (fe == NULL)
return;

/* Directories are viewed by changing to them */
if (S_ISDIR (fe->st.st_mode) || link_isdir (fe))
Expand Down Expand Up @@ -576,12 +578,16 @@ view_cmd (WPanel *panel)
void
view_file_cmd (const WPanel *panel)
{
const file_entry_t *fe;
char *filename;
vfs_path_t *vpath;

fe = panel_current_entry (panel);
if (fe == NULL)
return;

filename =
input_expand_dialog (_("View file"), _("Filename:"),
MC_HISTORY_FM_VIEW_FILE, panel_current_entry (panel)->fname->str,
input_expand_dialog (_("View file"), _("Filename:"), MC_HISTORY_FM_VIEW_FILE, fe->fname->str,
INPUT_COMPLETE_FILENAMES);
if (filename == NULL)
return;
Expand Down Expand Up @@ -609,7 +615,15 @@ view_filtered_cmd (const WPanel *panel)
const char *initial_command;

if (input_is_empty (cmdline))
initial_command = panel_current_entry (panel)->fname->str;
{
const file_entry_t *fe;

fe = panel_current_entry (panel);
if (fe == NULL)
return;

initial_command = fe->fname->str;
}
else
initial_command = input_get_ctext (cmdline);

Expand Down Expand Up @@ -673,9 +687,14 @@ edit_file_at_line (const vfs_path_t *what_vpath, gboolean internal, long start_l
void
edit_cmd (const WPanel *panel)
{
const file_entry_t *fe;
vfs_path_t *fname;

fname = vfs_path_from_str (panel_current_entry (panel)->fname->str);
fe = panel_current_entry (panel);
if (fe == NULL)
return;

fname = vfs_path_from_str (fe->fname->str);
if (regex_command (fname, "Edit") == 0)
do_edit (fname);
vfs_path_free (fname, TRUE);
Expand All @@ -687,9 +706,14 @@ edit_cmd (const WPanel *panel)
void
edit_cmd_force_internal (const WPanel *panel)
{
const file_entry_t *fe;
vfs_path_t *fname;

fname = vfs_path_from_str (panel_current_entry (panel)->fname->str);
fe = panel_current_entry (panel);
if (fe == NULL)
return;

fname = vfs_path_from_str (fe->fname->str);
if (regex_command (fname, "Edit") == 0)
edit_file_at_line (fname, TRUE, 1);
vfs_path_free (fname, TRUE);
Expand Down Expand Up @@ -736,6 +760,8 @@ mkdir_cmd (WPanel *panel)
const char *name = "";

fe = panel_current_entry (panel);
if (fe == NULL)
return;

/* If 'on' then automatically fills name with current item name */
if (auto_fill_mkdir_name && !DIR_IS_DOTDOT (fe->fname->str))
Expand Down Expand Up @@ -1055,11 +1081,11 @@ swap_cmd (void)
void
link_cmd (link_type_t link_type)
{
const char *filename;
const file_entry_t *fe;

filename = panel_current_entry (current_panel)->fname->str;
if (filename != NULL)
do_link (link_type, filename);
fe = panel_current_entry (current_panel);
if (fe != NULL)
do_link (link_type, fe->fname->str);
}

/* --------------------------------------------------------------------------------------------- */
Expand All @@ -1071,6 +1097,9 @@ edit_symlink_cmd (void)
const char *p;

fe = panel_current_entry (current_panel);
if (fe == NULL)
return;

p = fe->fname->str;

if (!S_ISLNK (fe->st.st_mode))
Expand Down Expand Up @@ -1218,7 +1247,8 @@ smart_dirsize_cmd (WPanel *panel)
const file_entry_t *entry;

entry = panel_current_entry (panel);
if ((S_ISDIR (entry->st.st_mode) && DIR_IS_DOTDOT (entry->fname->str)) || panel->dirs_marked)
if ((entry != NULL && S_ISDIR (entry->st.st_mode) && DIR_IS_DOTDOT (entry->fname->str))
|| panel->dirs_marked)
dirsizes_cmd (panel);
else
single_dirsize_cmd (panel);
Expand All @@ -1233,7 +1263,7 @@ single_dirsize_cmd (WPanel *panel)

entry = panel_current_entry (panel);

if (S_ISDIR (entry->st.st_mode) && !DIR_IS_DOTDOT (entry->fname->str))
if (entry != NULL && S_ISDIR (entry->st.st_mode) && !DIR_IS_DOTDOT (entry->fname->str))
{
size_t dir_count = 0;
size_t count = 0;
Expand Down
21 changes: 18 additions & 3 deletions src/filemanager/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,8 @@ do_move_dir_dir (const WPanel *panel, file_op_total_context_t *tctx, file_op_con
static const char *
panel_get_file (const WPanel *panel)
{
const file_entry_t *fe;

if (get_current_type () == view_tree)
{
WTree *tree;
Expand All @@ -1770,7 +1772,9 @@ panel_get_file (const WPanel *panel)
return panel->dir.list[i].fname->str;
}

return panel_current_entry (panel)->fname->str;
fe = panel_current_entry (panel);

return (fe == NULL ? NULL : fe->fname->str);
}

/* --------------------------------------------------------------------------------------------- */
Expand All @@ -1782,10 +1786,18 @@ check_single_entry (const WPanel *panel, gboolean force_single, struct stat *src
gboolean ok;

if (force_single)
source = panel_current_entry (panel)->fname->str;
{
const file_entry_t *fe;

fe = panel_current_entry (panel);
source = fe == NULL ? NULL : fe->fname->str;
}
else
source = panel_get_file (panel);

if (source == NULL)
return NULL;

ok = !DIR_IS_DOTDOT (source);

if (!ok)
Expand Down Expand Up @@ -3529,9 +3541,12 @@ panel_operate (void *source_panel, FileOperation operation, gboolean force_singl
else
#endif /* ENABLE_BACKGROUND */
{
const file_entry_t *fe;

if (operation == OP_DELETE)
dialog_type = FILEGUI_DIALOG_DELETE_ITEM;
else if (single_entry && S_ISDIR (panel_current_entry (panel)->st.st_mode))
else if (single_entry
&& ((fe = panel_current_entry (panel)) == NULL ? FALSE : S_ISDIR (fe->st.st_mode)))
dialog_type = FILEGUI_DIALOG_MULTI_ITEM;
else if (single_entry || force_single)
dialog_type = FILEGUI_DIALOG_ONE_ITEM;
Expand Down
Loading

0 comments on commit b46e813

Please sign in to comment.