[PATCH 04/11] FindModuleInSubdir: Stop allocating one more byte than needed

Matt Turner mattst88 at gmail.com
Tue Nov 30 06:46:03 PST 2010


On Tue, Nov 30, 2010 at 4:57 AM, Alan Coopersmith
<alan.coopersmith at oracle.com> wrote:
> 15ac25627e7239629be59 removed the "/" from the sprintf strings,
> but failed to remove the extra byte allocated for the '/'.
>
> Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
> ---
>  hw/xfree86/loader/loadmod.c |    6 +++---
>  1 files changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/hw/xfree86/loader/loadmod.c b/hw/xfree86/loader/loadmod.c
> index 6e65227..06d082b 100644
> --- a/hw/xfree86/loader/loadmod.c
> +++ b/hw/xfree86/loader/loadmod.c
> @@ -406,21 +406,21 @@ FindModuleInSubdir(const char *dirpath, const char *module)
>
>         snprintf(tmpBuf, PATH_MAX, "lib%s.so", module);
>         if (strcmp(direntry->d_name, tmpBuf) == 0) {
> -            ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 2);
> +            ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 1);
>             sprintf(ret, "%s%s", dirpath, tmpBuf);
>             break;
>         }
>
>         snprintf(tmpBuf, PATH_MAX, "%s_drv.so", module);
>         if (strcmp(direntry->d_name, tmpBuf) == 0) {
> -            ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 2);
> +            ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 1);
>             sprintf(ret, "%s%s", dirpath, tmpBuf);
>             break;
>         }
>
>         snprintf(tmpBuf, PATH_MAX, "%s.so", module);
>         if (strcmp(direntry->d_name, tmpBuf) == 0) {
> -            ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 2);
> +            ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 1);
>             sprintf(ret, "%s%s", dirpath, tmpBuf);
>             break;
>         }
> --

Good catch. Did you just happen to notice this, or find it in some
more automated way?

Reviewed-by: Matt Turner <mattst88 at gmail.com>

Is there any reason we shouldn't use Xasprintf here instead of malloc+sprintf?


More information about the xorg-devel mailing list