Re: [BUGS] Bug #756: suggestion: file with password instead - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: [BUGS] Bug #756: suggestion: file with password instead
Date
Msg-id 200209060232.g862Wel10050@candle.pha.pa.us
Whole thread Raw
In response to Re: [BUGS] Bug #756: suggestion: file with password instead of  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-patches
OK, changes made.  The pasting wasn't my idea but was there as part of
the original source:

    sprintf(psqlrc, "%s/.psqlrc-" PG_VERSION, home);

I thought that was the way to do it so I propogated it consistenly.

No unpropoaged.  Thanks for the code review.

---------------------------------------------------------------------------

Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > + #define PSQLHISTORY    "/.psql_history"
> > ...
> > !             char       *psql_history = (char *) malloc(strlen(home) +
> > !                                                 strlen(PSQLHISTORY) + 1);
>
> > !                 sprintf(psql_history, "%s" PSQLHISTORY, home);
>
> This seems like a really ugly coding practice.  The sprintf is hard to
> read and absolutely dependent on the assumption that PSQLHISTORY
> contains no %.  I'd suggest this pattern:
>
> #define PSQLHISTORY    ".psql_history"
> > ...
> > !             char       *psql_history = (char *) malloc(strlen(home) +
> > !                                                 strlen(PSQLHISTORY) + 2);
>
> > !                 sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
>
> as being easier to read and safer.
>
> In PasswordFromFile():
>
> > +     /* Look for it in the home dir */
> > +     home = getenv("HOME");
> > +     if (home)
> > +     {
> > +         pgpassfile = malloc(strlen(home) + strlen(PGPASSFILE) + 1);
> > +         if (!pgpassfile)
> > +         {
> > +             fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
> > +             exit(EXIT_FAILURE);
> > +         }
> > +     }
> > +     else
> > +         return NULL;
>
> libpq has no business calling exit().  How about just "return NULL" like
> all the other failure cases in that routine?
>
>             regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/bin/psql/input.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/input.c,v
retrieving revision 1.20
diff -c -c -r1.20 input.c
*** src/bin/psql/input.c    5 Sep 2002 22:05:50 -0000    1.20
--- src/bin/psql/input.c    6 Sep 2002 02:27:37 -0000
***************
*** 30,36 ****
  static void finishInput(int, void *);
  #endif

! #define PSQLHISTORY    "/.psql_history"


  /*
--- 30,36 ----
  static void finishInput(int, void *);
  #endif

! #define PSQLHISTORY    ".psql_history"


  /*
***************
*** 144,155 ****
          home = getenv("HOME");
          if (home)
          {
!             char       *psql_history = (char *) malloc(strlen(home) +
                                                  strlen(PSQLHISTORY) + 1);

              if (psql_history)
              {
!                 sprintf(psql_history, "%s" PSQLHISTORY, home);
                  read_history(psql_history);
                  free(psql_history);
              }
--- 144,155 ----
          home = getenv("HOME");
          if (home)
          {
!             char       *psql_history = (char *) malloc(strlen(home) + 1 +
                                                  strlen(PSQLHISTORY) + 1);

              if (psql_history)
              {
!                 sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
                  read_history(psql_history);
                  free(psql_history);
              }
***************
*** 204,210 ****
          home = getenv("HOME");
          if (home)
          {
!             psql_history = (char *) malloc(strlen(home) +
                                      strlen(PSQLHISTORY) + 1);
              if (psql_history)
              {
--- 204,210 ----
          home = getenv("HOME");
          if (home)
          {
!             psql_history = (char *) malloc(strlen(home) + 1 +
                                      strlen(PSQLHISTORY) + 1);
              if (psql_history)
              {
***************
*** 212,218 ****

                  if (var)
                      stifle_history(atoi(var));
!                 sprintf(psql_history, "%s" PSQLHISTORY, home);
                  write_history(psql_history);
                  free(psql_history);
              }
--- 212,218 ----

                  if (var)
                      stifle_history(atoi(var));
!                 sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
                  write_history(psql_history);
                  free(psql_history);
              }
Index: src/bin/psql/startup.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/startup.c,v
retrieving revision 1.65
diff -c -c -r1.65 startup.c
*** src/bin/psql/startup.c    5 Sep 2002 22:05:50 -0000    1.65
--- src/bin/psql/startup.c    6 Sep 2002 02:27:39 -0000
***************
*** 42,48 ****
   */
  PsqlSettings pset;

! #define PSQLRC "/.psqlrc"

  /*
   * Structures to pass information between the option parsing routine
--- 42,48 ----
   */
  PsqlSettings pset;

