Skip to content

Commit

Permalink
Use PATH_MAX if possible, instead of arbitrarily-sized buffers.
Browse files Browse the repository at this point in the history
  Fixes Bugzilla #840.
  • Loading branch information
icculus committed Oct 12, 2009
1 parent 5718f33 commit 75e512a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
12 changes: 6 additions & 6 deletions timidity/common.c
Expand Up @@ -34,7 +34,7 @@
/* I guess "rb" should be right for any libc */
#define OPEN_MODE "rb"

char current_filename[1024];
char current_filename[PATH_MAX];

static PathList *pathlist=NULL;

Expand All @@ -55,7 +55,7 @@ static FILE *try_to_open(const char *name, int decompress, int noise_mode)
int l,el;
static char *decompressor_list[] = DECOMPRESSOR_LIST, **dec;
const char *cp;
char tmp[1024], tmp2[1024], *cp2;
char tmp[PATH_MAX], tmp2[PATH_MAX], *cp2;
/* Check if it's a compressed file */
l=strlen(name);
for (dec=decompressor_list; *dec; dec+=2)
Expand Down Expand Up @@ -127,8 +127,8 @@ FILE *open_file(const char *name, int decompress, int noise_mode)

/* First try the given name */

strncpy(current_filename, name, 1023);
current_filename[1023]='\0';
strncpy(current_filename, name, PATH_MAX - 1);
current_filename[PATH_MAX - 1]='\0';

ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename);
if ((fp=try_to_open(current_filename, decompress, noise_mode)))
Expand Down Expand Up @@ -193,11 +193,11 @@ void close_file(FILE *fp)
void skip(FILE *fp, size_t len)
{
size_t c;
char tmp[1024];
char tmp[PATH_MAX];
while (len>0)
{
c=len;
if (c>1024) c=1024;
if (c>PATH_MAX) c=PATH_MAX;
len-=c;
if (c!=fread(tmp, 1, c, fp))
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: skip: %s",
Expand Down
6 changes: 6 additions & 0 deletions timidity/common.h
Expand Up @@ -21,6 +21,12 @@
common.h
*/

#include <limits.h>

#ifndef PATH_MAX /* GNU Hurd doesn't limit path size, thus no PATH_MAX... */
#define PATH_MAX 1024 /* ...so we'll just impose an arbitrary limit. */
#endif

extern char *program_name, current_filename[];

extern FILE *msgfp;
Expand Down
4 changes: 2 additions & 2 deletions timidity/instrum.c
Expand Up @@ -278,9 +278,9 @@ static InstrumentLayer *load_instrument(const char *name, int font_type, int per
/* Try with various extensions */
for (i=0; patch_ext[i]; i++)
{
if (strlen(name)+strlen(patch_ext[i])<1024)
if (strlen(name)+strlen(patch_ext[i])<PATH_MAX)
{
char path[1024];
char path[PATH_MAX];
strcpy(path, name);
strcat(path, patch_ext[i]);
if ((fp=open_file(path, 1, OF_NORMAL)) != NULL)
Expand Down
2 changes: 1 addition & 1 deletion timidity/timidity.c
Expand Up @@ -48,7 +48,7 @@ int num_ochannels;
static int read_config_file(const char *name)
{
FILE *fp;
char tmp[1024], *w[MAXWORDS], *cp;
char tmp[PATH_MAX], *w[MAXWORDS], *cp;
ToneBank *bank=0;
int i, j, k, line=0, words;
static int rcf_count=0;
Expand Down

0 comments on commit 75e512a

Please sign in to comment.