public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Ulrich Drepper <drepper@redhat.com>
Cc: Glibc hackers <libc-hacker@sources.redhat.com>
Subject: [PATCH] Fix up regexp.h
Date: Thu, 05 Jun 2008 19:49:00 -0000	[thread overview]
Message-ID: <20080605200208.GU3726@sunsite.mff.cuni.cz> (raw)

Hi!

The BZ #5607 namespace cleanups broke this junk header when not
compiled with -D_GNU_SOURCE, as can be seen e.g. on:

#include <stdlib.h>
#include <string.h>

#define INIT unsigned char *curp = (unsigned char *) instring;
#define GETC() (*curp++)
#define PEEKC() (*curp)
#define UNGETC(c) (curp--)
#define RETURN(c) return (char *) (c)
#define ERROR(c) abort ()

#include <regexp.h>

int
main (void)
{
  char buf[8 * sizeof (regex_t)];
  char *p = compile ("foo", buf, buf + sizeof buf, -1);
  if ((unsigned long) p < 100)
    return 1;
  if (step ("barfoobaz", buf) != 1)
    return 1;
  if (strcmp (loc1, "foobaz") || strcmp (loc2, "baz"))
    return 1;
  p = compile ("", buf, buf + sizeof buf, -1);
  if ((unsigned long) p < 100)
    return 1;
  if (step ("bar", buf) != 1)
    return 1;
  if (strcmp (loc1, "bar") || strcmp (loc2, "bar"))
    return 1;
  return 0;
}

The following patch fixes that as well as a segfault
when the pattern is an empty string.

2008-06-05  Jakub Jelinek  <jakub@redhat.com>

	* misc/regexp.h (compile): Use __REPB_PREFIX macro.
	Avoid segfault if first GETC returns eof/'\0'/'\n'.

--- libc/misc/regexp.h.jj	2004-05-19 18:52:44.000000000 +0200
+++ libc/misc/regexp.h	2008-06-05 21:40:33.000000000 +0200
@@ -1,4 +1,5 @@
-/* Copyright (C) 1996, 1997, 1998, 1999, 2004 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997, 1998, 1999, 2004, 2008
+   Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
 
@@ -129,8 +130,9 @@ compile (char *__restrict instring, char
   __expr_ptr = (regex_t *) expbuf;
   /* The remaining space in the buffer can be used for the compiled
      pattern.  */
-  __expr_ptr->buffer = expbuf + sizeof (regex_t);
-  __expr_ptr->allocated = endbuf -  (char *) __expr_ptr->buffer;
+  __expr_ptr->__REPB_PREFIX (buffer) = expbuf + sizeof (regex_t);
+  __expr_ptr->__REPB_PREFIX (allocated)
+    = endbuf - (char *) __expr_ptr->__REPB_PREFIX (buffer);
 
   while ((__ch = (GETC ())) != eof)
     {
@@ -162,7 +164,10 @@ compile (char *__restrict instring, char
 	}
       __input_buffer[__current_size++] = __ch;
     }
-  __input_buffer[__current_size++] = '\0';
+  if (__current_size)
+    __input_buffer[__current_size++] = '\0';
+  else
+    __input_buffer = "";
 
   /* Now compile the pattern.  */
   __error = regcomp (__expr_ptr, __input_buffer, REG_NEWLINE);
@@ -198,7 +203,8 @@ compile (char *__restrict instring, char
       }
 
   /* Everything is ok.  */
-  RETURN ((char *) (__expr_ptr->buffer + __expr_ptr->used));
+  RETURN ((char *) (__expr_ptr->__REPB_PREFIX (buffer)
+		    + __expr_ptr->__REPB_PREFIX (used)));
 }
 #endif
 


	Jakub

                 reply	other threads:[~2008-06-05 19:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080605200208.GU3726@sunsite.mff.cuni.cz \
    --to=jakub@redhat.com \
    --cc=drepper@redhat.com \
    --cc=libc-hacker@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).