comparison src/ops.c @ 16764:ef00b6bc186b v8.1.1384

patch 8.1.1384: using "int" for alloc() often results in compiler warnings commit https://github.com/vim/vim/commit/964b3746b9c81e65887e2ac9a335f181db2bb592 Author: Bram Moolenaar <Bram@vim.org> Date: Fri May 24 18:54:09 2019 +0200 patch 8.1.1384: using "int" for alloc() often results in compiler warnings Problem: Using "int" for alloc() often results in compiler warnings. Solution: Use "size_t" and remove type casts. Remove alloc_check(), Vim only works with 32 bit ints anyway.
author Bram Moolenaar <Bram@vim.org>
date Fri, 24 May 2019 19:00:07 +0200
parents ba592f30c082
children 695d9ef00b03
comparison
equal deleted inserted replaced
16763:fccf84413b53 16764:ef00b6bc186b
454 j = total; 454 j = total;
455 #endif 455 #endif
456 /* if we're splitting a TAB, allow for it */ 456 /* if we're splitting a TAB, allow for it */
457 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0); 457 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
458 len = (int)STRLEN(bd.textstart) + 1; 458 len = (int)STRLEN(bd.textstart) + 1;
459 newp = alloc_check((unsigned)(bd.textcol + i + j + len)); 459 newp = alloc(bd.textcol + i + j + len);
460 if (newp == NULL) 460 if (newp == NULL)
461 return; 461 return;
462 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len)); 462 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
463 mch_memmove(newp, oldp, (size_t)bd.textcol); 463 mch_memmove(newp, oldp, (size_t)bd.textcol);
464 vim_memset(newp + bd.textcol, TAB, (size_t)i); 464 vim_memset(newp + bd.textcol, TAB, (size_t)i);
548 * - the rest of the line, pointed to by non_white. */ 548 * - the rest of the line, pointed to by non_white. */
549 new_line_len = (unsigned)(verbatim_copy_end - oldp) 549 new_line_len = (unsigned)(verbatim_copy_end - oldp)
550 + fill 550 + fill
551 + (unsigned)STRLEN(non_white) + 1; 551 + (unsigned)STRLEN(non_white) + 1;
552 552
553 newp = alloc_check(new_line_len); 553 newp = alloc(new_line_len);
554 if (newp == NULL) 554 if (newp == NULL)
555 return; 555 return;
556 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp)); 556 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
557 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill); 557 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
558 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white); 558 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
642 } 642 }
643 spaces -= off; 643 spaces -= off;
644 count -= off; 644 count -= off;
645 } 645 }
646 646
647 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1); 647 newp = alloc(STRLEN(oldp) + s_len + count + 1);
648 if (newp == NULL) 648 if (newp == NULL)
649 continue; 649 continue;
650 650
651 /* copy up to shifted part */ 651 /* copy up to shifted part */
652 mch_memmove(newp, oldp, (size_t)(offset)); 652 mch_memmove(newp, oldp, (size_t)(offset));
1001 may_get_selection(name); 1001 may_get_selection(name);
1002 } 1002 }
1003 #endif 1003 #endif
1004 1004
1005 get_yank_register(name, 0); 1005 get_yank_register(name, 0);
1006 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T)); 1006 reg = (yankreg_T *)alloc(sizeof(yankreg_T));
1007 if (reg != NULL) 1007 if (reg != NULL)
1008 { 1008 {
1009 *reg = *y_current; 1009 *reg = *y_current;
1010 if (copy) 1010 if (copy)
1011 { 1011 {
1012 /* If we run out of memory some or all of the lines are empty. */ 1012 /* If we run out of memory some or all of the lines are empty. */
1013 if (reg->y_size == 0) 1013 if (reg->y_size == 0)
1014 reg->y_array = NULL; 1014 reg->y_array = NULL;
1015 else 1015 else
1016 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *) 1016 reg->y_array = (char_u **)alloc(sizeof(char_u *) * reg->y_size);
1017 * reg->y_size));
1018 if (reg->y_array != NULL) 1017 if (reg->y_array != NULL)
1019 { 1018 {
1020 for (i = 0; i < reg->y_size; ++i) 1019 for (i = 0; i < reg->y_size; ++i)
1021 reg->y_array[i] = vim_strsave(y_current->y_array[i]); 1020 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1022 } 1021 }
1175 } 1174 }
1176 else 1175 else
1177 { 1176 {
1178 free_yank_all(); 1177 free_yank_all();
1179 if ((y_current->y_array = 1178 if ((y_current->y_array =
1180 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL) 1179 (char_u **)alloc(sizeof(char_u *))) == NULL)
1181 { 1180 {
1182 vim_free(p); 1181 vim_free(p);
1183 return FAIL; 1182 return FAIL;
1184 } 1183 }
1185 y_current->y_array[0] = p; 1184 y_current->y_array[0] = p;
1919 // "n" == number of chars deleted 1918 // "n" == number of chars deleted
1920 // If we delete a TAB, it may be replaced by several characters. 1919 // If we delete a TAB, it may be replaced by several characters.
1921 // Thus the number of characters may increase! 1920 // Thus the number of characters may increase!
1922 n = bd.textlen - bd.startspaces - bd.endspaces; 1921 n = bd.textlen - bd.startspaces - bd.endspaces;
1923 oldp = ml_get(lnum); 1922 oldp = ml_get(lnum);
1924 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n); 1923 newp = alloc(STRLEN(oldp) + 1 - n);
1925 if (newp == NULL) 1924 if (newp == NULL)
1926 continue; 1925 continue;
1927 /* copy up to deleted part */ 1926 /* copy up to deleted part */
1928 mch_memmove(newp, oldp, (size_t)bd.textcol); 1927 mch_memmove(newp, oldp, (size_t)bd.textcol);
1929 /* insert spaces */ 1928 /* insert spaces */
2225 /* oldlen includes textlen, so don't double count */ 2224 /* oldlen includes textlen, so don't double count */
2226 n += numc - bd.textlen; 2225 n += numc - bd.textlen;
2227 2226
2228 oldp = ml_get_curline(); 2227 oldp = ml_get_curline();
2229 oldlen = STRLEN(oldp); 2228 oldlen = STRLEN(oldp);
2230 newp = alloc_check((unsigned)oldlen + 1 + n); 2229 newp = alloc(oldlen + 1 + n);
2231 if (newp == NULL) 2230 if (newp == NULL)
2232 continue; 2231 continue;
2233 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n)); 2232 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2234 /* copy up to deleted part */ 2233 /* copy up to deleted part */
2235 mch_memmove(newp, oldp, (size_t)bd.textcol); 2234 mch_memmove(newp, oldp, (size_t)bd.textcol);
2258 } 2257 }
2259 } 2258 }
2260 else 2259 else
2261 { 2260 {
2262 /* Replacing with \r or \n means splitting the line. */ 2261 /* Replacing with \r or \n means splitting the line. */
2263 after_p = alloc_check( 2262 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
2264 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
2265 if (after_p != NULL) 2263 if (after_p != NULL)
2266 STRMOVE(after_p, oldp); 2264 STRMOVE(after_p, oldp);
2267 } 2265 }
2268 /* replace the line */ 2266 /* replace the line */
2269 ml_replace(curwin->w_cursor.lnum, newp, FALSE); 2267 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2867 ins_len = (long)STRLEN(firstline) - pre_textlen; 2865 ins_len = (long)STRLEN(firstline) - pre_textlen;
2868 if (ins_len > 0) 2866 if (ins_len > 0)
2869 { 2867 {
2870 /* Subsequent calls to ml_get() flush the firstline data - take a 2868 /* Subsequent calls to ml_get() flush the firstline data - take a
2871 * copy of the inserted text. */ 2869 * copy of the inserted text. */
2872 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL) 2870 if ((ins_text = alloc(ins_len + 1)) != NULL)
2873 { 2871 {
2874 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len); 2872 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
2875 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum; 2873 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2876 linenr++) 2874 linenr++)
2877 { 2875 {
2888 (void)getvpos(&vpos, oap->start_vcol); 2886 (void)getvpos(&vpos, oap->start_vcol);
2889 } 2887 }
2890 else 2888 else
2891 vpos.coladd = 0; 2889 vpos.coladd = 0;
2892 oldp = ml_get(linenr); 2890 oldp = ml_get(linenr);
2893 newp = alloc_check((unsigned)(STRLEN(oldp) 2891 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
2894 + vpos.coladd + ins_len + 1));
2895 if (newp == NULL) 2892 if (newp == NULL)
2896 continue; 2893 continue;
2897 /* copy up to block start */ 2894 /* copy up to block start */
2898 mch_memmove(newp, oldp, (size_t)bd.textcol); 2895 mch_memmove(newp, oldp, (size_t)bd.textcol);
2899 offset = bd.textcol; 2896 offset = bd.textcol;
3492 } 3489 }
3493 } 3490 }
3494 } 3491 }
3495 if (y_array != NULL) 3492 if (y_array != NULL)
3496 break; 3493 break;
3497 y_array = (char_u **)alloc((unsigned) 3494 y_array = (char_u **)alloc((y_size * sizeof(char_u *)));
3498 (y_size * sizeof(char_u *)));
3499 if (y_array == NULL) 3495 if (y_array == NULL)
3500 goto end; 3496 goto end;
3501 } 3497 }
3502 } 3498 }
3503 else 3499 else
3739 if (spaces < 0) 3735 if (spaces < 0)
3740 spaces = 0; 3736 spaces = 0;
3741 3737
3742 /* insert the new text */ 3738 /* insert the new text */
3743 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces; 3739 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3744 newp = alloc_check((unsigned)totlen + oldlen + 1); 3740 newp = alloc(totlen + oldlen + 1);
3745 if (newp == NULL) 3741 if (newp == NULL)
3746 break; 3742 break;
3747 /* copy part up to cursor to new line */ 3743 /* copy part up to cursor to new line */
3748 ptr = newp; 3744 ptr = newp;
3749 mch_memmove(ptr, oldp, (size_t)bd.textcol); 3745 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3866 if (VIsual_active && col > (int)STRLEN(oldp)) 3862 if (VIsual_active && col > (int)STRLEN(oldp))
3867 { 3863 {
3868 lnum++; 3864 lnum++;
3869 continue; 3865 continue;
3870 } 3866 }
3871 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1)); 3867 newp = alloc(STRLEN(oldp) + totlen + 1);
3872 if (newp == NULL) 3868 if (newp == NULL)
3873 goto end; /* alloc() gave an error message */ 3869 goto end; /* alloc() gave an error message */
3874 mch_memmove(newp, oldp, (size_t)col); 3870 mch_memmove(newp, oldp, (size_t)col);
3875 ptr = newp + col; 3871 ptr = newp + col;
3876 for (i = 0; i < count; ++i) 3872 for (i = 0; i < count; ++i)
3918 * Then append y_array[0] to first line. 3914 * Then append y_array[0] to first line.
3919 */ 3915 */
3920 lnum = new_cursor.lnum; 3916 lnum = new_cursor.lnum;
3921 ptr = ml_get(lnum) + col; 3917 ptr = ml_get(lnum) + col;
3922 totlen = (int)STRLEN(y_array[y_size - 1]); 3918 totlen = (int)STRLEN(y_array[y_size - 1]);
3923 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1)); 3919 newp = alloc(STRLEN(ptr) + totlen + 1);
3924 if (newp == NULL) 3920 if (newp == NULL)
3925 goto error; 3921 goto error;
3926 STRCPY(newp, y_array[y_size - 1]); 3922 STRCPY(newp, y_array[y_size - 1]);
3927 STRCAT(newp, ptr); 3923 STRCAT(newp, ptr);
3928 /* insert second line */ 3924 /* insert second line */
3929 ml_append(lnum, newp, (colnr_T)0, FALSE); 3925 ml_append(lnum, newp, (colnr_T)0, FALSE);
3930 vim_free(newp); 3926 vim_free(newp);
3931 3927
3932 oldp = ml_get(lnum); 3928 oldp = ml_get(lnum);
3933 newp = alloc_check((unsigned)(col + yanklen + 1)); 3929 newp = alloc(col + yanklen + 1);
3934 if (newp == NULL) 3930 if (newp == NULL)
3935 goto error; 3931 goto error;
3936 /* copy first part of line */ 3932 /* copy first part of line */
3937 mch_memmove(newp, oldp, (size_t)col); 3933 mch_memmove(newp, oldp, (size_t)col);
3938 /* append to first line */ 3934 /* append to first line */
4561 4557
4562 /* store the column position before last line */ 4558 /* store the column position before last line */
4563 col = sumsize - currsize - spaces[count - 1]; 4559 col = sumsize - currsize - spaces[count - 1];
4564 4560
4565 /* allocate the space for the new line */ 4561 /* allocate the space for the new line */
4566 newp = alloc_check((unsigned)(sumsize + 1)); 4562 newp = alloc(sumsize + 1);
4567 cend = newp + sumsize; 4563 cend = newp + sumsize;
4568 *cend = 0; 4564 *cend = 0;
4569 4565
4570 #ifdef FEAT_TEXT_PROP 4566 #ifdef FEAT_TEXT_PROP
4571 // We need to move properties of the lines that are going to be deleted to 4567 // We need to move properties of the lines that are going to be deleted to
5878 /* 5874 /*
5879 * Prepare the leading characters in buf1[]. 5875 * Prepare the leading characters in buf1[].
5880 * When there are many leading zeros it could be very long. 5876 * When there are many leading zeros it could be very long.
5881 * Allocate a bit too much. 5877 * Allocate a bit too much.
5882 */ 5878 */
5883 buf1 = alloc((unsigned)length + NUMBUFLEN); 5879 buf1 = alloc(length + NUMBUFLEN);
5884 if (buf1 == NULL) 5880 if (buf1 == NULL)
5885 goto theend; 5881 goto theend;
5886 ptr = buf1; 5882 ptr = buf1;
5887 if (negative && (!visual || was_positive)) 5883 if (negative && (!visual || was_positive))
5888 *ptr++ = '-'; 5884 *ptr++ = '-';
6053 * The "do_it" flag is reset when something is wrong, in which case 6049 * The "do_it" flag is reset when something is wrong, in which case
6054 * array[] needs to be freed. 6050 * array[] needs to be freed.
6055 */ 6051 */
6056 if (set_prev) 6052 if (set_prev)
6057 y_previous = y_current; 6053 y_previous = y_current;
6058 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *))); 6054 array = (char_u **)alloc(limit * sizeof(char_u *));
6059 str = skipwhite(skiptowhite(str)); 6055 str = skipwhite(skiptowhite(str));
6060 if (STRNCMP(str, "CHAR", 4) == 0) 6056 if (STRNCMP(str, "CHAR", 4) == 0)
6061 new_type = MCHAR; 6057 new_type = MCHAR;
6062 else if (STRNCMP(str, "BLOCK", 5) == 0) 6058 else if (STRNCMP(str, "BLOCK", 5) == 0)
6063 new_type = MBLOCK; 6059 new_type = MBLOCK;
6074 if (do_it) 6070 if (do_it)
6075 { 6071 {
6076 if (size == limit) 6072 if (size == limit)
6077 { 6073 {
6078 char_u **new_array = (char_u **) 6074 char_u **new_array = (char_u **)
6079 alloc((unsigned)(limit * 2 * sizeof(char_u *))); 6075 alloc(limit * 2 * sizeof(char_u *));
6080 6076
6081 if (new_array == NULL) 6077 if (new_array == NULL)
6082 { 6078 {
6083 do_it = FALSE; 6079 do_it = FALSE;
6084 break; 6080 break;
6114 y_current->y_array = NULL; 6110 y_current->y_array = NULL;
6115 } 6111 }
6116 else 6112 else
6117 { 6113 {
6118 /* Move the lines from array[] to y_array[]. */ 6114 /* Move the lines from array[] to y_array[]. */
6119 y_current->y_array = 6115 y_current->y_array = (char_u **)alloc(size * sizeof(char_u *));
6120 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6121 for (i = 0; i < size; i++) 6116 for (i = 0; i < size; i++)
6122 { 6117 {
6123 if (y_current->y_array == NULL) 6118 if (y_current->y_array == NULL)
6124 vim_free(array[i]); 6119 vim_free(array[i]);
6125 else 6120 else
6212 if (linecount == 0) 6207 if (linecount == 0)
6213 { 6208 {
6214 y_ptr->y_array = NULL; 6209 y_ptr->y_array = NULL;
6215 return; 6210 return;
6216 } 6211 }
6217 y_ptr->y_array = (char_u **)alloc((unsigned)(linecount * sizeof(char_u *))); 6212 y_ptr->y_array = (char_u **)alloc(linecount * sizeof(char_u *));
6218 if (y_ptr->y_array == NULL) 6213 if (y_ptr->y_array == NULL)
6219 { 6214 {
6220 y_ptr->y_size = 0; // ensure object state is consistent 6215 y_ptr->y_size = 0; // ensure object state is consistent
6221 return; 6216 return;
6222 } 6217 }
7143 --lnum; 7138 --lnum;
7144 extra = (int)STRLEN(y_ptr->y_array[lnum]); 7139 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7145 } 7140 }
7146 else 7141 else
7147 extra = 0; 7142 extra = 0;
7148 s = alloc((unsigned)(i + extra + 1)); 7143 s = alloc(i + extra + 1);
7149 if (s == NULL) 7144 if (s == NULL)
7150 break; 7145 break;
7151 if (extra) 7146 if (extra)
7152 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra); 7147 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7153 if (append) 7148 if (append)