BASIC compiler/interpreter for PIC32MX/MZ-80K
Revision | 2526d3441b908fe0e1ee3013aef738eca529c2d6 (tree) |
---|---|
Time | 2019-02-18 09:04:42 |
Author | Katsumi <kmorimatsu@sour...> |
Commiter | Katsumi |
Avoid possible invasion of allocated memory area.
@@ -110,8 +110,6 @@ void* _alloc_memory_main(int size, int var_num){ | ||
110 | 110 | while(1){ |
111 | 111 | // Try the block previously deleted, not for temporary block. |
112 | 112 | // This is for fast allocation of memory for class object. |
113 | - // Note that the temporary areas can be invaded for following purpose | |
114 | - // because these are temporary ones. | |
115 | 113 | if (var_num<26 || ALLOC_VAR_NUM<=var_num) { |
116 | 114 | candidate=0; |
117 | 115 | while(g_deleted_num){ |
@@ -136,7 +134,29 @@ void* _alloc_memory_main(int size, int var_num){ | ||
136 | 134 | candidate=g_var_pointer[i]+g_var_size[i]; |
137 | 135 | } |
138 | 136 | } |
139 | - if (candidate+size<=g_max_mem) break; | |
137 | + if (candidate+size<=g_max_mem) { | |
138 | + // Check after deleted block | |
139 | + j=candidate; | |
140 | + for(i=0;i<g_deleted_num;i++){ | |
141 | + if (j<g_deleted_pointer[i]+g_deleted_size[i]) { | |
142 | + j=g_deleted_pointer[i]+g_deleted_size[i]; | |
143 | + } | |
144 | + } | |
145 | + if (j+size<=g_max_mem) { | |
146 | + // Candidate block found after previously deleted blokcs | |
147 | + candidate=j; | |
148 | + break; | |
149 | + } else { | |
150 | + // Candidate is before previously deleted blocks, | |
151 | + // and there is no candidate block after previously deleted blocks. | |
152 | + // Therefore, use the current candidate, which may invade previously | |
153 | + // deleted blocks. Therefore, erase the previously deleted blocks list. | |
154 | + g_deleted_num=0; | |
155 | + break; | |
156 | + } | |
157 | + } | |
158 | + // Peviously deleted blocks cannot be used any more | |
159 | + g_deleted_num=0; | |
140 | 160 | // Check between blocks |
141 | 161 | // Note that there is at least one block with zero pointer and zero size (see above). |
142 | 162 | for(i=0;i<ALLOC_BLOCK_NUM;i++){ |