Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:17:46+00:00 2026-05-31T02:17:46+00:00

Here is a piece of code from the lib/xreadlink.c file in GNU Coreutils.. /*

  • 0

Here is a piece of code from the lib/xreadlink.c file in GNU Coreutils..

  /* Call readlink to get the symbolic link value of FILENAME.
   +   SIZE is a hint as to how long the link is expected to be;
   +   typically it is taken from st_size.  It need not be correct.
       Return a pointer to that NUL-terminated string in malloc'd storage.
       If readlink fails, return NULL (caller may use errno to diagnose).
       If malloc fails, or if the link value is longer than SSIZE_MAX :-),
       give a diagnostic and exit.  */

 char * xreadlink (char const *filename)
 {
    /* The initial buffer size for the link value.  A power of 2
       detects arithmetic overflow earlier, but is not required.  */
    size_t buf_size = 128;

    while (1)
    {
      char* buffer = xmalloc(buf_size);
      ssize_t link_length = readlink(filename, buffer, buf_size);
      if(link_length < 0)
      {
         /*handle failure of system call*/
      }

      if((size_t) link_length < buf_size)
      {
           buffer[link_length] = 0;
           return buffer;
      }

      /*size not sufficient, allocate more*/
      free (buffer);
      buf_size *= 2;
      /*Check whether increase is possible*/
      if (SSIZE_MAX < buf_size || (SIZE_MAX / 2 < SSIZE_MAX && buf_size == 0))
          xalloc_die ();
   }
}

The code is understandable except I could not understand how the check for whether the link’s size is too big works, that is the line:

      if (SSIZE_MAX < buf_size || (SIZE_MAX / 2 < SSIZE_MAX && buf_size == 0))

Further, how can

     (SIZE_MAX / 2 < SSIZE_MAX) 

condition be true on any system???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:17:47+00:00Added an answer on May 31, 2026 at 2:17 am

    SSIZE_MAX is the maximum value of the signed variety of size_t. For instance if size_t is only 16 bits (very unlikely these days), SIZE_MAX is 65535 while ssize_max is 32767. More likely it is 32 bits (giving 4294967295 and 2147483647 respectively), or even 64 bits (giving numbers too big to type here 🙂 ).

    The basic problem to solve here is that readlink returns a signed value even though SIZE_MAX is an unsigned one … so once buf_size exceeds SSIZE_MAX, it’s impossible to read the link, as the large positive value will result in a negative return value.

    As for the “furthermore” part: it quite likely can’t, i.e., you’re right. At least on any sane system, anyway. (It is theoretically possible to have, e.g., a 32-bit SIZE_MAX but a 33-bit signed integer so that SSIZE_MAX is also 4294967295. Presumably this code is written to guard against theoretically-possible, but never-actually-seen, systems.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a piece of code from the xss_clean method of the Input_Core class of
Here is a piece of code borrowed from the Endogine engine. It is supposed
Here is a piece of normal code from my site for centered images: <img
I found this piece of code from anther post here. It worked as intended
Here is a piece of code from the jQuery source (bit.ly/jqsource): // The DOM
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
Here's a piece of code that takes most time in my program, according to
Here's a piece of code, currently driving me nuts. It's a UIView, with an
Here is a simple piece of code: import java.io.*; public class Read { public
I have a piece of code here that does not work despite me using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.