Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-
- * Copyright (c) 2014 EMC / Isilon Storage Division
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
- #include <atf-c.h>
- #include <assert.h>
- #include <dirent.h>
- #include <err.h>
- #include <fcntl.h>
- #include <malloc_np.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- ATF_TC(cgetstr_leak);
- ATF_TC_HEAD(cgetstr_leak, tc)
- {
- atf_tc_set_md_var(tc, "descr",
- "Check cgetstr(3) for memory leakage (Bug 195128)");
- }
- void
- malgetuint64(char *name, uint64_t *val)
- {
- size_t len;
- len = sizeof(*val);
- if (mallctl(name, val, &len, NULL, 0) == -1)
- err(2, "mallctl failed");
- if (len != sizeof(*val))
- err(2, "bad size from mallctl");
- }
- int64_t
- malallocated(void)
- {
- uint64_t allocated, deallocated;
- malgetuint64("thread.allocated", &allocated);
- malgetuint64("thread.deallocated", &deallocated);
- printf("alloc: %jd dealloc: %jd delta: %jd\n", (uintmax_t)allocated, (uintmax_t)deallocated, (uintmax_t)(allocated - deallocated));
- return ((int64_t)(allocated - deallocated));
- }
- ATF_TC_BODY(cgetstr_leak, tc)
- {
- char *buf, *str, *dbarray[] = { "/etc/login.conf" };
- int errcode, i;
- int64_t allocated, start;
- start = malallocated();
- printf("Start %jd\n", (intmax_t)start);
- i = cgetent(&buf, dbarray, "default");
- if (i == -1)
- atf_tc_skip("Unable to find default class in login.conf");
- if (i == -2)
- atf_tc_skip("Error reading login.conf");
- allocated = malallocated();
- printf("cgetent %jd\n", (intmax_t)allocated);
- for (i = 0; i < 1000; i++) {
- if ((errcode = cgetstr(buf, "path", &str)) < 0)
- atf_tc_skip("cgetstr failed with %d", errcode);
- }
- allocated = malallocated();
- printf("cgetstr %jd\n", (intmax_t)allocated);
- free(buf);
- printf("free %jd\n", (intmax_t)allocated);
- cgetclose();
- allocated = malallocated();
- printf("cgetclose %jd\n", (intmax_t)allocated);
- if (allocated - start > 4096)
- atf_tc_fail("Failure: leaked %jd bytes", allocated - start);
- else
- (void)printf("OK: used %jd bytes\n", allocated - start);
- }
- ATF_TP_ADD_TCS(tp)
- {
- ATF_TP_ADD_TC(tp, cgetstr_leak);
- return atf_no_error();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement