netcf-devel  

Re: [netcf-devel] [PATCH] * src/drv_initscripts.c (drv_close): protect against segfault

Laine Stump
Wed, 28 Oct 2009 11:07:28 -0700

On 10/28/2009 01:53 PM, David Lutterkort wrote:
> It should be legal to pass in a NULL ncf or ncf->driver. Also added a test
> that checks we return an error code when ncf_init is called with a bogus
> root.
> ---
>   src/drv_initscripts.c    |    2 ++
>   tests/test-initscripts.c |   14 ++++++++++++++
>   2 files changed, 16 insertions(+), 0 deletions(-)
>
> diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
> index 8e082b6..6163035 100644
> --- a/src/drv_initscripts.c
> +++ b/src/drv_initscripts.c
> @@ -508,6 +508,8 @@ int drv_init(struct netcf *ncf) {
>   }
>
>   void drv_close(struct netcf *ncf) {
> +    if (ncf == NULL || ncf->driver == NULL)
> +        return;
>       xsltFreeStylesheet(ncf->driver->get);
>       xsltFreeStylesheet(ncf->driver->put);
>       xmlRelaxNGFree(ncf->driver->rng);
> diff --git a/tests/test-initscripts.c b/tests/test-initscripts.c
> index 0f2a887..329bf19 100644
> --- a/tests/test-initscripts.c
> +++ b/tests/test-initscripts.c
> @@ -192,6 +192,19 @@ static void testTransforms(CuTest *tc) {
>       assert_transforms(tc, "ipv6-static-multi");
>   }
>
> +static void testCorruptedSetup(CuTest *tc) {
> +    int r;
> +
> +    ncf_close(ncf);
> +    ncf = NULL;
> +
> +    r = ncf_init(&ncf, "/dev/null");
> +    CuAssertIntEquals(tc, -1, r);
> +    CuAssertPtrNotNull(tc, ncf);
> +    r = ncf_error(ncf, NULL, NULL);
> +    CuAssertIntEquals(tc, NETCF_EFILE, r);
> +}
> +
>   int main(void) {
>       char *output = NULL;
>       CuSuite* suite = CuSuiteNew();
> @@ -221,6 +234,7 @@ int main(void) {
>       SUITE_ADD_TEST(suite, testLookupByMAC);
>       SUITE_ADD_TEST(suite, testDefineUndefine);
>       SUITE_ADD_TEST(suite, testTransforms);
> +    SUITE_ADD_TEST(suite, testCorruptedSetup);
>
>       CuSuiteRun(suite);
>       CuSuiteSummary(suite,&output);
>    

+1
_______________________________________________
netcf-devel mailing list
netcf-devel@lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/netcf-devel