File: | src/usr.bin/vi/build/../common/delete.c |
Warning: | line 111, column 3 Null pointer passed as 1st argument to memory copy function |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: delete.c,v 1.12 2017/11/26 09:59:41 mestre Exp $ */ | |||
2 | ||||
3 | /*- | |||
4 | * Copyright (c) 1992, 1993, 1994 | |||
5 | * The Regents of the University of California. All rights reserved. | |||
6 | * Copyright (c) 1992, 1993, 1994, 1995, 1996 | |||
7 | * Keith Bostic. All rights reserved. | |||
8 | * | |||
9 | * See the LICENSE file for redistribution information. | |||
10 | */ | |||
11 | ||||
12 | #include "config.h" | |||
13 | ||||
14 | #include <sys/types.h> | |||
15 | #include <sys/queue.h> | |||
16 | ||||
17 | #include <bitstring.h> | |||
18 | #include <errno(*__errno()).h> | |||
19 | #include <stdint.h> | |||
20 | #include <stdio.h> | |||
21 | #include <stdlib.h> | |||
22 | #include <string.h> | |||
23 | ||||
24 | #include "common.h" | |||
25 | ||||
26 | /* | |||
27 | * del -- | |||
28 | * Delete a range of text. | |||
29 | * | |||
30 | * PUBLIC: int del(SCR *, MARK *, MARK *, int); | |||
31 | */ | |||
32 | int | |||
33 | del(SCR *sp, MARK *fm, MARK *tm, int lmode) | |||
34 | { | |||
35 | recno_t lno; | |||
36 | size_t blen, len, nlen, tlen; | |||
37 | char *bp, *p; | |||
38 | int eof, rval; | |||
39 | ||||
40 | bp = NULL((void *)0); | |||
41 | ||||
42 | /* Case 1 -- delete in line mode. */ | |||
43 | if (lmode) { | |||
| ||||
44 | for (lno = tm->lno; lno >= fm->lno; --lno) { | |||
45 | if (db_delete(sp, lno)) | |||
46 | return (1); | |||
47 | ++sp->rptlines[L_DELETED2]; | |||
48 | if (lno % INTERRUPT_CHECK100 == 0 && INTERRUPTED(sp)(((((sp)->gp)->flags) & ((0x0004))) || (!v_event_get ((sp), ((void *)0), 0, 0x001) && ((((sp)->gp)-> flags) & ((0x0004)))))) | |||
49 | break; | |||
50 | } | |||
51 | goto done; | |||
52 | } | |||
53 | ||||
54 | /* | |||
55 | * Case 2 -- delete to EOF. This is a special case because it's | |||
56 | * easier to pick it off than try and find it in the other cases. | |||
57 | */ | |||
58 | if (db_last(sp, &lno)) | |||
59 | return (1); | |||
60 | if (tm->lno >= lno) { | |||
61 | if (tm->lno == lno) { | |||
62 | if (db_get(sp, lno, DBG_FATAL0x001, &p, &len)) | |||
63 | return (1); | |||
64 | eof = tm->cno != -1 && tm->cno >= len ? 1 : 0; | |||
65 | } else | |||
66 | eof = 1; | |||
67 | if (eof) { | |||
68 | for (lno = tm->lno; lno > fm->lno; --lno) { | |||
69 | if (db_delete(sp, lno)) | |||
70 | return (1); | |||
71 | ++sp->rptlines[L_DELETED2]; | |||
72 | if (lno % | |||
73 | INTERRUPT_CHECK100 == 0 && INTERRUPTED(sp)(((((sp)->gp)->flags) & ((0x0004))) || (!v_event_get ((sp), ((void *)0), 0, 0x001) && ((((sp)->gp)-> flags) & ((0x0004)))))) | |||
74 | break; | |||
75 | } | |||
76 | if (db_get(sp, fm->lno, DBG_FATAL0x001, &p, &len)) | |||
77 | return (1); | |||
78 | GET_SPACE_RET(sp, bp, blen, fm->cno){ GS *L__gp = (sp) == ((void *)0) ? ((void *)0) : (sp)->gp ; if (L__gp == ((void *)0) || (((L__gp)->flags) & ((0x0100 )))) { (bp) = ((void *)0); (blen) = 0; { void *L__bincp; if ( ((fm->cno)) > ((blen))) { if ((L__bincp = binc(((sp)), ( (bp)), &((blen)), ((fm->cno)))) == ((void *)0)) return (1); ((bp)) = L__bincp; } }; } else { { void *L__bincp; if ( ((fm->cno)) > (L__gp->tmp_blen)) { if ((L__bincp = binc (((sp)), (L__gp->tmp_bp), &(L__gp->tmp_blen), ((fm-> cno)))) == ((void *)0)) return (1); (L__gp->tmp_bp) = L__bincp ; } }; (bp) = L__gp->tmp_bp; (blen) = L__gp->tmp_blen; ( ((L__gp)->flags) |= ((0x0100))); } }; | |||
79 | memcpy(bp, p, fm->cno); | |||
80 | if (db_set(sp, fm->lno, bp, fm->cno)) | |||
81 | return (1); | |||
82 | goto done; | |||
83 | } | |||
84 | } | |||
85 | ||||
86 | /* Case 3 -- delete within a single line. */ | |||
87 | if (tm->lno == fm->lno) { | |||
88 | if (db_get(sp, fm->lno, DBG_FATAL0x001, &p, &len)) | |||
89 | return (1); | |||
90 | if (len != 0) { | |||
91 | GET_SPACE_RET(sp, bp, blen, len){ GS *L__gp = (sp) == ((void *)0) ? ((void *)0) : (sp)->gp ; if (L__gp == ((void *)0) || (((L__gp)->flags) & ((0x0100 )))) { (bp) = ((void *)0); (blen) = 0; { void *L__bincp; if ( ((len)) > ((blen))) { if ((L__bincp = binc(((sp)), ((bp)), &((blen)), ((len)))) == ((void *)0)) return (1); ((bp)) = L__bincp; } }; } else { { void *L__bincp; if (((len)) > ( L__gp->tmp_blen)) { if ((L__bincp = binc(((sp)), (L__gp-> tmp_bp), &(L__gp->tmp_blen), ((len)))) == ((void *)0)) return (1); (L__gp->tmp_bp) = L__bincp; } }; (bp) = L__gp ->tmp_bp; (blen) = L__gp->tmp_blen; (((L__gp)->flags ) |= ((0x0100))); } }; | |||
92 | if (fm->cno != 0) | |||
93 | memcpy(bp, p, fm->cno); | |||
94 | memcpy(bp + fm->cno, p + (tm->cno + 1), len - (tm->cno + 1)); | |||
95 | if (db_set(sp, fm->lno, | |||
96 | bp, len - ((tm->cno - fm->cno) + 1))) | |||
97 | goto err; | |||
98 | goto done; | |||
99 | } | |||
100 | } | |||
101 | ||||
102 | /* | |||
103 | * Case 4 -- delete over multiple lines. | |||
104 | * | |||
105 | * Copy the start partial line into place. | |||
106 | */ | |||
107 | if ((tlen = fm->cno) != 0) { | |||
108 | if (db_get(sp, fm->lno, DBG_FATAL0x001, &p, NULL((void *)0))) | |||
109 | return (1); | |||
110 | GET_SPACE_RET(sp, bp, blen, tlen + 256){ GS *L__gp = (sp) == ((void *)0) ? ((void *)0) : (sp)->gp ; if (L__gp == ((void *)0) || (((L__gp)->flags) & ((0x0100 )))) { (bp) = ((void *)0); (blen) = 0; { void *L__bincp; if ( ((tlen + 256)) > ((blen))) { if ((L__bincp = binc(((sp)), ( (bp)), &((blen)), ((tlen + 256)))) == ((void *)0)) return (1); ((bp)) = L__bincp; } }; } else { { void *L__bincp; if ( ((tlen + 256)) > (L__gp->tmp_blen)) { if ((L__bincp = binc (((sp)), (L__gp->tmp_bp), &(L__gp->tmp_blen), ((tlen + 256)))) == ((void *)0)) return (1); (L__gp->tmp_bp) = L__bincp ; } }; (bp) = L__gp->tmp_bp; (blen) = L__gp->tmp_blen; ( ((L__gp)->flags) |= ((0x0100))); } }; | |||
111 | memcpy(bp, p, tlen); | |||
| ||||
112 | } | |||
113 | ||||
114 | /* Copy the end partial line into place. */ | |||
115 | if (db_get(sp, tm->lno, DBG_FATAL0x001, &p, &len)) | |||
116 | goto err; | |||
117 | if (len != 0 && tm->cno != len - 1) { | |||
118 | if (len < tm->cno + 1 || len - (tm->cno + 1) > SIZE_MAX0xffffffffffffffffUL - tlen) { | |||
119 | msgq(sp, M_ERR, "Line length overflow"); | |||
120 | goto err; | |||
121 | } | |||
122 | nlen = (len - (tm->cno + 1)) + tlen; | |||
123 | if (tlen == 0) { | |||
124 | GET_SPACE_RET(sp, bp, blen, nlen){ GS *L__gp = (sp) == ((void *)0) ? ((void *)0) : (sp)->gp ; if (L__gp == ((void *)0) || (((L__gp)->flags) & ((0x0100 )))) { (bp) = ((void *)0); (blen) = 0; { void *L__bincp; if ( ((nlen)) > ((blen))) { if ((L__bincp = binc(((sp)), ((bp)) , &((blen)), ((nlen)))) == ((void *)0)) return (1); ((bp) ) = L__bincp; } }; } else { { void *L__bincp; if (((nlen)) > (L__gp->tmp_blen)) { if ((L__bincp = binc(((sp)), (L__gp-> tmp_bp), &(L__gp->tmp_blen), ((nlen)))) == ((void *)0) ) return (1); (L__gp->tmp_bp) = L__bincp; } }; (bp) = L__gp ->tmp_bp; (blen) = L__gp->tmp_blen; (((L__gp)->flags ) |= ((0x0100))); } }; | |||
125 | } else | |||
126 | ADD_SPACE_RET(sp, bp, blen, nlen){ GS *L__gp = (sp) == ((void *)0) ? ((void *)0) : (sp)->gp ; if (L__gp != ((void *)0) && (bp) == L__gp->tmp_bp ) { (((L__gp)->flags) &= ~((0x0100))); { void *L__bincp ; if (((nlen)) > (L__gp->tmp_blen)) { if ((L__bincp = binc (((sp)), (L__gp->tmp_bp), &(L__gp->tmp_blen), ((nlen )))) == ((void *)0)) return (1); (L__gp->tmp_bp) = L__bincp ; } }; (bp) = L__gp->tmp_bp; (blen) = L__gp->tmp_blen; ( ((L__gp)->flags) |= ((0x0100))); } else { void *L__bincp; if (((nlen)) > ((blen))) { if ((L__bincp = binc(((sp)), ((bp )), &((blen)), ((nlen)))) == ((void *)0)) return (1); ((bp )) = L__bincp; } }; }; | |||
127 | ||||
128 | memcpy(bp + tlen, p + (tm->cno + 1), len - (tm->cno + 1)); | |||
129 | tlen += len - (tm->cno + 1); | |||
130 | } | |||
131 | ||||
132 | /* Set the current line. */ | |||
133 | if (db_set(sp, fm->lno, bp, tlen)) | |||
134 | goto err; | |||
135 | ||||
136 | /* Delete the last and intermediate lines. */ | |||
137 | for (lno = tm->lno; lno > fm->lno; --lno) { | |||
138 | if (db_delete(sp, lno)) | |||
139 | goto err; | |||
140 | ++sp->rptlines[L_DELETED2]; | |||
141 | if (lno % INTERRUPT_CHECK100 == 0 && INTERRUPTED(sp)(((((sp)->gp)->flags) & ((0x0004))) || (!v_event_get ((sp), ((void *)0), 0, 0x001) && ((((sp)->gp)-> flags) & ((0x0004)))))) | |||
142 | break; | |||
143 | } | |||
144 | ||||
145 | done: rval = 0; | |||
146 | if (0) | |||
147 | err: rval = 1; | |||
148 | if (bp != NULL((void *)0)) | |||
149 | FREE_SPACE(sp, bp, blen){ GS *L__gp = (sp) == ((void *)0) ? ((void *)0) : (sp)->gp ; if (L__gp != ((void *)0) && (bp) == L__gp->tmp_bp ) (((L__gp)->flags) &= ~((0x0100))); else free(bp); }; | |||
150 | return (rval); | |||
151 | } |