All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/7] add random_pathstring()
@ 2015-03-16 20:25 tyson.w.smith
  2015-03-17 14:26 ` Dave Jones
  0 siblings, 1 reply; 2+ messages in thread
From: tyson.w.smith @ 2015-03-16 20:25 UTC (permalink / raw)
  To: davej; +Cc: trinity, tysmith, Tyson Smith

From: Tyson Smith <tyson.w.smith@gmail.com>

This will generate a path that will likely not exist but may
look somewhat valid or totally crazy depending on rand.
---
 random-pathname.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/random-pathname.c b/random-pathname.c
index 4d093d1..d8f5094 100644
--- a/random-pathname.c
+++ b/random-pathname.c
@@ -9,6 +9,52 @@
 
 #define MAX_PATH_LEN 4096
 
+/*
+ * Generate a path that will likely not exist but may look
+ * somewhat valid or totally crazy depending on rand.
+ */
+static void random_pathstring(char *path, unsigned int len) {
+	const char *fmts = "dns";
+	unsigned int i;
+
+	if (len < 1)
+		return;
+
+	switch(rand() % 5) {
+	case 0:
+		// single repeating random ASCII character
+		(void) memset(path, (rand() % 95) + 32, rand() % len);
+		path[len-1] = '\0';
+		break;
+	case 1:
+		// random ASCII characters 32(space) -> 126(~)
+		for (i=0; i < len; i++)
+			path[i] = (char) ((rand() % 95) + 32);
+		path[len-1] = '\0';
+		break;
+	case 2:
+		// random . or /
+		for (i=0; i < len; i++)
+			path[i] = RAND_BOOL() ? '.' : '/';
+		path[len-1] = '\0';
+		break;
+	case 3:
+		// format strings
+		for (i=0; i < (len - 2); i+=2) {
+			path[i] = '%';
+			path[i+1] = fmts[rand() % 3];
+		}
+		path[len-1] = '\0';
+		break;
+	case 4:
+		// junk
+		for (i=0; i < len; i++)
+			path[i] = (char) RAND_BYTE();
+		path[len-1] = '\0';
+		break;
+	}
+}
+
 const char * generate_pathname(void)
 {
 	const char *pathname = get_filename();
@@ -25,6 +71,13 @@ const char * generate_pathname(void)
 	/* Create a bogus filename. */
 	newpath = zmalloc(MAX_PATH_LEN);	// FIXME: We leak this.
 
+	/* 50/50 chance using a random path string */
+	if (RAND_BOOL()) {
+		len = RAND_BOOL() ? RAND_BYTE() : rand() % MAX_PATH_LEN;
+		random_pathstring(newpath, len);
+		return newpath;
+	}
+
 	len = strlen(pathname);
 
 	if (RAND_BOOL())
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/7] add random_pathstring()
  2015-03-16 20:25 [PATCH 1/7] add random_pathstring() tyson.w.smith
@ 2015-03-17 14:26 ` Dave Jones
  0 siblings, 0 replies; 2+ messages in thread
From: Dave Jones @ 2015-03-17 14:26 UTC (permalink / raw)
  To: tyson.w.smith; +Cc: trinity, tysmith

On Mon, Mar 16, 2015 at 01:25:21PM -0700, tyson.w.smith@gmail.com wrote:
 > From: Tyson Smith <tyson.w.smith@gmail.com>
 > 
 > This will generate a path that will likely not exist but may
 > look somewhat valid or totally crazy depending on rand.

We kinda tried this before. It stressed the negative dentry code
a little (perhaps a little too much tbh), but never really found
anything interesting, and just caused trinity to crap garbage
filenames all over the tmp dir.

Not sure it's worth trying this again. If we do, we should
make it a lot less likely to happen, like 1 in a 100 or something.


 > +	switch(rand() % 5) {

Someone recently added a handy ONE_IN macro :)

 > +	case 0:
 > +		// single repeating random ASCII character
 > +		(void) memset(path, (rand() % 95) + 32, rand() % len);
 > +		path[len-1] = '\0';
 > +		break;
 > +	case 1:
 > +		// random ASCII characters 32(space) -> 126(~)
 > +		for (i=0; i < len; i++)
 > +			path[i] = (char) ((rand() % 95) + 32);
 > +		path[len-1] = '\0';
 > +		break;
 > +	case 2:
 > +		// random . or /
 > +		for (i=0; i < len; i++)
 > +			path[i] = RAND_BOOL() ? '.' : '/';
 > +		path[len-1] = '\0';
 > +		break;
 > +	case 3:
 > +		// format strings
 > +		for (i=0; i < (len - 2); i+=2) {
 > +			path[i] = '%';
 > +			path[i+1] = fmts[rand() % 3];
 > +		}
 > +		path[len-1] = '\0';
 > +		break;
 > +	case 4:
 > +		// junk
 > +		for (i=0; i < len; i++)
 > +			path[i] = (char) RAND_BYTE();
 > +		path[len-1] = '\0';
 > +		break;
 > +	}

This should probably just be a generate_rand_bytes() call,
with enhancements to that function if necessary.

	Dave

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-03-17 14:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-16 20:25 [PATCH 1/7] add random_pathstring() tyson.w.smith
2015-03-17 14:26 ` Dave Jones

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.