raindrops RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: raindrops@librelist.org
Subject: Re: [PATCH] Remove Scope IDs from IPv6 addresses.
Date: Tue, 10 Sep 2013 18:36:30 +0000	[thread overview]
Message-ID: <20130910183630.GB502@dcvr.yhbt.net> (raw)
In-Reply-To: 20130910183504.GA502@dcvr.yhbt.net

(Moving to the right list, this thread was originally posted to kgio :x)

Hleb Valoshka <375gnu@gmail.com> wrote:
> 	Scoped ipv6 addresses are defined in rfc4007.
> 	Ruby doesn't support them yet and it's unknown whether it will
> 	(see http://bugs.ruby-lang.org/issues/8464).
> 	So we just remove scope ids.

Thanks.  Comments inline.

> --- a/ext/raindrops/linux_inet_diag.c
> +++ b/ext/raindrops/linux_inet_diag.c
> @@ -134,17 +134,54 @@ static int st_free_data(st_data_t key, st_data_t value, st_data_t ignored)

> +static char* remove_scope_id(const char * addr)
> +{
> +        char *newaddr, *t;
> +
> +        newaddr = strdup(addr);
> +        if (newaddr == NULL)
> +                perror("strdup");

We'll segfault on strchr below if we perrored above.  If we continue
using strdup, I would just use ruby_strdup() (works on both MRI/rbx).

But I'd rather avoid *strdup entirely.  This is unlikely to
remove, right?

> +        t = strchr(newaddr, '%');
> +
> +        if (t != NULL) {
> +                *t = '\0';
> +                t = strchr(t+1, ']');
> +                if (t == NULL)
> +                        fprintf(stderr, "Bad IPv6 address: %s!\n", addr);

Better to use rb_warn/rb_warning.
These are preferable since they allow assigning $stderr to StringIO and
such.  But if we don't use strdup, maybe we could raise instead.

> +                else
> +                        strcat(newaddr, t);
> +        }
> +
> +        return newaddr;
> +}

Perhaps something like this (totally untested, likely off-by-one errors)

static VALUE remove_scope_id(const char *addr)
{
	VALUE rv = rb_str_new2(addr);
	long len = RSTRING_LEN(rv);
	char *ptr = RSTRING_PTR(rv);
	char *pct = memchr(ptr, '%', len);

	/*
	 * remove scoped portion
	 * Ruby equivalent: rv.sub!(/%([^\]]*)\]/, "]")
	 */
	if (pct) {
		long newlen = pct - ptr;
		char *rbracket = memchr(pct, ']', len - newlen);

		if (rbracket) {
			size_t move = len - (rbracket - ptr);

			memmove(pct, rbracket, move);
			newlen += move;

			rb_str_set_len(rv, newlen);
		} else {
			/* perhaps raise here? */
			rb_warn("Bad IPv6 address: %s", addr);
		}
	}
	return rv;
}
-- 
Eric Wong


       reply	other threads:[~2013-09-10 18:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1378826261-10771-1-git-send-email-375gnu@gmail.com>
     [not found] ` <20130910183504.GA502@dcvr.yhbt.net>
2013-09-10 18:36   ` Eric Wong [this message]
2013-09-10 19:57     ` Re: [PATCH] Remove Scope IDs from IPv6 addresses Hleb Valoshka
2013-09-10 20:17       ` Eric Wong
2013-09-11 16:12         ` Hleb Valoshka

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

  List information: https://yhbt.net/raindrops/

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

  git send-email \
    --in-reply-to=20130910183630.GB502@dcvr.yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=raindrops@librelist.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/raindrops.git/

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).