Source-Changes-D archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: CVS commit: src/usr.bin/ftp



Hi,

After this commit, make fetch in pkgsrc broken. It gets segfault.
I cannot fetch tarball from github.com like as follows:

$ ftp https://github.com/intel/libva/releases/download/2.24.1/libva-2.24.1.tar.bz2
Requesting https://github.com/intel/libva/releases/download/2.24.1/libva-2.24.1.tar.bz2
Redirected to https://release-assets.githubusercontent.com/github-production-release-asset/16745233/af00959a-1177-46aa-a834-650739b8afaa?sp=r&sv=2018-11-09&sr=b&spr=https&se=2026-07-08T11%3A51%3A32Z&rscd=attachment%3B+filename%3Dlibva-2.24.1.tar.bz2&rsct=application%2Foctet-stream&skoid=96c2d410-5711-43a1-aedd-ab1947aa7ab0&sktid=398a6654-997b-47e9-b12b-9515b896b4de&skt=2026-07-08T10%3A51%3A12Z&ske=2026-07-08T11%3A51%3A32Z&sks=b&skv=2018-11-09&sig=bloGic59Bd929UK%2BpS%2Foepg1nq47CmD1MsVnwqdUYbc%3D&jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmVsZWFzZS1hc3NldHMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwia2V5Ijoia2V5MSIsImV4cCI6MTc4MzUwOTIxNywibmJmIjoxNzgzNTA4OTE3LCJwYXRoIjoicmVsZWFzZWFzc2V0cHJvZHVjdGlvbi5ibG9iLmNvcmUud2luZG93cy5uZXQifQ.0UzkPm6BJrn1dL4jGYAS9lSt6V0nf8OTI-fld3kR7n4&response-content-disposition=attachment%3B%20filename%3Dlibva-2.24.1.tar.bz2&response-content-type=application%2Foctet-stream
zsh: segmentation fault (core dumped)  ftp


$ gdb /usr/bin/ftp ftp.core
GNU gdb (GDB) 17.1
Copyright (C) 2025 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64--netbsd".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /usr/bin/ftp...
Reading symbols from /usr/libdata/debug/usr/bin/ftp.debug...
[New process 15724]
Core was generated by `ftp'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000079f50b1979b2 in memcpy () from /usr/lib/libc.so.12
(gdb) bt
#0  0x000079f50b1979b2 in memcpy () from /usr/lib/libc.so.12
#1  0x000000000010d53a in remove_dot_segments (path=<optimized out>, len=<optimized out>) at /usr/src/usr.bin/ftp/uri.c:216
#2  0x000000000010d7d8 in make_absurl (
    refurl=refurl@entry=0x79f50adfd000 "https://release-assets.githubusercontent.com/github-production-release-asset/16745233/af00959a-1177-46aa-a834-650739b8afaa?sp=r&sv=2018-11-09&sr=b&spr=https&se=2026-07-08T11%3A51%3A32Z&rscd=attachment";...,
    baseurl=baseurl@entry=0x7f7fffb3205c "https://github.com/intel/libva/releases/download/2.24.1/libva-2.24.1.tar.bz2";) at /usr/src/usr.bin/ftp/uri.c:271
#3  0x00000000001047fc in negotiate_connection (fin=0x79f50af70840, url=0x7f7fffb3205c "https://github.com/intel/libva/releases/download/2.24.1/libva-2.24.1.tar.bz2";,
    penv=0x79f50af9c100 "/github-production-release-asset/16745233/af00959a-1177-46aa-a834-650739b8afaa?s/github-production-release-asset/16745233/af00959a-1177-46aa-a834-650739b8afaa?s/github-production-release-asset/1674523"..., pi=<synthetic pointer>, mtime=0x7f7fffb309f0, wauth=0x7f7fffb30a80, pauth=0x7f7fffb30aa0, rval=0x7f7fffb30968,
    ischunked=0x7f7fffb3096c, auth=0x7f7fffb309a8, ui=0x7f7fffb30ac0) at /usr/src/usr.bin/ftp/fetch.c:1250
#4  fetch_url (url=0x7f7fffb3205c "https://github.com/intel/libva/releases/download/2.24.1/libva-2.24.1.tar.bz2";, proxyenv=proxyenv@entry=0x0, proxyauth=proxyauth@entry=0x0,
    wwwauth=wwwauth@entry=0x0, rui=rui@entry=0x0) at /usr/src/usr.bin/ftp/fetch.c:1635
#5  0x0000000000104da0 in go_fetch (url=<optimized out>, rui=rui@entry=0x0) at /usr/src/usr.bin/ftp/fetch.c:2370
#6  0x00000000001051dc in auto_fetch (argc=1, argv=0x7f7fffb31a50) at /usr/src/usr.bin/ftp/fetch.c:2413
#7  0x0000000000112857 in main (argc=<optimized out>, argv=<optimized out>) at /usr/src/usr.bin/ftp/main.c:550
(gdb)

Thank you.


"Michael van Elst" <mlelstv%netbsd.org@localhost> writes:

> Module Name:	src
> Committed By:	mlelstv
> Date:		Mon Jul  6 07:07:00 UTC 2026
>
> Modified Files:
> 	src/usr.bin/ftp: Makefile fetch.c
> Added Files:
> 	src/usr.bin/ftp: uri.c uri.h
>
> Log Message:
> Evaluate relative URLs in a HTTP redirect.
>
> Fixes PR 60346
>
>
> To generate a diff of this commit:
> cvs rdiff -u -r1.45 -r1.46 src/usr.bin/ftp/Makefile
> cvs rdiff -u -r1.246 -r1.247 src/usr.bin/ftp/fetch.c
> cvs rdiff -u -r0 -r1.1 src/usr.bin/ftp/uri.c src/usr.bin/ftp/uri.h
>
> Please note that diffs are not public domain; they are subject to the
> copyright notices on the relevant files.
>
> Modified files:
>
> Index: src/usr.bin/ftp/Makefile
> diff -u src/usr.bin/ftp/Makefile:1.45 src/usr.bin/ftp/Makefile:1.46
> --- src/usr.bin/ftp/Makefile:1.45	Sun Feb  8 09:00:54 2026
> +++ src/usr.bin/ftp/Makefile	Mon Jul  6 07:07:00 2026
> @@ -1,4 +1,4 @@
> -#	$NetBSD: Makefile,v 1.45 2026/02/08 09:00:54 lukem Exp $
> +#	$NetBSD: Makefile,v 1.46 2026/07/06 07:07:00 mlelstv Exp $
>  #	from: @(#)Makefile	8.2 (Berkeley) 4/3/94
>  
>  .include <bsd.own.mk>
> @@ -7,7 +7,7 @@ USE_FORT?= yes	# network client
>  
>  PROG=	ftp
>  SRCS=	cmds.c cmdtab.c complete.c domacro.c fetch.c ftp.c main.c \
> -	progressbar.c ruserpass.c util.c
> +	progressbar.c ruserpass.c uri.c util.c
>  SRCS+=	ssl.c
>  
>  # Uncomment the following to provide defaults for gate-ftp operation
>
> Index: src/usr.bin/ftp/fetch.c
> diff -u src/usr.bin/ftp/fetch.c:1.246 src/usr.bin/ftp/fetch.c:1.247
> --- src/usr.bin/ftp/fetch.c:1.246	Sun Feb  8 09:00:54 2026
> +++ src/usr.bin/ftp/fetch.c	Mon Jul  6 07:07:00 2026
> @@ -1,4 +1,4 @@
> -/*	$NetBSD: fetch.c,v 1.246 2026/02/08 09:00:54 lukem Exp $	*/
> +/*	$NetBSD: fetch.c,v 1.247 2026/07/06 07:07:00 mlelstv Exp $	*/
>  
>  /*-
>   * Copyright (c) 1997-2026 The NetBSD Foundation, Inc.
> @@ -37,7 +37,7 @@
>  
>  #include <sys/cdefs.h>
>  #ifndef lint
> -__RCSID("$NetBSD: fetch.c,v 1.246 2026/02/08 09:00:54 lukem Exp $");
> +__RCSID("$NetBSD: fetch.c,v 1.247 2026/07/06 07:07:00 mlelstv Exp $");
>  #endif /* not lint */
>  
>  /*
> @@ -68,6 +68,7 @@ __RCSID("$NetBSD: fetch.c,v 1.246 2026/0
>  
>  #include "ssl.h"
>  #include "ftp_var.h"
> +#include "uri.h"
>  #include "version.h"
>  
>  typedef enum {
> @@ -1246,6 +1247,7 @@ negotiate_connection(FETCH *fin, const c
>  			if (verbose)
>  				fprintf(ttyout, "Redirected to %s\n",
>  				    location);
> +			location = make_absurl(location, url);
>  			*rval = go_fetch(location, ui);
>  		}
>  		goto cleanup_fetch_url;
>
> Added files:
>
> Index: src/usr.bin/ftp/uri.c
> diff -u /dev/null src/usr.bin/ftp/uri.c:1.1
> --- /dev/null	Mon Jul  6 07:07:00 2026
> +++ src/usr.bin/ftp/uri.c	Mon Jul  6 07:07:00 2026
> @@ -0,0 +1,314 @@
> +/*	$NetBSD: uri.c,v 1.1 2026/07/06 07:07:00 mlelstv Exp $	*/
> +
> +/*-
> + * Copyright (c) 2026 The NetBSD Foundation, Inc.
> + * All rights reserved.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *    notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *    notice, this list of conditions and the following disclaimer in the
> + *    documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
> + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
> + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
> + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
> + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
> + * POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#include <stdlib.h>
> +#include <stdio.h>
> +#include <string.h>
> +
> +#include "ssl.h"
> +#include "ftp_var.h"
> +#include "uri.h"
> +
> +typedef struct {
> +	const char *buf;
> +	size_t scheme;
> +	size_t authority;
> +	size_t path;
> +	size_t query;
> +	size_t fragment;
> +} uriparts_t;
> +
> +#define SCHEME(p) ((p)->buf)
> +#define AUTHORITY(p) (SCHEME(p) + (p)->scheme)
> +#define PATH(p) (AUTHORITY(p) + (p)->authority)
> +#define QUERY(p) (PATH(p) + (p)->path)
> +#define FRAGMENT(p) (QUERY(p) + (p)->query)
> +
> +static void
> +split_uri(const char *uri, uriparts_t *parts)
> +{
> +	char *colon, *slash, *query, *sharp;
> +
> +	parts->buf = uri;
> +	parts->scheme = 0;
> +	parts->authority = 0;
> +	parts->path = 0;
> +	parts->query = 0;
> +	parts->fragment = 0;
> +
> +	colon = strchr(uri, ':');
> +	slash = strchr(uri, '/');
> +	if (colon && (slash == NULL || (colon - uri) < (slash - uri))) {
> +		parts->scheme = colon + 1 - uri;
> +		uri = colon + 1;
> +	}       
> +
> +	if (uri[0] == '/' && uri[1] == '/') {
> +		slash = strchr(uri + 2, '/');
> +		if (slash == NULL) {
> +			parts->authority = strlen(uri); 
> +			return;
> +		}
> +		parts->authority = slash - uri;
> +		uri = slash;
> +	}       
> +
> +	query = strchr(uri, '?');
> +	sharp = strchr(uri, '#');
> +	if (query && (sharp == NULL || (query - uri) < (sharp - uri))) {
> +		parts->path = query - uri;
> +		uri = query;
> +		if (sharp != NULL) {
> +			parts->query = sharp - query;
> +			parts->fragment = strlen(sharp);
> +			return;
> +		}
> +		parts->query = strlen(query);
> +		return;
> +	}
> +
> +	if (sharp != NULL) {
> +		parts->path = sharp - uri;
> +		parts->fragment = strlen(sharp);
> +		return;
> +	}
> +
> +	parts->path = strlen(uri);
> +}
> +
> +static char *
> +build_uri(uriparts_t *s, uriparts_t *a, uriparts_t *p,
> +    uriparts_t *q, uriparts_t *f)
> +{
> +	size_t len;
> +	char *buf, *out;
> +
> +	len = s->scheme + a->authority + p->path + q->query + f->fragment;
> +	buf = ftp_malloc(len + 1);
> +
> +	out = buf;
> +	memcpy(out, SCHEME(s), s->scheme);
> +	out += s->scheme;
> +	memcpy(out, AUTHORITY(a), a->authority);
> +	out += a->authority;
> +	memcpy(out, PATH(p), p->path);
> +	out += p->path;
> +	memcpy(out, QUERY(q), q->query);
> +	out += q->query;
> +	memcpy(out, FRAGMENT(f), f->fragment);
> +	out += f->fragment;
> +	*out = '\0';
> +
> +	return buf;
> +}
> +
> +static char *
> +remove_dot_segments(const char *path, size_t len)
> +{
> +	char *p;
> +	char *buf, *out, *seg;
> +	size_t n;
> +
> +	p = ftp_malloc(len + 1);
> +	strcpy(p, path);
> +
> +	buf = ftp_malloc(len + 1);
> +	out = buf;
> +	*out = '\0';
> +
> +	while (len > 0) {
> +		/* A */
> +		if (p[0] == '.') {
> +			if (len > 1 && p[1] == '/') {
> +				p += 2;
> +				len -= 2;
> +				continue;
> +			} else if (len > 2 && p[1] == '.' && p[2] == '/') {
> +				p += 3;
> +				len -= 3;
> +				continue;
> +			}
> +		}
> +
> +		/* B */
> +		if (p[0] == '/') {
> +			if (len > 2 && p[1] == '.' && p[2] == '/') {
> +				p += 2;
> +				len -= 2;
> +				continue;
> +			} else if (len == 2 && p[1] == '.') {
> +				p += 1;
> +				len -= 1;
> +				p[0] = '/';
> +				continue;
> +			}
> +		}
> +
> +		/* C */
> +		if (len > 2 && p[0] == '/' && p[1] == '.' && p[2] == '.') {
> +			if (len > 3 && p[3] == '/') {
> +				p += 3;
> +				len -= 3;
> +				seg = strrchr(buf, '/');
> +				if (seg != NULL)
> +					*seg = '\0';
> +				continue;
> +			} else if (len == 3) {
> +				p += 2;
> +				len -= 2;
> +				p[0] = '/';
> +				seg = strrchr(buf, '/');
> +				if (seg != NULL)
> +					*seg = '\0';
> +				continue;
> +			}
> +		}
> +
> +		/* D */
> +		if (p[0] == '.') {
> +			if (len == 1) {
> +				p += 1;
> +				len -= 1;
> +				continue;
> +			} else if (len == 2 && p[1] == '.') {
> +				p += 2;
> +				len -= 2;
> +				continue;
> +			}
> +		}
> +
> +		/* E */
> +		if (p[0] == '/') {
> +			seg = strchr(p+1, '/');
> +		} else {
> +			seg = strchr(p, '/');
> +		}
> +		if (seg != NULL)
> +			n = seg - p;
> +		else
> +			n = len;
> +
> +		memcpy(out, p, n);
> +		out += n;
> +		*out = '\0';
> +		p += n;
> +		len -= n;
> +	};
> +
> +	FREEPTR(p);
> +
> +	return buf;
> +}
> +
> +static char *
> +merge_paths(uriparts_t *base, uriparts_t *ref)
> +{
> +	char *buf;
> +	const char *path, *last;
> +	size_t len;
> +
> +	if (base->authority && base->path == 0) {
> +		len = 1 + ref->path;
> +		buf = ftp_malloc(len + 1);
> +		buf[0] = '/';
> +		memcpy(buf + 1, PATH(ref), ref->path);
> +	} else {
> +		path = PATH(base);
> +		last = strrchr(path, '/');
> +		if (last == NULL || (size_t)(last - path) > base->path)
> +			len = 0;
> +		else
> +			len = (size_t)(last - path) + 1;
> +		buf = ftp_malloc(len + ref->path + 1);
> +		memcpy(buf, path, len);
> +		memcpy(buf + len, PATH(ref), ref->path);
> +		len += ref->path;
> +	}
> +	buf[len] = '\0';
> +
> +	return buf;
> +}
> +
> +char *
> +make_absurl(char *refurl, const char *baseurl)
> +{
> +	uriparts_t base;
> +	uriparts_t ref;
> +	uriparts_t p;
> +	char *buf, *tmp1, *tmp2;
> +	
> +	split_uri(baseurl, &base);
> +	split_uri(refurl, &ref);
> +
> +	p.scheme = p.authority = p.path = p.query = p.fragment = 0;
> +
> +	if (ref.scheme) {
> +		tmp1 = remove_dot_segments(PATH(&ref), ref.path);
> +		p.buf = tmp1;
> +		p.path = strlen(tmp1);
> +		buf = build_uri(&ref, &ref, &p, &ref, &ref);
> +		FREEPTR(tmp1);
> +	} else {
> +		if (ref.authority) {
> +			tmp1 = remove_dot_segments(PATH(&ref), ref.path);
> +			p.buf = tmp1;
> +			p.path = strlen(tmp1);
> +			buf = build_uri(&base, &ref, &p, &ref, &ref);
> +			FREEPTR(tmp1);
> +		} else {
> +			if (ref.path == 0) {
> +				if (ref.query) {
> +					buf = build_uri(&base, &base, &base, &ref, &ref);
> +				} else {
> +					buf = build_uri(&base, &base, &base, &base, &ref);
> +				}
> +			} else {
> +				if (PATH(&ref)[0] == '/') {
> +					tmp1 = remove_dot_segments(PATH(&ref), ref.path);
> +					p.buf = tmp1;
> +					p.path = strlen(tmp1);
> +					buf = build_uri(&base, &base, &p, &ref, &ref);
> +					FREEPTR(tmp1);
> +				} else {
> +					tmp2 = merge_paths(&base, &ref);
> +					tmp1 = remove_dot_segments(tmp2, strlen(tmp2));
> +					p.buf = tmp1;
> +					p.path = strlen(tmp1);
> +					FREEPTR(tmp2);
> +
> +					buf = build_uri(&base, &base, &p, &ref, &ref);
> +					FREEPTR(tmp1);
> +				}
> +			}
> +		}
> +	}
> +
> +	FREEPTR(refurl);
> +
> +	return buf;
> +}
> Index: src/usr.bin/ftp/uri.h
> diff -u /dev/null src/usr.bin/ftp/uri.h:1.1
> --- /dev/null	Mon Jul  6 07:07:00 2026
> +++ src/usr.bin/ftp/uri.h	Mon Jul  6 07:07:00 2026
> @@ -0,0 +1,29 @@
> +/*	$NetBSD: uri.h,v 1.1 2026/07/06 07:07:00 mlelstv Exp $	*/
> +
> +/*-
> + * Copyright (c) 2026 The NetBSD Foundation, Inc.
> + * All rights reserved.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *    notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *    notice, this list of conditions and the following disclaimer in the
> + *    documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
> + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
> + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
> + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
> + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
> + * POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +char *make_absurl(char *, const char *);
>

-- 
Ryo ONODERA // ryo%tetera.org@localhost
PGP fingerprint = 82A2 DC91 76E0 A10A 8ABB  FD1B F404 27FA C7D1 15F3


Home | Main Index | Thread Index | Old Index