Return-Path: owner-postman Received: from localhost.Berkeley.EDU (localhost.Berkeley.EDU [127.0.0.1]) by nobozo.CS.Berkeley.EDU (8.6.10/8.6.3) with SMTP id KAA06829 for postgres-redist; Mon, 10 Jul 1995 10:10:45 -0700 Resent-From: POSTGRES mailing list Resent-Message-Id: <199507101710.KAA06829@nobozo.CS.Berkeley.EDU> X-Authentication-Warning: nobozo.CS.Berkeley.EDU: Host localhost.Berkeley.EDU didn't use HELO protocol Sender: owner-postman@postgres.Berkeley.EDU X-Return-Path: owner-postman Received: from methi.ndim.edrc.cmu.edu (METHI.NDIM.EDRC.CMU.EDU [128.2.214.230]) by nobozo.CS.Berkeley.EDU (8.6.10/8.6.3) with SMTP id KAA06819 for ; Mon, 10 Jul 1995 10:10:43 -0700 Message-Id: <199507101710.KAA06819@nobozo.CS.Berkeley.EDU> Received: from localhost by methi.ndim.edrc.cmu.edu id aa08676; 10 Jul 95 13:09 EDT To: postgres@postgres.Berkeley.EDU cc: Shreeniwas N Sapre Subject: Re: putenv in postmaster.c In-reply-to: Shreeniwas N Sapre's message of Sat, 08 Jul 1995 16:13:07 -53000 Date: Mon, 10 Jul 95 13:09:25 -0400 From: rp2y@methi.ndim.edrc.cmu.edu Resent-To: postgres-redist@postgres.Berkeley.EDU X-Mts: smtp Resent-Date: Mon, 10 Jul 95 10:10:44 -0700 Resent-XMts: smtp > The use of putenv in postmaster.c (sample below) has crashed > postmaster on our system (DGUX 5.4R2.10). The symptom is its inability > to find "template1" after servicing the first 2 or 3 requests. My > diagnosis is the use of envEntry ( which is on stack) for putenv > causing corrupted environment entries, as putenv keeps a pointer to > the string and not a copy of the string. This problem surfaced after > we made a few changes (unrelated to this problem) to postmaster. We > have made envEntry as "static" as a solution. > > Sample Code: > BackendStartup(packet, port) > ... > { > char envEntry[4][2 * ARGV_SIZE]; > > ...... > sprintf(envEntry[0], "POSTPORT=%d", PostPortName); > putenv(envEntry[0]); > ...... Similar code here. > ...... > } First, I'm confused by the code that you posted, this obviously isn't right since the memory allocated for envEntry is allocated on the stack when this function is called and released when the function returns. I am assuming that this is the "bad" example. Second, I'm not sure where you got this code because both Postgres V4.2 and Postgres95 actually declare envEntry as an array of pointers and call calloc() to allocate the memory (which is probably a cleaner solution that using a "static" array of character arrays). BackendStartup looks like this in Postgres V4.2: BackendStartup(packet, port) ... { ... char *envEntry[4]; for (i = 0; i < 4; ++i) { envEntry[i] = calloc(2 * ARGV_SIZE, 1); } sprintf(envEntry[0], "POSTPORT=%d", PostPortName); putenv(envEntry[0]); ... } Robert -- +------------------------------------------------------------------------+ | Robert Patrick (rp2y+@edrc.cmu.edu) Engineering Design Research Center | | n-dim Group Carnegie Mellon University | | World Wide Web: http://tika.ndim.edrc.cmu.edu/~rp2y/Home.html | +------------------------------------------------------------------------+ -- +------------------------------------------------------------------------+ | Robert Patrick (rp2y+@edrc.cmu.edu) Engineering Design Research Center | | n-dim Group Carnegie Mellon University | | World Wide Web: http://tika.ndim.edrc.cmu.edu/~rp2y/Home.html | +------------------------------------------------------------------------+ ============================================================================== To add/remove yourself to/from the POSTGRES mailing list: send mail with the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU". If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and a human will deal with it. DO NOT post to the "postgres" mailing list. ============================================================================== URL: http://s2k-ftp.CS.Berkeley.EDU:8000/postgres/