[PATCH 6/6] Convert some malloc + strncpy pairs into strndup calls

Alan Coopersmith alan.coopersmith at oracle.com
Fri Oct 28 20:10:30 PDT 2011

On 10/28/11 18:13, Matt Turner wrote:
> On Fri, Oct 28, 2011 at 8:34 PM, Alan Coopersmith
> <alan.coopersmith at oracle.com>  wrote:
>> Signed-off-by: Alan Coopersmith<alan.coopersmith at oracle.com>
> I think these can be converted as well.
> Xext/xselinux_ext.c:SELinuxCopyContext
> dix/dixfonts.c:SetFontPathElements
> hw/dmx/dmxinit.c:dmxSetDefaultFontPath
> hw/dmx/dmxprop.c:dmxPropertyCheckOtherServers
> hw/dmx/input/dmxarg.c:dmxArgParse
> hw/xfree86/loader/loadmod.c:LoaderListDirs
> hw/xwin/InitOutput.c:winFixupPaths
> hw/xwin/winmultiwindowclass.c:winMultiWindowGetClassHint
> hw/xwin/winwindowswm.c:ProcWindowsWMFrameSetTitle
> os/client.c:DetermineClientCmd
> If you want, fold these into your patch. Otherwise I can send a
> follow-on patch to fix them up.

strndup(str, len) is not quite the same as
	new = malloc(len + 1);
	strncpy(new, str, len);
	new[len] = '\0';

as it actually mallocs MIN(strlen(str), len)

I hadn't converted every one of those places yet since I hadn't
confirmed that in each of them either it wasn't going to allocate
something shorter than len, or that nothing would break if indeed
it did allocate something shorter (for instance, it wouldn't later
try to write a larger string to the allocated memory).

Also, in the xselinux_ext.c case, I was unsure about the
security_context_t it's being cast do and didn't take the time to
research if it's just a typedef for char * or a char [fixed_length]

Please feel free to work on converting any of the above if you can
determine it's safe to do so.

	-Alan Coopersmith-        alan.coopersmith at oracle.com
	 Oracle Solaris Platform Engineering: X Window System

More information about the xorg-devel mailing list