X-Git-Url: http://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Ftinfo%2Fread_entry.c;h=abc8a7655d75582b7ee464c516765e9e810deb2d;hp=85dc3e0bf6e9a4acfcfdf1872f6ce3a5911ae540;hb=55ccd2b959766810cf7db8d1c4462f338ce0afc8;hpb=b1f61d9f3aa244512045a6b02e759825d7049d34;ds=sidebyside diff --git a/ncurses/tinfo/read_entry.c b/ncurses/tinfo/read_entry.c index 85dc3e0b..abc8a765 100644 --- a/ncurses/tinfo/read_entry.c +++ b/ncurses/tinfo/read_entry.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * + * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -29,6 +29,7 @@ /**************************************************************************** * Author: Zeyd M. Ben-Halim 1992,1995 * * and: Eric S. Raymond * + * and: Thomas E. Dickey 1996-on * ****************************************************************************/ /* @@ -41,12 +42,14 @@ #include #include -MODULE_ID("$Id: read_entry.c,v 1.67 2000/03/11 12:35:45 tom Exp $") +MODULE_ID("$Id: read_entry.c,v 1.81 2005/06/02 22:04:32 tom Exp $") #if !HAVE_TELL -#define tell(fd) 0 /* lseek() is POSIX, but not tell() - odd... */ +#define tell(fd) lseek(fd, 0, SEEK_CUR) /* lseek() is POSIX, but not tell() */ #endif +#define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts)) + /* * int * _nc_read_file_entry(filename, ptr) @@ -70,7 +73,7 @@ static bool keep_tic_directory = FALSE; * Record the "official" location of the terminfo directory, according to * the place where we're writing to, or the normal default, if not. */ -const char * +NCURSES_EXPORT(const char *) _nc_tic_dir(const char *path) { static const char *result = TERMINFO; @@ -79,7 +82,7 @@ _nc_tic_dir(const char *path) if (path != 0) { result = path; have_tic_directory = TRUE; - } else if (!have_tic_directory) { + } else if (!have_tic_directory && use_terminfo_vars()) { char *envp; if ((envp = getenv("TERMINFO")) != 0) return _nc_tic_dir(envp); @@ -93,7 +96,7 @@ _nc_tic_dir(const char *path) * has chdir'd to it. If we let it be changed, then if $TERMINFO has a * relative path, we'll lose track of the actual directory. */ -void +NCURSES_EXPORT(void) _nc_keep_tic_dir(const char *path) { _nc_tic_dir(path); @@ -145,20 +148,22 @@ convert_strings(char *buf, char **Strings, int count, int size, char *table) } } -#define read_shorts(fd, buf, count) (read(fd, buf, (count)*2) == (count)*2) +#define read_shorts(fd, buf, count) \ + (read(fd, buf, (unsigned) (count)*2) == (int) (count)*2) #define even_boundary(value) \ if ((value) % 2 != 0) read(fd, buf, 1) static int -read_termtype(int fd, TERMTYPE * ptr) +read_termtype(int fd, TERMTYPE *ptr) /* return 1 if read, 0 if not found or garbled */ { int name_size, bool_count, num_count, str_count, str_size; int i; - char buf[MAX_ENTRY_SIZE]; + char buf[MAX_ENTRY_SIZE + 1]; + unsigned want, have; - TR(TRACE_DATABASE, ("READ termtype header @%d", tell(fd))); + TR(TRACE_DATABASE, ("READ termtype header @%ld", (long) tell(fd))); memset(ptr, 0, sizeof(*ptr)); @@ -168,7 +173,6 @@ read_termtype(int fd, TERMTYPE * ptr) return (0); } - _nc_free_termtype(ptr); name_size = LOW_MSB(buf + 2); bool_count = LOW_MSB(buf + 4); num_count = LOW_MSB(buf + 6); @@ -176,9 +180,9 @@ read_termtype(int fd, TERMTYPE * ptr) str_size = LOW_MSB(buf + 10); TR(TRACE_DATABASE, - ("TERMTYPE name_size=%d, bool=%d/%d, num=%d/%d str=%d/%d(%d)", - name_size, bool_count, BOOLCOUNT, num_count, NUMCOUNT, - str_count, STRCOUNT, str_size)); + ("TERMTYPE name_size=%d, bool=%d/%d, num=%d/%d str=%d/%d(%d)", + name_size, bool_count, BOOLCOUNT, num_count, NUMCOUNT, + str_count, STRCOUNT, str_size)); if (name_size < 0 || bool_count < 0 || num_count < 0 @@ -197,19 +201,22 @@ read_termtype(int fd, TERMTYPE * ptr) str_count = 0; } - /* grab the name (a null-terminate string) */ - read(fd, buf, min(MAX_NAME_SIZE, (unsigned) name_size)); - buf[MAX_NAME_SIZE] = '\0'; - ptr->term_names = typeCalloc(char, strlen(buf) + 1); + /* grab the name (a null-terminated string) */ + want = min(MAX_NAME_SIZE, (unsigned) name_size); + if ((have = read(fd, buf, want)) != want) { + memset(buf + have, 0, want - have); + } + buf[want] = '\0'; + ptr->term_names = TYPE_CALLOC(char, strlen(buf) + 1); if (ptr->term_names == NULL) { return (0); } (void) strcpy(ptr->term_names, buf); - if (name_size > MAX_NAME_SIZE) - lseek(fd, (off_t) (name_size - MAX_NAME_SIZE), 1); + if (have > MAX_NAME_SIZE) + lseek(fd, (off_t) (have - MAX_NAME_SIZE), 1); /* grab the booleans */ - if ((ptr->Booleans = typeCalloc(char, max(BOOLCOUNT, bool_count))) == 0 + if ((ptr->Booleans = TYPE_CALLOC(char, max(BOOLCOUNT, bool_count))) == 0 || read(fd, ptr->Booleans, (unsigned) bool_count) < bool_count) { return (0); } @@ -223,13 +230,13 @@ read_termtype(int fd, TERMTYPE * ptr) even_boundary(name_size + bool_count); /* grab the numbers */ - if ((ptr->Numbers = typeCalloc(short, max(NUMCOUNT, num_count))) == 0 + if ((ptr->Numbers = TYPE_CALLOC(short, max(NUMCOUNT, num_count))) == 0 || !read_shorts(fd, buf, num_count)) { return (0); } convert_shorts(buf, ptr->Numbers, num_count); - if ((ptr->Strings = typeCalloc(char *, max(STRCOUNT, str_count))) == 0) + if ((ptr->Strings = TYPE_CALLOC(char *, max(STRCOUNT, str_count))) == 0) return (0); if (str_count) { @@ -252,17 +259,17 @@ read_termtype(int fd, TERMTYPE * ptr) * Read extended entries, if any, after the normal end of terminfo data. */ even_boundary(str_size); - TR(TRACE_DATABASE, ("READ extended_header @%d", tell(fd))); + TR(TRACE_DATABASE, ("READ extended_header @%ld", (long) tell(fd))); if (_nc_user_definable && read_shorts(fd, buf, 5)) { int ext_bool_count = LOW_MSB(buf + 0); int ext_num_count = LOW_MSB(buf + 2); int ext_str_count = LOW_MSB(buf + 4); int ext_str_size = LOW_MSB(buf + 6); int ext_str_limit = LOW_MSB(buf + 8); - int need = (ext_bool_count + ext_num_count + ext_str_count); + unsigned need = (ext_bool_count + ext_num_count + ext_str_count); int base = 0; - if (need >= (int) sizeof(buf) + if (need >= sizeof(buf) || ext_str_size >= (int) sizeof(buf) || ext_str_limit >= (int) sizeof(buf) || ext_bool_count < 0 @@ -281,19 +288,20 @@ read_termtype(int fd, TERMTYPE * ptr) ptr->Strings = typeRealloc(char *, ptr->num_Strings, ptr->Strings); TR(TRACE_DATABASE, ("extended header is %d/%d/%d(%d:%d)", - ext_bool_count, ext_num_count, ext_str_count, ext_str_size, ext_str_limit)); + ext_bool_count, ext_num_count, ext_str_count, + ext_str_size, ext_str_limit)); - TR(TRACE_DATABASE, ("READ %d extended-booleans @%d", - ext_bool_count, tell(fd))); + TR(TRACE_DATABASE, ("READ %d extended-booleans @%ld", + ext_bool_count, (long) tell(fd))); if ((ptr->ext_Booleans = ext_bool_count) != 0) { if (read(fd, ptr->Booleans + BOOLCOUNT, (unsigned) - ext_bool_count) != ext_bool_count) + ext_bool_count) != ext_bool_count) return (0); } even_boundary(ext_bool_count); - TR(TRACE_DATABASE, ("READ %d extended-numbers @%d", - ext_num_count, tell(fd))); + TR(TRACE_DATABASE, ("READ %d extended-numbers @%ld", + ext_num_count, (long) tell(fd))); if ((ptr->ext_Numbers = ext_num_count) != 0) { if (!read_shorts(fd, buf, ext_num_count)) return (0); @@ -301,62 +309,64 @@ read_termtype(int fd, TERMTYPE * ptr) convert_shorts(buf, ptr->Numbers + NUMCOUNT, ext_num_count); } - TR(TRACE_DATABASE, ("READ extended-offsets @%d", tell(fd))); + TR(TRACE_DATABASE, ("READ extended-offsets @%ld", (long) tell(fd))); if ((ext_str_count || need) && !read_shorts(fd, buf, ext_str_count + need)) return (0); - TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%d", - ext_str_limit, tell(fd))); + TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%ld", + ext_str_limit, (long) tell(fd))); if (ext_str_limit) { if ((ptr->ext_str_table = typeMalloc(char, ext_str_limit)) == 0) return (0); - if (read(fd, ptr->ext_str_table, ext_str_limit) != ext_str_limit) + if (read(fd, ptr->ext_str_table, (unsigned) ext_str_limit) != ext_str_limit) return (0); TR(TRACE_DATABASE, ("first extended-string is %s", _nc_visbuf(ptr->ext_str_table))); } if ((ptr->ext_Strings = ext_str_count) != 0) { TR(TRACE_DATABASE, - ("Before computing extended-string capabilities str_count=%d, ext_str_count=%d", - str_count, ext_str_count)); + ("Before computing extended-string capabilities str_count=%d, ext_str_count=%d", + str_count, ext_str_count)); convert_strings(buf, ptr->Strings + str_count, ext_str_count, - ext_str_limit, ptr->ext_str_table); + ext_str_limit, ptr->ext_str_table); for (i = ext_str_count - 1; i >= 0; i--) { TR(TRACE_DATABASE, ("MOVE from [%d:%d] %s", - i, i + str_count, - _nc_visbuf(ptr->Strings[i + str_count]))); + i, i + str_count, + _nc_visbuf(ptr->Strings[i + str_count]))); ptr->Strings[i + STRCOUNT] = ptr->Strings[i + str_count]; if (VALID_STRING(ptr->Strings[i + STRCOUNT])) base += (strlen(ptr->Strings[i + STRCOUNT]) + 1); TR(TRACE_DATABASE, ("... to [%d] %s", - i + STRCOUNT, - _nc_visbuf(ptr->Strings[i + STRCOUNT]))); + i + STRCOUNT, + _nc_visbuf(ptr->Strings[i + STRCOUNT]))); } } if (need) { - if ((ptr->ext_Names = typeCalloc(char *, need)) == 0) + if ((ptr->ext_Names = TYPE_CALLOC(char *, need)) == 0) return (0); TR(TRACE_DATABASE, - ("ext_NAMES starting @%d in extended_strings, first = %s", - base, _nc_visbuf(ptr->ext_str_table + base))); - convert_strings(buf + (2 * ext_str_count), ptr->ext_Names, need, - ext_str_limit, ptr->ext_str_table + base); + ("ext_NAMES starting @%d in extended_strings, first = %s", + base, _nc_visbuf(ptr->ext_str_table + base))); + convert_strings(buf + (2 * ext_str_count), + ptr->ext_Names, + (int) need, + ext_str_limit, ptr->ext_str_table + base); } T(("...done reading terminfo bool %d(%d) num %d(%d) str %d(%d)", - ptr->num_Booleans, ptr->ext_Booleans, - ptr->num_Numbers, ptr->ext_Numbers, - ptr->num_Strings, ptr->ext_Strings)); + ptr->num_Booleans, ptr->ext_Booleans, + ptr->num_Numbers, ptr->ext_Numbers, + ptr->num_Strings, ptr->ext_Strings)); TR(TRACE_DATABASE, ("extend: num_Booleans:%d", ptr->num_Booleans)); } else #endif /* NCURSES_XNAMES */ { T(("...done reading terminfo bool %d num %d str %d", - bool_count, num_count, str_count)); + bool_count, num_count, str_count)); #if NCURSES_XNAMES TR(TRACE_DATABASE, ("normal: num_Booleans:%d", ptr->num_Booleans)); #endif @@ -372,8 +382,8 @@ read_termtype(int fd, TERMTYPE * ptr) return (1); } -int -_nc_read_file_entry(const char *const filename, TERMTYPE * ptr) +NCURSES_EXPORT(int) +_nc_read_file_entry(const char *const filename, TERMTYPE *ptr) /* return 1 if read, 0 if not found or garbled */ { int code, fd = -1; @@ -381,14 +391,15 @@ _nc_read_file_entry(const char *const filename, TERMTYPE * ptr) if (_nc_access(filename, R_OK) < 0 || (fd = open(filename, O_RDONLY | O_BINARY)) < 0) { T(("cannot open terminfo %s (errno=%d)", filename, errno)); - return (0); + code = 0; + } else { + T(("read terminfo %s", filename)); + if ((code = read_termtype(fd, ptr)) == 0) { + _nc_free_termtype(ptr); + } + close(fd); } - T(("read terminfo %s", filename)); - if ((code = read_termtype(fd, ptr)) == 0) - _nc_free_termtype(ptr); - close(fd); - return (code); } @@ -398,12 +409,11 @@ _nc_read_file_entry(const char *const filename, TERMTYPE * ptr) */ static int _nc_read_tic_entry(char *const filename, - const char *const dir, const char *ttn, TERMTYPE * const tp) + const char *const dir, const char *ttn, TERMTYPE *const tp) { -/* maximum safe length of terminfo root directory name */ -#define MAX_TPATH (PATH_MAX - MAX_ALIAS - 6) + int need = 2 + strlen(dir) + strlen(ttn); - if (strlen(dir) > MAX_TPATH) + if (need > PATH_MAX) return 0; (void) sprintf(filename, "%s/%s", dir, ttn); return _nc_read_file_entry(filename, tp); @@ -415,7 +425,7 @@ _nc_read_tic_entry(char *const filename, */ static int _nc_read_terminfo_dirs(const char *dirs, char *const filename, const char *const - ttn, TERMTYPE * const tp) + ttn, TERMTYPE *const tp) { char *list, *a; const char *b; @@ -427,7 +437,7 @@ _nc_read_terminfo_dirs(const char *dirs, char *const filename, const char *const for (;;) { int c = *a; - if (c == 0 || c == ':') { + if (c == 0 || c == NCURSES_PATHSEP) { *a = 0; if ((b + 1) >= a) b = TERMINFO; @@ -455,14 +465,22 @@ _nc_read_terminfo_dirs(const char *dirs, char *const filename, const char *const * overrun the file buffer. */ -int -_nc_read_entry(const char *const tn, char *const filename, TERMTYPE * const tp) +NCURSES_EXPORT(int) +_nc_read_entry(const char *const tn, char *const filename, TERMTYPE *const tp) { char *envp; - char ttn[MAX_ALIAS + 3]; + char ttn[PATH_MAX]; - /* truncate the terminal name to prevent dangerous buffer airline */ - (void) sprintf(ttn, "%c/%.*s", *tn, MAX_ALIAS, tn); + if (strlen(tn) == 0 + || strcmp(tn, ".") == 0 + || strcmp(tn, "..") == 0 + || _nc_pathlast(tn) != 0) { + T(("illegal or missing entry name '%s'", tn)); + return 0; + } + + /* truncate the terminal name to prevent buffer overflow */ + (void) sprintf(ttn, "%c/%.*s", *tn, (int) sizeof(ttn) - 3, tn); /* This is System V behavior, in conjunction with our requirements for * writing terminfo entries. @@ -471,19 +489,22 @@ _nc_read_entry(const char *const tn, char *const filename, TERMTYPE * const tp) && _nc_read_tic_entry(filename, _nc_tic_dir(0), ttn, tp) == 1) return 1; - if ((envp = getenv("TERMINFO")) != 0 - && _nc_read_tic_entry(filename, _nc_tic_dir(envp), ttn, tp) == 1) - return 1; + if (use_terminfo_vars()) { + if ((envp = getenv("TERMINFO")) != 0 + && _nc_read_tic_entry(filename, _nc_tic_dir(envp), ttn, tp) == 1) + return 1; - if ((envp = _nc_home_terminfo()) != 0) { - if (_nc_read_tic_entry(filename, envp, ttn, tp) == 1) { - return (1); + /* this is an ncurses extension */ + if ((envp = _nc_home_terminfo()) != 0) { + if (_nc_read_tic_entry(filename, envp, ttn, tp) == 1) { + return 1; + } } - } - /* this is an ncurses extension */ - if ((envp = getenv("TERMINFO_DIRS")) != 0) - return _nc_read_terminfo_dirs(envp, filename, ttn, tp); + /* this is an ncurses extension */ + if ((envp = getenv("TERMINFO_DIRS")) != 0) + return _nc_read_terminfo_dirs(envp, filename, ttn, tp); + } /* Try the system directory. Note that the TERMINFO_DIRS value, if * defined by the configure script, begins with a ":", which will be