Skip to content

Commit 7ba43c0

Browse files
author
434b
committed
First approach to for a toggable printableStringConsumer
1 parent 50c6993 commit 7ba43c0

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

packages/core/FuzzedDataProvider.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,8 @@ describe("FuzzedDataProvider checks", () => {
946946
expect(strings).toContain("t ame");
947947
});
948948
it("verifyPrintableString", () => {
949-
const data = new FuzzedDataProvider(Buffer.from(Data), true);
950-
const consumedStrAsArr = [...data.consumeString(1024)];
949+
const data = new FuzzedDataProvider(Buffer.from(Data));
950+
const consumedStrAsArr = [...data.consumeString(1024, "ascii", true)];
951951
consumedStrAsArr.forEach((c) => {
952952
const charAsNum = c.charCodeAt(0);
953953
expect(charAsNum >= 32 && charAsNum <= 126).toBeTruthy();

packages/core/FuzzedDataProvider.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
export class FuzzedDataProvider {
2525
private readonly data: Buffer;
2626
private dataPtr = -1;
27-
private readonly enforcePrintable: boolean = false;
2827
/** The number of remaining bytes that can be consumed from the fuzzer input data. */
2928
_remainingBytes = 0;
3029

@@ -35,12 +34,9 @@ export class FuzzedDataProvider {
3534

3635
/**
3736
* @param data - a buffer containing the fuzzer input
38-
* @param printable - a boolean, which defaults to false that indicates whether consumed strings
39-
* should be forced to contain only valid printable characters
4037
*/
41-
constructor(data: Buffer, enforcePrintable: boolean | undefined = false) {
38+
constructor(data: Buffer) {
4239
this.data = data;
43-
this.enforcePrintable = enforcePrintable;
4440
if (data.length > 0) {
4541
this.dataPtr = 0;
4642
this._remainingBytes = data.length;
@@ -376,17 +372,20 @@ export class FuzzedDataProvider {
376372
* is not sufficiently long.
377373
* @param maxLength the maximum length of the string
378374
* @param encoding the encoding of the string
375+
* @param printable - a boolean, which defaults to false that indicates whether consumed strings
376+
* should be forced to contain only valid printable characters
379377
* @returns a `string` of length between 0 and `maxLength` (inclusive)
380378
*/
381379
consumeString(
382380
maxLength: number,
383-
encoding: BufferEncoding | undefined = "ascii"
381+
encoding: BufferEncoding | undefined = "ascii",
382+
printable: boolean | undefined = false
384383
): string {
385384
if (maxLength < 0) throw new Error("maxLength must be non-negative");
386385
let result;
387386
const arrayLength = Math.min(maxLength, this._remainingBytes);
388387

389-
if (this.enforcePrintable) {
388+
if (printable) {
390389
result = this.bufToPrintableString(
391390
this.data,
392391
this.dataPtr,
@@ -438,32 +437,35 @@ export class FuzzedDataProvider {
438437
* high enough. Limitations are that there are collisions such that, e.g.:
439438
* the conversion of values: `0x1`, `0x42`, and `0xa2` all yield the character `B`.
440439
* @param buf - Buffer that contains arbitrary values
441-
* @param lb - lower bound at which processing of the provided `Buffer` shall begin
442-
* @param ub - upper bound, analogous to the lower bound
440+
* @param min - lower bound at which processing of the provided `Buffer` shall begin
441+
* @param max - upper bound, analogous to the lower bound
443442
* @param encoding - a valid `BufferEncoding`.
444443
* @returns a string that was sanitized and only contains printable characters
445444
*/
446445
bufToPrintableString(
447446
buf: Buffer,
448-
lb: number,
449-
ub: number,
447+
min: number,
448+
max: number,
450449
encoding: BufferEncoding
451450
): string {
452-
for (let i = lb; i < ub; i++) {
451+
for (let i = min; i < max; i++) {
453452
buf[i] = this.toAscii(buf[i]);
454453
}
455-
return buf.subarray(lb, ub).toString(encoding);
454+
return buf.subarray(min, max).toString(encoding);
456455
}
457456

458457
/**
459458
* Consumes the remaining bytes of the fuzzer input as a string.
460459
* @param encoding - the encoding of the string
460+
* @param printable - a boolean, which defaults to false that indicates whether consumed strings
461+
* should be forced to contain only valid printable characters
461462
* @returns a string constructed from the remaining bytes of the fuzzer input using the given encoding
462463
*/
463464
consumeRemainingAsString(
464-
encoding: BufferEncoding | undefined = "ascii"
465+
encoding: BufferEncoding | undefined = "ascii",
466+
printable: boolean | undefined = false
465467
): string {
466-
return this.consumeString(this._remainingBytes, encoding);
468+
return this.consumeString(this._remainingBytes, encoding, printable);
467469
}
468470

469471
/**
@@ -473,16 +475,19 @@ export class FuzzedDataProvider {
473475
* @param maxArrayLength the maximum length of the array
474476
* @param maxStringLength the maximum length of the strings
475477
* @param encoding the encoding of the strings
478+
* @param printable - a boolean, which defaults to false that indicates whether consumed strings
479+
* should be forced to contain only valid printable characters
476480
* @returns an array containing strings constructed from the remaining bytes of the fuzzer input using the given encoding
477481
*/
478482
consumeStringArray(
479483
maxArrayLength: number,
480484
maxStringLength: number,
481-
encoding: BufferEncoding | undefined = "ascii"
485+
encoding: BufferEncoding | undefined = "ascii",
486+
printable: boolean | undefined = false
482487
) {
483488
const strs = [];
484489
while (strs.length < maxArrayLength && this.remainingBytes > 0) {
485-
const str = this.consumeString(maxStringLength, encoding);
490+
const str = this.consumeString(maxStringLength, encoding, printable);
486491
if (str) {
487492
strs.push(str);
488493
}

0 commit comments

Comments
 (0)