X-Git-Url: https://ncurses.scripts.mit.edu/?a=blobdiff_plain;f=ncurses%2Fbase%2Flib_screen.c;h=695ed30e7e1000b98011a70b6c0624921ab6ca58;hb=03f728e5bb3630a54fffc4a2ff2f8dbfcce9088e;hp=176edc4e43c04d2c20a0e6c1933e048997bf964e;hpb=d4ede7c5fa6a269d338fea32cd93bf39083dda8c;p=ncurses.git diff --git a/ncurses/base/lib_screen.c b/ncurses/base/lib_screen.c index 176edc4e..695ed30e 100644 --- a/ncurses/base/lib_screen.c +++ b/ncurses/base/lib_screen.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc. * + * Copyright (c) 1998-2009,2011 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 * @@ -39,7 +39,7 @@ #define CUR SP_TERMTYPE #endif -MODULE_ID("$Id: lib_screen.c,v 1.35 2009/06/06 20:26:17 tom Exp $") +MODULE_ID("$Id: lib_screen.c,v 1.41 2011/10/22 15:03:11 tom Exp $") #define MAX_SIZE 0x3fff /* 16k is big enough for a window or pad */ @@ -49,20 +49,25 @@ NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep) WINDOW tmp, *nwin; int n; - T((T_CALLED("getwin(%p)"), filep)); + T((T_CALLED("getwin(%p)"), (void *) filep)); + if (filep == 0) { + returnWin(0); + } clearerr(filep); - (void) fread(&tmp, sizeof(WINDOW), 1, filep); - if (ferror(filep) + if (fread(&tmp, (size_t) 1, sizeof(WINDOW), filep) < sizeof(WINDOW) + || ferror(filep) || tmp._maxy == 0 || tmp._maxy > MAX_SIZE || tmp._maxx == 0 - || tmp._maxx > MAX_SIZE) + || tmp._maxx > MAX_SIZE) { returnWin(0); + } if (tmp._flags & _ISPAD) { nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx - tmp._maxy + 1, tmp._maxx + 1); + tmp._maxy + 1, + tmp._maxx + 1); } else { nwin = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx tmp._maxy + 1, @@ -75,6 +80,8 @@ NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep) * made sense is probably gone. */ if (nwin != 0) { + size_t linesize = sizeof(NCURSES_CH_T) * (size_t) (tmp._maxx + 1); + nwin->_curx = tmp._curx; nwin->_cury = tmp._cury; nwin->_maxy = tmp._maxy; @@ -106,11 +113,8 @@ NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep) for (n = 0; n <= nwin->_maxy; n++) { clearerr(filep); - (void) fread(nwin->_line[n].text, - sizeof(NCURSES_CH_T), - (size_t) (nwin->_maxx + 1), - filep); - if (ferror(filep)) { + if (fread(nwin->_line[n].text, (size_t) 1, linesize, filep) < linesize + || ferror(filep)) { delwin(nwin); returnWin(0); } @@ -134,13 +138,13 @@ putwin(WINDOW *win, FILE *filep) int code = ERR; int n; - T((T_CALLED("putwin(%p,%p)"), win, filep)); + T((T_CALLED("putwin(%p,%p)"), (void *) win, (void *) filep)); if (win != 0) { size_t len = (size_t) (win->_maxx + 1); clearerr(filep); - if (fwrite(win, sizeof(WINDOW), 1, filep) != 1 + if (fwrite(win, sizeof(WINDOW), (size_t) 1, filep) != 1 || ferror(filep)) returnCode(code); @@ -161,7 +165,7 @@ NCURSES_SP_NAME(scr_restore) (NCURSES_SP_DCLx const char *file) { FILE *fp = 0; - T((T_CALLED("scr_restore(%p,%s)"), SP_PARM, _nc_visbuf(file))); + T((T_CALLED("scr_restore(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file))); if (_nc_access(file, R_OK) < 0 || (fp = fopen(file, "rb")) == 0) { @@ -188,18 +192,20 @@ scr_restore(const char *file) NCURSES_EXPORT(int) scr_dump(const char *file) { + int result; FILE *fp = 0; T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file))); if (_nc_access(file, W_OK) < 0 || (fp = fopen(file, "wb")) == 0) { - returnCode(ERR); + result = ERR; } else { (void) putwin(newscr, fp); (void) fclose(fp); - returnCode(OK); + result = OK; } + returnCode(result); } NCURSES_EXPORT(int) @@ -208,7 +214,7 @@ NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file) FILE *fp = 0; int code = ERR; - T((T_CALLED("scr_init(%p,%s)"), SP_PARM, _nc_visbuf(file))); + T((T_CALLED("scr_init(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file))); if (SP_PARM != 0 && #ifdef USE_TERM_DRIVER @@ -242,7 +248,7 @@ scr_init(const char *file) NCURSES_EXPORT(int) NCURSES_SP_NAME(scr_set) (NCURSES_SP_DCLx const char *file) { - T((T_CALLED("scr_set(%p,%s)"), SP_PARM, _nc_visbuf(file))); + T((T_CALLED("scr_set(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file))); if (NCURSES_SP_NAME(scr_init) (NCURSES_SP_ARGx file) == ERR) { returnCode(ERR);