Browse Source

check whether malloc returns a valid pointer

tags/v0.9.3
Dan Lynch 18 years ago
parent
commit
916f21921f
1 changed files with 9 additions and 4 deletions
  1. 9
    4
      src/hci/mucurses/windows.c

+ 9
- 4
src/hci/mucurses/windows.c View File

51
 	WINDOW *child;
51
 	WINDOW *child;
52
 	if ( parent == NULL )
52
 	if ( parent == NULL )
53
 		return NULL;
53
 		return NULL;
54
+	if ( ( child = malloc( sizeof( WINDOW ) ) ) == NULL )
55
+		return NULL;
54
 	if ( ( (unsigned)ncols > parent->width ) || 
56
 	if ( ( (unsigned)ncols > parent->width ) || 
55
 	     ( (unsigned)nlines > parent->height ) )
57
 	     ( (unsigned)nlines > parent->height ) )
56
 		return NULL;
58
 		return NULL;
57
-	child = malloc( sizeof( WINDOW ) );
58
 	child->ori_y = parent->ori_y + begin_y;
59
 	child->ori_y = parent->ori_y + begin_y;
59
 	child->ori_x = parent->ori_x + begin_x;
60
 	child->ori_x = parent->ori_x + begin_x;
60
 	child->height = nlines;
61
 	child->height = nlines;
74
 	WINDOW *copy;
75
 	WINDOW *copy;
75
 	if ( orig == NULL )
76
 	if ( orig == NULL )
76
 		return NULL;
77
 		return NULL;
77
-	copy = malloc( sizeof( WINDOW ) );
78
+	if ( ( copy = malloc( sizeof( WINDOW ) ) ) == NULL )
79
+		return NULL;
78
 	copy->scr = orig->scr;
80
 	copy->scr = orig->scr;
79
 	copy->attrs = orig->attrs;
81
 	copy->attrs = orig->attrs;
80
 	copy->ori_y = orig->ori_y;
82
 	copy->ori_y = orig->ori_y;
117
  * @ret *win	return pointer to new window
119
  * @ret *win	return pointer to new window
118
  */
120
  */
119
 WINDOW *newwin ( int nlines, int ncols, int begin_y, int begin_x ) {
121
 WINDOW *newwin ( int nlines, int ncols, int begin_y, int begin_x ) {
120
-	WINDOW *win = malloc( sizeof(WINDOW) );
122
+	WINDOW *win;
123
+	if ( ( win = malloc( sizeof(WINDOW) ) ) == NULL )
124
+		return NULL;
121
 	if ( ( (unsigned)( begin_y + nlines ) > stdscr->height ) &&
125
 	if ( ( (unsigned)( begin_y + nlines ) > stdscr->height ) &&
122
 	     ( (unsigned)( begin_x + ncols ) > stdscr->width ) )
126
 	     ( (unsigned)( begin_x + ncols ) > stdscr->width ) )
123
 		return NULL;
127
 		return NULL;
145
 	WINDOW *child;
149
 	WINDOW *child;
146
 	if ( parent == NULL )
150
 	if ( parent == NULL )
147
 		return NULL;
151
 		return NULL;
148
-	child = malloc( sizeof( WINDOW ) );
152
+	if ( ( child = malloc( sizeof( WINDOW ) ) ) == NULL )
153
+		return NULL;
149
 	child = newwin( nlines, ncols, begin_y, begin_x );
154
 	child = newwin( nlines, ncols, begin_y, begin_x );
150
 	child->parent = parent;
155
 	child->parent = parent;
151
 	child->scr = parent->scr;
156
 	child->scr = parent->scr;

Loading…
Cancel
Save