From a8ec5f1d09205f5a699031271150f978ab711848 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 21 May 2009 19:38:51 +0000 Subject: [PATCH] stat(2) files before trying to load them to avoid problems, for example with "source-file /dev/zero". This commit dedicated to Tom: protecting idiots from their own stupidity for more than 20 years. --- CHANGES | 7 ++++++- cfg.c | 13 ++++++++++++- cmd-load-buffer.c | 6 +++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 2c85331c..9413b581 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +21 May 2009 + +* stat(2) files before trying to load them to avoid problems, for example + with "source-file /dev/zero". + 19 May 2009 * Try to guess if the window is UTF-8 by outputting a three-byte UTF-8 wide @@ -1282,7 +1287,7 @@ (including mutt, emacs). No status bar yet and no key remapping or other customisation. -$Id: CHANGES,v 1.295 2009-05-19 16:08:35 nicm Exp $ +$Id: CHANGES,v 1.296 2009-05-21 19:38:51 nicm Exp $ LocalWords: showw utf UTF fulvio ciriaco joshe OSC APC gettime abc DEF OA clr LocalWords: rivo nurges lscm Erdely eol smysession mysession ek dstname RB ms diff --git a/cfg.c b/cfg.c index 2a45c393..e0f3f2a3 100644 --- a/cfg.c +++ b/cfg.c @@ -1,4 +1,4 @@ -/* $Id: cfg.c,v 1.15 2009-03-31 22:23:43 nicm Exp $ */ +/* $Id: cfg.c,v 1.16 2009-05-21 19:38:51 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -55,11 +56,21 @@ load_cfg(const char *path, char **cause) { FILE *f; u_int n; + struct stat sb; char *buf, *line, *ptr; size_t len; struct cmd_list *cmdlist; struct cmd_ctx ctx; + if (stat(path, &sb) != 0) { + xasprintf(cause, "%s: %s", path, strerror(errno)); + return (-1); + } + if (!S_ISREG(sb.st_mode)) { + xasprintf(cause, "%s: not a regular file", path); + return (-1); + } + if ((f = fopen(path, "rb")) == NULL) { xasprintf(cause, "%s: %s", path, strerror(errno)); return (1); diff --git a/cmd-load-buffer.c b/cmd-load-buffer.c index fd3387b0..1be567ec 100644 --- a/cmd-load-buffer.c +++ b/cmd-load-buffer.c @@ -1,4 +1,4 @@ -/* $Id: cmd-load-buffer.c,v 1.3 2009-05-18 16:22:30 nicm Exp $ */ +/* $Id: cmd-load-buffer.c,v 1.4 2009-05-21 19:38:51 nicm Exp $ */ /* * Copyright (c) 2009 Tiago Cunha @@ -62,6 +62,10 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) ctx->error(ctx, "%s: %s", data->arg, strerror(errno)); return (-1); } + if (!S_ISREG(statbuf.st_mode)) { + ctx->error(ctx, "%s: not a regular file", data->arg); + return (-1); + } if ((f = fopen(data->arg, "rb")) == NULL) { ctx->error(ctx, "%s: %s", data->arg, strerror(errno));