Project

General

Profile

Bug #1097 » glob.c-len.diff

Gary Mills, 2013-01-30 03:42 AM

View differences:

glob.c-len Tue Jan 29 13:16:05 2013
164 164
    glob_t *pglob)
165 165
{
166 166
	const char *patnext;
167
	size_t n;
167
	size_t n, patlen;
168 168
	wchar_t c;
169 169
	wcat_t *bufnext, *bufend, patbuf[MAXPATHLEN];
170 170
	struct glob_lim limit = { 0, 0, 0 };
171 171

  
172
	if (strnlen(pattern, PATH_MAX) == PATH_MAX)
172
	if ((patlen = strnlen(pattern, PATH_MAX)) == PATH_MAX)
173 173
		return (GLOB_NOMATCH);
174 174

  
175 175
	patnext = pattern;
......
191 191

  
192 192
	bufnext = patbuf;
193 193
	bufend = bufnext + MAXPATHLEN - 1;
194
	patlen += 1;
194 195
	if (flags & GLOB_NOESCAPE) {
195 196
		while (bufnext < bufend) {
196
			if ((n = mbtowc(&c, patnext, MB_LEN_MAX)) > 0) {
197
			if ((n = mbtowc(&c, patnext, patlen)) > 0) {
197 198
				patnext += n;
199
				patlen -= n;
198 200
				bufnext->w_at = 0;
199 201
				(bufnext++)->w_wc = c;
200 202
			} else if (n == 0) {
......
206 208
	} else {
207 209
		/* Protect the quoted characters. */
208 210
		while (bufnext < bufend) {
209
			if ((n = mbtowc(&c, patnext, MB_LEN_MAX)) > 0) {
211
			if ((n = mbtowc(&c, patnext, patlen)) > 0) {
210 212
				patnext += n;
213
				patlen -= n;
211 214
				if (c == QUOTE) {
212
					n = mbtowc(&c, patnext, MB_LEN_MAX);
215
					n = mbtowc(&c, patnext, patlen);
213 216
					if (n < 0)
214 217
						return (GLOB_NOMATCH);
215
					if (n > 0)
218
					if (n > 0) {
216 219
						patnext += n;
220
						patlen -= n;
221
					}
217 222
					if (n == 0)
218 223
						c = QUOTE;
219 224
					bufnext->w_at = M_PROTECT;
......
380 385
	char *h;
381 386
	const wcat_t *p;
382 387
	wcat_t *b, *eb, *q;
383
	size_t n;
388
	size_t n, lenh;
384 389
	wchar_t c;
385 390

  
386 391
	if (pattern->w_wc != TILDE || !(pglob->gl_flags & GLOB_TILDE))
......
421 426
	}
422 427

  
423 428
	/* Copy the home directory */
429
	lenh = strlen(h) + 1;
424 430
	for (b = patbuf; b < eb && *h != EOS; b++) {
425
		if ((n = mbtowc(&c, h, MB_LEN_MAX)) > 0) {
431
		if ((n = mbtowc(&c, h, lenh)) > 0) {
426 432
			h += n;
433
			lenh -= n;
427 434
			b->w_at = 0;
428 435
			b->w_wc = c;
429 436
		} else if (n < 0) {
......
829 836
	while ((dp = (*readdirfunc)(dirp))) {
830 837
		char *sc;
831 838
		wcat_t *dc;
832
		size_t n;
839
		size_t n, lensc;
833 840
		wchar_t w;
834 841

  
835 842
		if ((pglob->gl_flags & GLOB_LIMIT) &&
......
848 855
			continue;
849 856
		dc = pathend;
850 857
		sc = dp->d_name;
858
		lensc = strlen(sc) + 1;
851 859
		while (dc < pathend_last) {
852
			if ((n = mbtowc(&w, sc, MB_LEN_MAX)) <= 0) {
860
			if ((n = mbtowc(&w, sc, lensc)) <= 0) {
853 861
				sc += 1;
862
				lensc -= 1;
854 863
				dc->w_at = 0;
855 864
				dc->w_wc = EOS;
856 865
			} else {
857 866
				sc += n;
867
				lensc -= n;
858 868
				dc->w_at = 0;
859 869
				dc->w_wc = w;
860 870
			}
(10-10/11)