From 2ee3ac7fd334d0fe044d8f3aa49636a86b01e026 Mon Sep 17 00:00:00 2001
From: Eric Blake <ebb9@byu.net>
Date: Thu, 8 Oct 2009 10:09:05 -0600
Subject: [PATCH] areadlink: use SIZE_MAX consistently

* modules/areadlink (Depends-on): Add stdint.
* modules/areadlink-with-size (Depends-on): Likewise.
* lib/areadlink-with-size.c (includes): Drop stdio, since stdlib
gives NULL; drop sys/types, since unistd gives size_t; and add
stdint for SIZE_MAX.
(SIZE_MAX): Rely on headers.
* lib/areadlinkat-with-size.c (includes): Drop stdio, sys/types,
and add stdint.
* lib/areadlink.c (includes): Drop sys/types, and add stdint.
(SIZE_MAX): Likewise.
(INITIAL_BUF_SIZE): Turn into enum.
* lib/areadlinkat.c (INITIAL_BUF_SIZE): Likewise.

Signed-off-by: Eric Blake <ebb9@byu.net>
---
 ChangeLog                   | 16 ++++++++++++++++
 lib/areadlink-with-size.c   |  8 ++------
 lib/areadlink.c             | 21 ++++++++++-----------
 lib/areadlinkat-with-size.c |  3 +--
 lib/areadlinkat.c           | 15 ++++++++-------
 modules/areadlink           |  3 ++-
 modules/areadlink-with-size |  1 +
 7 files changed, 40 insertions(+), 27 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3af4f51209..3f191977aa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2009-10-08  Eric Blake  <ebb9@byu.net>
+
+	areadlink: use SIZE_MAX consistently
+	* modules/areadlink (Depends-on): Add stdint.
+	* modules/areadlink-with-size (Depends-on): Likewise.
+	* lib/areadlink-with-size.c (includes): Drop stdio, since stdlib
+	gives NULL; drop sys/types, since unistd gives size_t; and add
+	stdint for SIZE_MAX.
+	(SIZE_MAX): Rely on headers.
+	* lib/areadlinkat-with-size.c (includes): Drop stdio, sys/types,
+	and add stdint.
+	* lib/areadlink.c (includes): Drop sys/types, and add stdint.
+	(SIZE_MAX): Likewise.
+	(INITIAL_BUF_SIZE): Turn into enum.
+	* lib/areadlinkat.c (INITIAL_BUF_SIZE): Likewise.
+
 2009-10-08  Jim Meyering  <meyering@redhat.com>
 
 	areadlinkat: avoid compilation failure
diff --git a/lib/areadlink-with-size.c b/lib/areadlink-with-size.c
index d0bcb70ad5..625ddb795e 100644
--- a/lib/areadlink-with-size.c
+++ b/lib/areadlink-with-size.c
@@ -1,7 +1,7 @@
 /* readlink wrapper to return the link name in malloc'd storage.
    Unlike xreadlink and xreadlink_with_size, don't ever call exit.
 
-   Copyright (C) 2001, 2003-2007 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2003-2007, 2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -22,16 +22,12 @@
 
 #include "areadlink.h"
 
-#include <stdio.h>
 #include <errno.h>
 #include <limits.h>
-#include <sys/types.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
 
-#ifndef SIZE_MAX
-# define SIZE_MAX ((size_t) -1)
-#endif
 #ifndef SSIZE_MAX
 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
 #endif
diff --git a/lib/areadlink.c b/lib/areadlink.c
index 4e770eb410..f6871c572b 100644
--- a/lib/areadlink.c
+++ b/lib/areadlink.c
@@ -1,7 +1,7 @@
 /* areadlink.c -- readlink wrapper to return the link name in malloc'd storage
    Unlike xreadlink and xreadlink_with_size, don't ever call exit.
 
-   Copyright (C) 2001, 2003-2007 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2003-2007, 2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -24,20 +24,23 @@
 /* Specification.  */
 #include "areadlink.h"
 
-#include <string.h>
 #include <errno.h>
 #include <limits.h>
