mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>,
	"barebox@lists.infradead.org" <barebox@lists.infradead.org>
Subject: Re: [PATCH master 3/7] sandbox: support escaping commas in --image filenames
Date: Mon, 14 Sep 2020 15:42:01 +0200	[thread overview]
Message-ID: <b37d7058-b96a-e790-8d7c-2225c1fe130d@pengutronix.de> (raw)
In-Reply-To: <20200914100553.24808-3-a.fatoum@pengutronix.de>

On 9/14/20 12:05 PM, Ahmad Fatoum wrote:
> Some tools like afl-fuzz generate file names containing commas.
> Allow escaping the commas in the file names, so they can be passed
> to barebox.

/* Unit tests for strsep_unescaped */

#include <stdio.h>
#include <string.h>
#include <assert.h>

char *strsep_unescaped(char **s, const char *ct)
{
        char *sbegin = *s, *hay;
        const char *needle;
        size_t shift = 0;

        if (sbegin == NULL)
                return NULL;

        for (hay = sbegin; *hay != '\0'; ++hay) {
                *hay = hay[shift];

                if (*hay == '\\') {
                        *hay = hay[++shift];
                        if (*hay != '\\')
                                continue;
                }

                for (needle = ct; *needle != '\0'; ++needle) {
                        if (*hay == *needle)
                                goto match;
                }
        }

        *s = NULL;
        return sbegin;

match:
        *hay = '\0';
        *s = &hay[shift + 1];

        return sbegin;
}

static _Bool streq(const char *a, const char *b)
{
	if (a == NULL || b == NULL)
		return a == b;
	return strcmp(a, b) == 0;
}

#define ensure(_s, d, ...) do {                              \
	char *expected[] = { __VA_ARGS__,  NULL };           \
	char *tok;                                           \
	char *s = strdup(_s);                                \
	int i = 0;                                           \
	while ((tok = strsep_unescaped(&s, d))) {            \
		printf("'%s' <> '%s'?\n", tok, expected[i]);   \
		assert(streq(tok, expected[i]));             \
		i++;                                         \
	}                                                    \
} while (0)

int main(int argc, char *argv[])
{
	setbuf(stdout, NULL);
	ensure("aaa", ",",       /* => */ "aaa", NULL);
	ensure("bla,bla", ",",   /* => */ "bla", "bla");
	ensure("bla,bAL", ",",   /* => */ "bla", "bAL");
	ensure("2e\\,,bAL", ",", /* => */ "2e,", "bAL");
	ensure("1\\,2,dol", ",", /* => */ "1,2", "dol");
	ensure("1-2,\\,", ",",   /* => */ "1-2", ",");
	ensure("1\\,2,\\,", ",", /* => */ "1,2", ",");
	ensure("oh,oh,oh", ",",  /* => */ "oh", "oh", "oh");
	ensure("oh\\,,oh", ",",  /* => */ "oh,", "oh");
	ensure("oh\\,,,oh", ",", /* => */ "oh,", "", "oh");
	ensure("1-2,\\,", ",",   /* => */   "1-2", ",");
	ensure("1280\\,1024.png,ro", ",",      /* => */ "1280,1024.png", "ro");
	ensure("1280\\1024.png,ro", ",",       /* => */ "12801024.png", "ro");
	ensure("1280\\\\1024.png,ro", ",",     /* => */ "1280\\1024.png", "ro");
	ensure("1280.png\\\\ro", "\\",         /* => */ "1280.png", "ro");
	ensure("1280\\1024.png,ro", "\\",      /* => */ "12801024.png,ro", NULL);
	ensure("1280\\\\1024.png,ro", "\\",    /* => */ "1280", "1024.png,ro", NULL);
	ensure("1280\\\\1024.png\\ro", "\\",   /* => */ "1280", "1024.pngro");
	ensure("1280\\\\1024.png\\\\ro", "\\", /* => */ "1280", "1024.png", "ro");
	ensure("/file/aa\\,b\\,b\\,b,ro,cdev", ",", /* => */ "/file/aa,b,b,b", "ro", "cdev");

	printf("\nAll succeeded\n");
}



-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2020-09-14 13:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-14 10:05 [PATCH master 1/7] sandbox: hostfile: error out if file couldn't be opened Ahmad Fatoum
2020-09-14 10:05 ` [PATCH master 2/7] sandbox: add_image: support mmaping block devices on 32-bit hosts Ahmad Fatoum
2020-09-14 10:05 ` [PATCH master 3/7] sandbox: support escaping commas in --image filenames Ahmad Fatoum
2020-09-14 13:42   ` Ahmad Fatoum [this message]
2020-09-14 10:05 ` [PATCH master 4/7] readkey: fix buffer overflow handling longer escape sequences Ahmad Fatoum
2020-09-14 10:05 ` [PATCH master 5/7] sandbox: fix SANDBOX_UNWIND dependency to be KASAN only Ahmad Fatoum
2020-09-14 10:05 ` [PATCH master 6/7] fs: don't free device in remove callback Ahmad Fatoum
2020-09-14 10:05 ` [PATCH master 7/7] common: ubsan: replace pr_err with printf Ahmad Fatoum

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

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

  git send-email \
    --in-reply-to=b37d7058-b96a-e790-8d7c-2225c1fe130d@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox