File 0001-possibly-truncate-pathname-components.patch of Package wget.36152
--- wget-1.20.3/src/url.c 2019-04-01 20:11:12.000000000 +0200
+++ wget-1.20.3-new/src/url.c 2024-10-23 17:51:42.463169213 +0200
@@ -1463,6 +1463,7 @@ append_uri_pathel (const char *b, const
{
const char *p;
int quoted, outlen;
+ int max_length;
int mask;
if (opt.restrict_files_os == restrict_unix)
@@ -1503,6 +1504,18 @@ append_uri_pathel (const char *b, const
string length. Each quoted char introduces two additional
characters in the string, hence 2*quoted. */
outlen = (e - b) + (2 * quoted);
+# ifdef WINDOWS
+ max_length = MAX_PATH;
+# else
+ max_length = get_max_length(dest->base, dest->tail, _PC_NAME_MAX);
+# endif
+ max_length -= CHOMP_BUFFER;
+ if (max_length > 0 && outlen > max_length)
+ {
+ logprintf (LOG_NOTQUIET, "The destination name is too long (%d), reducing to %d\n", outlen, max_length);
+
+ outlen = max_length;
+ }
GROW (dest, outlen);
if (!quoted)
@@ -1514,19 +1527,29 @@ append_uri_pathel (const char *b, const
else
{
char *q = TAIL (dest);
- for (p = b; p < e; p++)
+ int i;
+
+ for (i = 0, p = b; p < e; p++)
{
if (!FILE_CHAR_TEST (*p, mask))
- *q++ = *p;
- else
+ {
+ if (i == outlen)
+ break;
+ *q++ = *p;
+ i++;
+ }
+ else if (i + 3 > outlen)
+ break;
+ else
{
unsigned char ch = *p;
*q++ = '%';
*q++ = XNUM_TO_DIGIT (ch >> 4);
*q++ = XNUM_TO_DIGIT (ch & 0xf);
+ i += 3;
}
}
- assert (q - TAIL (dest) == outlen);
+ assert (q - TAIL (dest) <= outlen);
}
/* Perform inline case transformation if required. */
@@ -1686,7 +1709,6 @@ url_file_name (const struct url *u, char
const char *u_file;
char *fname, *unique, *fname_len_check;
const char *index_filename = "index.html"; /* The default index file is index.html */
- size_t max_length;
fnres.base = NULL;
fnres.size = 0;
@@ -1780,41 +1802,8 @@ url_file_name (const struct url *u, char
temp_fnres.size = 0;
temp_fnres.tail = 0;
append_string (fname, &temp_fnres);
- xfree (fname);
-
- /* Check that the length of the file name is acceptable. */
-#ifdef WINDOWS
- if (MAX_PATH > (fnres.tail + CHOMP_BUFFER + 2))
- {
- max_length = MAX_PATH - (fnres.tail + CHOMP_BUFFER + 2);
- /* FIXME: In Windows a filename is usually limited to 255 characters.
- To really be accurate you could call GetVolumeInformation() to get
- lpMaximumComponentLength
- */
- if (max_length > 255)
- {
- max_length = 255;
- }
- }
- else
- {
- max_length = 0;
- }
-#else
- max_length = get_max_length (fnres.base, fnres.tail, _PC_NAME_MAX) - CHOMP_BUFFER;
-#endif
- if (max_length > 0 && strlen (temp_fnres.base) > max_length)
- {
- logprintf (LOG_NOTQUIET, "The name is too long, %lu chars total.\n",
- (unsigned long) strlen (temp_fnres.base));
- logprintf (LOG_NOTQUIET, "Trying to shorten...\n");
-
- /* Shorten the file name. */
- temp_fnres.base[max_length] = '\0';
-
- logprintf (LOG_NOTQUIET, "New name is %s.\n", temp_fnres.base);
- }
+ xfree (fname);
xfree (fname_len_check);
/* The filename has already been 'cleaned' by append_uri_pathel() above. So,