comparison src/libvterm/src/vterm.c @ 15249:544490b69e1d v8.1.0633

patch 8.1.0633: crash when out of memory while opening a terminal window commit https://github.com/vim/vim/commit/cd929f7ba8cc5b6d6dcf35c8b34124e969fed6b8 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Dec 24 21:38:45 2018 +0100 patch 8.1.0633: crash when out of memory while opening a terminal window Problem: Crash when out of memory while opening a terminal window. Solution: Handle out-of-memory more gracefully.
author Bram Moolenaar <Bram@vim.org>
date Mon, 24 Dec 2018 21:45:05 +0100
parents 694594a0d25d
children 811a12a78164
comparison
equal deleted inserted replaced
15248:1e57afb3e8e9 15249:544490b69e1d
1 #define DEFINE_INLINES 1 #define DEFINE_INLINES
2 2
3 /* vim: set sw=2 : */
3 #include "vterm_internal.h" 4 #include "vterm_internal.h"
4 5
5 #include <stdio.h> 6 #include <stdio.h>
6 #include <stdlib.h> 7 #include <stdlib.h>
7 #include <stdarg.h> 8 #include <stdarg.h>
39 VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata) 40 VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata)
40 { 41 {
41 /* Need to bootstrap using the allocator function directly */ 42 /* Need to bootstrap using the allocator function directly */
42 VTerm *vt = (*funcs->malloc)(sizeof(VTerm), allocdata); 43 VTerm *vt = (*funcs->malloc)(sizeof(VTerm), allocdata);
43 44
45 if (vt == NULL)
46 return NULL;
44 vt->allocator = funcs; 47 vt->allocator = funcs;
45 vt->allocdata = allocdata; 48 vt->allocdata = allocdata;
46 49
47 vt->rows = rows; 50 vt->rows = rows;
48 vt->cols = cols; 51 vt->cols = cols;
53 vt->parser.cbdata = NULL; 56 vt->parser.cbdata = NULL;
54 57
55 vt->parser.strbuffer_len = 500; /* should be able to hold an OSC string */ 58 vt->parser.strbuffer_len = 500; /* should be able to hold an OSC string */
56 vt->parser.strbuffer_cur = 0; 59 vt->parser.strbuffer_cur = 0;
57 vt->parser.strbuffer = vterm_allocator_malloc(vt, vt->parser.strbuffer_len); 60 vt->parser.strbuffer = vterm_allocator_malloc(vt, vt->parser.strbuffer_len);
61 if (vt->parser.strbuffer == NULL)
62 {
63 vterm_allocator_free(vt, vt);
64 return NULL;
65 }
58 66
59 vt->outbuffer_len = 200; 67 vt->outbuffer_len = 200;
60 vt->outbuffer_cur = 0; 68 vt->outbuffer_cur = 0;
61 vt->outbuffer = vterm_allocator_malloc(vt, vt->outbuffer_len); 69 vt->outbuffer = vterm_allocator_malloc(vt, vt->outbuffer_len);
70 if (vt->outbuffer == NULL)
71 {
72 vterm_allocator_free(vt, vt->parser.strbuffer);
73 vterm_allocator_free(vt, vt);
74 return NULL;
75 }
62 76
63 return vt; 77 return vt;
64 } 78 }
65 79
66 void vterm_free(VTerm *vt) 80 void vterm_free(VTerm *vt)
80 INTERNAL void *vterm_allocator_malloc(VTerm *vt, size_t size) 94 INTERNAL void *vterm_allocator_malloc(VTerm *vt, size_t size)
81 { 95 {
82 return (*vt->allocator->malloc)(size, vt->allocdata); 96 return (*vt->allocator->malloc)(size, vt->allocdata);
83 } 97 }
84 98
99 /*
100 * Free "ptr" unless it is NULL.
101 */
85 INTERNAL void vterm_allocator_free(VTerm *vt, void *ptr) 102 INTERNAL void vterm_allocator_free(VTerm *vt, void *ptr)
86 { 103 {
87 (*vt->allocator->free)(ptr, vt->allocdata); 104 if (ptr)
105 (*vt->allocator->free)(ptr, vt->allocdata);
88 } 106 }
89 107
90 void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp) 108 void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp)
91 { 109 {
92 if(rowsp) 110 if(rowsp)