PERFORCE change 144026 for review

Tim Kientzle kientzle at freebsd.org
Tue Jun 24 16:30:44 UTC 2008


Gabor,

Unrelated, but I noticed that you have an unchecked
call to mbstowcs() here.  mbstowcs() can fail; I
recently went through a couple months of pain reworking
chunks of libarchive to correctly handle such failures.
I ended up falling back on mbtowc() to convert one
character at a time.

You'll see conversion failures, for example, if
someone is using a multi-character locale such
as UTF-8 and runs grep over a file encoded in ISO-8859-1.
(People often use "grep -R <symbol> /usr/src" for example,
and a lot of C source files have people's names
in ISO-8859-1.)

Throwing out the entire file (or even entire line)
because of a single character that can't be
interpreted is probably not going to be feasible.

Tim

Gabor Kovesdan wrote:
> http://perforce.freebsd.org/chv.cgi?CH=144026
> 
> Change 144026 by gabor at gabor_server on 2008/06/24 16:15:17
> 
> 	- Cleanup: use grep_malloc instead of malloc
> 
> Affected files ...
> 
> .. //depot/projects/soc2008/gabor_textproc/grep/binary.c#10 edit
> .. //depot/projects/soc2008/gabor_textproc/grep/grep.c#42 edit
> .. //depot/projects/soc2008/gabor_textproc/grep/util.c#37 edit
> 
> Differences ...
> 
> ==== //depot/projects/soc2008/gabor_textproc/grep/binary.c#10 (text+ko) ====
> 
> @@ -77,8 +77,7 @@
>  	if ((s = mbstowcs(NULL, f->base, 0)) == -1)
>  		return (0);
>  
> -	if ((wbuf = malloc((s + 1) * sizeof(wchar_t))) == NULL)
> -		err(2, NULL);
> +	wbuf = grep_malloc((s + 1) * sizeof(wchar_t));
>  
>  	mbstowcs(wbuf, f->base, s);


More information about the p4-projects mailing list