! #define PSQLRC ".psqlrc"

  /*
   * Structures to pass information between the option parsing routine
***************
*** 605,611 ****

      if (home)
      {
!         psqlrc = malloc(strlen(home) + strlen(PSQLRC) + 1 +
                   strlen(PG_VERSION) + 1);
          if (!psqlrc)
          {
--- 605,611 ----

      if (home)
      {
!         psqlrc = malloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
                   strlen(PG_VERSION) + 1);
          if (!psqlrc)
          {
***************
*** 613,624 ****
              exit(EXIT_FAILURE);
          }

!         sprintf(psqlrc, "%s" PSQLRC "-" PG_VERSION, home);
          if (access(psqlrc, R_OK) == 0)
              process_file(psqlrc);
          else
          {
!             sprintf(psqlrc, "%s" PSQLRC, home);
              if (access(psqlrc, R_OK) == 0)
                  process_file(psqlrc);
          }
--- 613,624 ----
              exit(EXIT_FAILURE);
          }

!         sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);
          if (access(psqlrc, R_OK) == 0)
              process_file(psqlrc);
          else
          {
!             sprintf(psqlrc, "%s/%s", home, PSQLRC);
              if (access(psqlrc, R_OK) == 0)
                  process_file(psqlrc);
          }
Index: src/interfaces/libpq/fe-connect.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.203
diff -c -c -r1.203 fe-connect.c
*** src/interfaces/libpq/fe-connect.c    5 Sep 2002 22:24:23 -0000    1.203
--- src/interfaces/libpq/fe-connect.c    6 Sep 2002 02:27:50 -0000
***************
*** 66,72 ****
  #define NOTIFYLIST_INITIAL_SIZE 10
  #define NOTIFYLIST_GROWBY 10

! #define PGPASSFILE "/.pgpass"

  /* ----------
   * Definition of the conninfo parameters and their fallback resources.
--- 66,72 ----
  #define NOTIFYLIST_INITIAL_SIZE 10
  #define NOTIFYLIST_GROWBY 10

! #define PGPASSFILE ".pgpass"

  /* ----------
   * Definition of the conninfo parameters and their fallback resources.
***************
*** 2927,2944 ****
      home = getenv("HOME");
      if (home)
      {
!         pgpassfile = malloc(strlen(home) + strlen(PGPASSFILE) + 1);
          if (!pgpassfile)
          {
-
              fprintf(stderr, libpq_gettext("out of memory\n"));
!             exit(EXIT_FAILURE);
          }
      }
      else
          return NULL;

!     sprintf(pgpassfile, "%s" PGPASSFILE, home);

      /* If password file cannot be opened, ignore it. */
      if (stat(pgpassfile, &stat_buf) == -1)
--- 2927,2943 ----
      home = getenv("HOME");
      if (home)
      {
!         pgpassfile = malloc(strlen(home) + 1 + strlen(PGPASSFILE) + 1);
          if (!pgpassfile)
          {
              fprintf(stderr, libpq_gettext("out of memory\n"));
!             return NULL;
          }
      }
      else
          return NULL;

!     sprintf(pgpassfile, "%s/%s", home, PGPASSFILE);

      /* If password file cannot be opened, ignore it. */
      if (stat(pgpassfile, &stat_buf) == -1)
Index: src/bin/psql/input.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/input.c,v
retrieving revision 1.20
diff -c -c -r1.20 input.c
*** src/bin/psql/input.c    5 Sep 2002 22:05:50 -0000    1.20
--- src/bin/psql/input.c    6 Sep 2002 02:27:37 -0000
***************
*** 30,36 ****
  static void finishInput(int, void *);
  #endif

! #define PSQLHISTORY    "/.psql_history"


  /*
--- 30,36 ----
  static void finishInput(int, void *);
  #endif

! #define PSQLHISTORY    ".psql_history"


  /*
***************
*** 144,155 ****
          home = getenv("HOME");
          if (home)
          {
!             char       *psql_history = (char *) malloc(strlen(home) +
                                                  strlen(PSQLHISTORY) + 1);

              if (psql_history)
              {
!                 sprintf(psql_history, "%s" PSQLHISTORY, home);
                  read_history(psql_history);
                  free(psql_history);
              }
--- 144,155 ----
          home = getenv("HOME");
          if (home)
          {
!             char       *psql_history = (char *) malloc(strlen(home) + 1 +
                                                  strlen(PSQLHISTORY) + 1);

              if (psql_history)
              {
!                 sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
                  read_history(psql_history);
                  free(psql_history);
              }
***************
*** 204,210 ****
          home = getenv("HOME");
          if (home)
          {
!             psql_history = (char *) malloc(strlen(home) +
                                      strlen(PSQLHISTORY) + 1);
              if (psql_history)
              {
--- 204,210 ----
          home = getenv("HOME");
          if (home)
          {
!             psql_history = (char *) malloc(strlen(home) + 1 +
                                      strlen(PSQLHISTORY) + 1);
              if (psql_history)
              {
***************
*** 212,218 ****

                  if (var)
                      stifle_history(atoi(var));
!                 sprintf(psql_history, "%s" PSQLHISTORY, home);
                  write_history(psql_history);
                  free(psql_history);
              }
--- 212,218 ----

                  if (var)
                      stifle_history(atoi(var));
!                 sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
                  write_history(psql_history);
                  free(psql_history);
              }
Index: src/bin/psql/startup.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/startup.c,v
retrieving revision 1.65
diff -c -c -r1.65 startup.c
*** src/bin/psql/startup.c    5 Sep 2002 22:05:50 -0000    1.65
--- src/bin/psql/startup.c    6 Sep 2002 02:27:39 -0000
***************
*** 42,48 ****
   */
  PsqlSettings pset;