-#include <sys/types.h>
+#include <stdint.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
-#ifndef SIZE_MAX
-# define SIZE_MAX ((size_t) -1)
-#endif
 #ifndef SSIZE_MAX
 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
 #endif
 
+/* The initial buffer size for the link value.  A power of 2
+   detects arithmetic overflow earlier, but is not required.  */
+enum {
+  INITIAL_BUF_SIZE = 1024
+};
+
 /* Call readlink to get the symbolic link value of FILENAME.
    Return a pointer to that NUL-terminated string in malloc'd storage.
    If readlink fails, return NULL and set errno.
@@ -47,17 +50,13 @@
 char *
 areadlink (char const *filename)
 {
-  /* The initial buffer size for the link value.  A power of 2
-     detects arithmetic overflow earlier, but is not required.  */
-#define INITIAL_BUF_SIZE 1024
-
   /* Allocate the initial buffer on the stack.  This way, in the common
      case of a symlink of small size, we get away with a single small malloc()
      instead of a big malloc() followed by a shrinking realloc().  */
   char initial_buf[INITIAL_BUF_SIZE];
 
   char *buffer = initial_buf;
-  size_t buf_size = sizeof (initial_buf);
+  size_t buf_size = sizeof initial_buf;
 
   while (1)
     {
diff --git a/lib/areadlinkat-with-size.c b/lib/areadlinkat-with-size.c
index d1abb5b011..4ad76b77c5 100644
--- a/lib/areadlinkat-with-size.c
+++ b/lib/areadlinkat-with-size.c
@@ -23,10 +23,9 @@
 
 #include "areadlink.h"
 
-#include <stdio.h>
 #include <errno.h>
 #include <limits.h>
-#include <sys/types.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
 
diff --git a/lib/areadlinkat.c b/lib/areadlinkat.c
index 07714d0a9e..f9a3dafbb1 100644
--- a/lib/areadlinkat.c
+++ b/lib/areadlinkat.c
@@ -25,12 +25,11 @@
 /* Specification.  */
 #include "areadlink.h"
 
-#include <string.h>
 #include <errno.h>
 #include <limits.h>
-#include <sys/types.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
 #ifndef SSIZE_MAX
@@ -39,6 +38,12 @@
 
 #if HAVE_READLINKAT
 
+/* The initial buffer size for the link value.  A power of 2
+   detects arithmetic overflow earlier, but is not required.  */
+enum {
+  INITIAL_BUF_SIZE = 1024
+};
+
 /* Call readlinkat to get the symbolic link value of FILENAME relative to FD.
    Return a pointer to that NUL-terminated string in malloc'd storage.
    If readlinkat fails, return NULL and set errno (although failure to
@@ -49,17 +54,13 @@
 char *
 areadlinkat (int fd, char const *filename)
 {
-  /* The initial buffer size for the link value.  A power of 2
-     detects arithmetic overflow earlier, but is not required.  */
-# define INITIAL_BUF_SIZE 1024
-
   /* Allocate the initial buffer on the stack.  This way, in the common
      case of a symlink of small size, we get away with a single small malloc()
      instead of a big malloc() followed by a shrinking realloc().  */
   char initial_buf[INITIAL_BUF_SIZE];
 
   char *buffer = initial_buf;
-  size_t buf_size = sizeof (initial_buf);
+  size_t buf_size = sizeof initial_buf;
 
   while (1)
     {
diff --git a/modules/areadlink b/modules/areadlink
index 2acc4a90a8..dc7cfe49ae 100644
--- a/modules/areadlink
+++ b/modules/areadlink
@@ -6,10 +6,11 @@ lib/areadlink.h
 lib/areadlink.c
 
 Depends-on:
+malloc-posix
 readlink
 ssize_t
+stdint
 unistd
-malloc-posix
 
 configure.ac:
 
diff --git a/modules/areadlink-with-size b/modules/areadlink-with-size
index df6db33b0a..212f8ab77a 100644
--- a/modules/areadlink-with-size
+++ b/modules/areadlink-with-size
@@ -8,6 +8,7 @@ lib/areadlink-with-size.c
 Depends-on:
 readlink
 ssize_t
+stdint
 unistd
 
 configure.ac:
-- 
2.39.5