Note: This is a beta release of Red Hat Bugzilla 5.0. The data contained within is a snapshot of the live data so any changes you make will not be reflected in the production Bugzilla. Also email is disabled so feel free to test any aspect of the site that you want. File any problems you find or give feedback here.
Bug 5131 - getcwd(NULL,1024) does not behave as per man page
Summary: getcwd(NULL,1024) does not behave as per man page
Keywords:
Status: CLOSED WORKSFORME
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: glibc
Version: 6.0
Hardware: i386
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Cristian Gafton
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 1999-09-14 16:40 UTC by cosmo
Modified: 2008-05-01 15:37 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 1999-10-06 17:12:17 UTC


Attachments (Terms of Use)

Description cosmo 1999-09-14 16:40:16 UTC
/*
When called with a NULL buffer and a positive size argument
the man page states that a buffer of size bytes is
allocated.
This does not apear true as appending to the end of the
buffer causes a SEGV when later malloc/free calls are made:
*/

#include <unistd.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
  char *cwd;
  char *chunk;
  size_t buffsize = 1024;
  size_t bigbuffsize = 10000;

  if ((cwd = getcwd(NULL, buffsize)) == NULL)
  {
    printf ("getcwd (NULL, %d) == NULL\n");
    exit (1);
  }
  else
    printf ("cwd: %s\n", cwd);

  strcat (cwd, ":This is a bogus string");

  printf ("Now my buffer contains \"%s\"[%d]\n",
          cwd, strlen(cwd));

  if ((chunk = malloc (10000)) == NULL)
  {
    printf ("malloc(%d) failure!\n", bigbuffsize);
  }
  else
  {
    printf ("Grabbed %d bytes\n", bigbuffsize);
    free (chunk);
  }

  if (cwd)
    free (cwd);

  return (0);
}

Comment 1 Cristian Gafton 1999-10-06 17:12:59 UTC
This function behaves as defined by the Single Unix Specification
version 2. We will have to fix the man page for it, but the behavior
is according to the specs:

The getcwd() function places an absolute pathname of the current
working directory in the array
pointed to by buf, and returns buf. The size argument is the size in
bytes of the character array
pointed to by the buf argument. If buf is a null pointer, the
behaviour of getcwd() is undefined.


Note You need to log in before you can comment on or make changes to this bug.