! #define PSQLRC "/.psqlrc"

  /*
   * Structures to pass information between the option parsing routine
--- 42,48 ----
   */
  PsqlSettings pset;

! #define PSQLRC ".psqlrc"

  /*
   * Structures to pass information between the option parsing routine
***************
*** 605,611 ****

      if (home)
      {
!         psqlrc = malloc(strlen(home) + strlen(PSQLRC) + 1 +
                   strlen(PG_VERSION) + 1);
          if (!psqlrc)
          {
--- 605,611 ----

      if (home)
      {
!         psqlrc = malloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
                   strlen(PG_VERSION) + 1);
          if (!psqlrc)
          {
***************
*** 613,624 ****
              exit(EXIT_FAILURE);
          }

!         sprintf(psqlrc, "%s" PSQLRC "-" PG_VERSION, home);
          if (access(psqlrc, R_OK) == 0)
              process_file(psqlrc);
          else
          {
!             sprintf(psqlrc, "%s" PSQLRC, home);
              if (access(psqlrc, R_OK) == 0)
                  process_file(psqlrc);
          }
--- 613,624 ----
              exit(EXIT_FAILURE);
          }

!         sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);
          if (access(psqlrc, R_OK) == 0)
              process_file(psqlrc);
          else
          {
!             sprintf(psqlrc, "%s/%s", home, PSQLRC);
              if (access(psqlrc, R_OK) == 0)
                  process_file(psqlrc);
          }
Index: src/interfaces/libpq/fe-connect.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.203
diff -c -c -r1.203 fe-connect.c
*** src/interfaces/libpq/fe-connect.c    5 Sep 2002 22:24:23 -0000    1.203
--- src/interfaces/libpq/fe-connect.c    6 Sep 2002 02:27:50 -0000
***************
*** 66,72 ****
  #define NOTIFYLIST_INITIAL_SIZE 10
  #define NOTIFYLIST_GROWBY 10

! #define PGPASSFILE "/.pgpass"

  /* ----------
   * Definition of the conninfo parameters and their fallback resources.
--- 66,72 ----
  #define NOTIFYLIST_INITIAL_SIZE 10
  #define NOTIFYLIST_GROWBY 10

! #define PGPASSFILE ".pgpass"

  /* ----------
   * Definition of the conninfo parameters and their fallback resources.
***************
*** 2927,2944 ****
      home = getenv("HOME");
      if (home)
      {
!         pgpassfile = malloc(strlen(home) + strlen(PGPASSFILE) + 1);
          if (!pgpassfile)
          {
-
              fprintf(stderr, libpq_gettext("out of memory\n"));
!             exit(EXIT_FAILURE);
          }
      }
      else
          return NULL;

!     sprintf(pgpassfile, "%s" PGPASSFILE, home);

      /* If password file cannot be opened, ignore it. */
      if (stat(pgpassfile, &stat_buf) == -1)
--- 2927,2943 ----
      home = getenv("HOME");
      if (home)
      {
!         pgpassfile = malloc(strlen(home) + 1 + strlen(PGPASSFILE) + 1);
          if (!pgpassfile)
          {
              fprintf(stderr, libpq_gettext("out of memory\n"));
!             return NULL;
          }
      }
      else
          return NULL;

!     sprintf(pgpassfile, "%s/%s", home, PGPASSFILE);

      /* If password file cannot be opened, ignore it. */
      if (stat(pgpassfile, &stat_buf) == -1)

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [BUGS] Bug #756: suggestion: file with password instead
Next
From: Yury Bokhoncovich
Date:
Subject: Big number of "unused" pages as reported by VACUUM