comparison src/textprop.c @ 29340:fba9e366ced4 v9.0.0013

patch 9.0.0013: reproducing memory access errors can be difficult Commit: https://github.com/vim/vim/commit/fa4873ccfc10e0f278dc46f39d00136fab059b19 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Jun 30 22:13:59 2022 +0100 patch 9.0.0013: reproducing memory access errors can be difficult Problem: Reproducing memory access errors can be difficult. Solution: When testing, copy each line to allocated memory, so that valgrind can detect accessing memory before and/or after it. Fix uncovered problems.
author Bram Moolenaar <Bram@vim.org>
date Thu, 30 Jun 2022 23:15:03 +0200
parents 755ab148288b
children 827d9f2b7a71
comparison
equal deleted inserted replaced
29339:a023e3008ae3 29340:fba9e366ced4
285 if (i < proplen) 285 if (i < proplen)
286 mch_memmove(newprops + (i + 1) * sizeof(textprop_T), 286 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
287 props + i * sizeof(textprop_T), 287 props + i * sizeof(textprop_T),
288 sizeof(textprop_T) * (proplen - i)); 288 sizeof(textprop_T) * (proplen - i));
289 289
290 if (buf->b_ml.ml_flags & ML_LINE_DIRTY) 290 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
291 vim_free(buf->b_ml.ml_line_ptr); 291 vim_free(buf->b_ml.ml_line_ptr);
292 buf->b_ml.ml_line_ptr = newtext; 292 buf->b_ml.ml_line_ptr = newtext;
293 buf->b_ml.ml_line_len += sizeof(textprop_T); 293 buf->b_ml.ml_line_len += sizeof(textprop_T);
294 buf->b_ml.ml_flags |= ML_LINE_DIRTY; 294 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
295 } 295 }
562 if (newtext == NULL) 562 if (newtext == NULL)
563 return; 563 return;
564 mch_memmove(newtext, text, textlen); 564 mch_memmove(newtext, text, textlen);
565 if (len > 0) 565 if (len > 0)
566 mch_memmove(newtext + textlen, props, len); 566 mch_memmove(newtext + textlen, props, len);
567 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY) 567 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
568 vim_free(curbuf->b_ml.ml_line_ptr); 568 vim_free(curbuf->b_ml.ml_line_ptr);
569 curbuf->b_ml.ml_line_ptr = newtext; 569 curbuf->b_ml.ml_line_ptr = newtext;
570 curbuf->b_ml.ml_line_len = textlen + len; 570 curbuf->b_ml.ml_line_len = textlen + len;
571 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY; 571 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
572 } 572 }
696 char_u *newtext = vim_strsave(text); 696 char_u *newtext = vim_strsave(text);
697 697
698 // need to allocate the line now 698 // need to allocate the line now
699 if (newtext == NULL) 699 if (newtext == NULL)
700 return; 700 return;
701 if (buf->b_ml.ml_flags & ML_ALLOCATED)
702 vim_free(buf->b_ml.ml_line_ptr);
701 buf->b_ml.ml_line_ptr = newtext; 703 buf->b_ml.ml_line_ptr = newtext;
702 buf->b_ml.ml_flags |= ML_LINE_DIRTY; 704 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
703 } 705 }
704 buf->b_ml.ml_line_len = (int)len; 706 buf->b_ml.ml_line_len = (int)len;
705 } 707 }
1271 // need to allocate the line to be able to change it 1273 // need to allocate the line to be able to change it
1272 if (newptr == NULL) 1274 if (newptr == NULL)
1273 return; 1275 return;
1274 mch_memmove(newptr, buf->b_ml.ml_line_ptr, 1276 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1275 buf->b_ml.ml_line_len); 1277 buf->b_ml.ml_line_len);
1278 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1279 vim_free(buf->b_ml.ml_line_ptr);
1276 buf->b_ml.ml_line_ptr = newptr; 1280 buf->b_ml.ml_line_ptr = newptr;
1277 buf->b_ml.ml_flags |= ML_LINE_DIRTY; 1281 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1278 1282
1279 cur_prop = buf->b_ml.ml_line_ptr + len 1283 cur_prop = buf->b_ml.ml_line_ptr + len
1280 + idx * sizeof(textprop_T); 1284 + idx * sizeof(textprop_T);
1764 if (dirty) 1768 if (dirty)
1765 { 1769 {
1766 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T); 1770 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1767 1771
1768 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0) 1772 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1769 curbuf->b_ml.ml_line_ptr = 1773 {
1770 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen); 1774 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1775
1776 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1777 vim_free(curbuf->b_ml.ml_line_ptr);
1778 curbuf->b_ml.ml_line_ptr = p;
1779 }
1771 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY; 1780 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
1772 curbuf->b_ml.ml_line_len = newlen; 1781 curbuf->b_ml.ml_line_len = newlen;
1773 } 1782 }
1774 return dirty; 1783 return dirty;
1775 } 1784 }