Skip to content

Commit aa269bb

Browse files
committed
fuzzing: New data fuzzer with HRI check
1 parent 9812340 commit aa269bb

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

src/c-lib/Makefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@ TEST_OBJ = $(BUILD_DIR)/$(TEST_SRC:.c=.o)
174174
LINTER_TEST_SRC = syntax/gs1syntaxdictionary-test.c
175175

176176
FUZZER_PREFIX = $(NAME)-fuzzer-
177-
FUZZER_SRCS = gs1encoders-fuzzer-ais.c gs1encoders-fuzzer-dl.c gs1encoders-fuzzer-scandata.c gs1encoders-fuzzer-syn.c
177+
FUZZER_SRCS = gs1encoders-fuzzer-ais.c gs1encoders-fuzzer-data.c gs1encoders-fuzzer-dl.c gs1encoders-fuzzer-scandata.c gs1encoders-fuzzer-syn.c
178178
FUZZER_OBJS = $(addprefix $(BUILD_DIR)/, $(FUZZER_SRCS:.c=.o))
179-
FUZZER_BINS = $(BUILD_DIR)/$(FUZZER_PREFIX)ais $(BUILD_DIR)/$(FUZZER_PREFIX)dl $(BUILD_DIR)/$(FUZZER_PREFIX)scandata $(BUILD_DIR)/$(FUZZER_PREFIX)syn
179+
FUZZER_BINS = $(BUILD_DIR)/$(FUZZER_PREFIX)ais $(BUILD_DIR)/$(FUZZER_PREFIX)data $(BUILD_DIR)/$(FUZZER_PREFIX)dl $(BUILD_DIR)/$(FUZZER_PREFIX)scandata $(BUILD_DIR)/$(FUZZER_PREFIX)syn
180180

181181
FUZZER_CORPUS_PREFIX = corpus-
182-
FUZZER_CORPUSES = $(FUZZER_CORPUS_PREFIX)ais/ $(FUZZER_CORPUS_PREFIX)dl/ $(FUZZER_CORPUS_PREFIX)scandata/ $(FUZZER_CORPUS_PREFIX)syn/
182+
FUZZER_CORPUSES = $(FUZZER_CORPUS_PREFIX)ais/ $(FUZZER_CORPUS_PREFIX)data/ $(FUZZER_CORPUS_PREFIX)dl/ $(FUZZER_CORPUS_PREFIX)scandata/ $(FUZZER_CORPUS_PREFIX)syn/
183183

184184
ALL_SRCS = $(wildcard *.c) $(wildcard syntax/*.c)
185185
SRCS = $(filter-out $(APP_SRC) $(TEST_SRC) $(LINTER_TEST_SRC) $(FUZZER_SRCS), $(ALL_SRCS))
@@ -278,6 +278,11 @@ $(FUZZER_CORPUS_PREFIX)ais/:
278278
$(BUILD_DIR)/$(FUZZER_PREFIX)ais: $(OBJS) $(BUILD_DIR)/$(FUZZER_PREFIX)ais.o
279279
$(CC) $(CFLAGS) $(OBJS) $(BUILD_DIR)/$(FUZZER_PREFIX)ais.o -o $(BUILD_DIR)/$(FUZZER_PREFIX)ais
280280

281+
$(FUZZER_CORPUS_PREFIX)data/:
282+
mkdir -p $@
283+
284+
$(BUILD_DIR)/$(FUZZER_PREFIX)data: $(OBJS) $(BUILD_DIR)/$(FUZZER_PREFIX)data.o
285+
$(CC) $(CFLAGS) $(OBJS) $(BUILD_DIR)/$(FUZZER_PREFIX)data.o -o $(BUILD_DIR)/$(FUZZER_PREFIX)data
281286

282287
$(FUZZER_CORPUS_PREFIX)dl/:
283288
mkdir -p $@

src/c-lib/gs1encoders-fuzzer-data.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* GS1 Syntax Engine
3+
*
4+
* @author Copyright (c) 2021-2024 GS1 AISBL.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
*
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <stdbool.h>
22+
#include <stdint.h>
23+
#include <stdlib.h>
24+
#include <stdio.h>
25+
#include <string.h>
26+
27+
#include "gs1encoders.h"
28+
#include "enc-private.h"
29+
30+
static gs1_encoder *ctx = NULL;
31+
32+
33+
int LLVMFuzzerInitialize(int *argc, char ***argv) {
34+
35+
(void)argc;
36+
(void)argv;
37+
38+
ctx = gs1_encoder_init(NULL);
39+
gs1_encoder_setPermitUnknownAIs(ctx, true);
40+
41+
return 0;
42+
43+
}
44+
45+
46+
int LLVMFuzzerTestOneInput(const uint8_t* const buf, size_t len) {
47+
48+
char in[MAX_DATA+1];
49+
char pristine[MAX_DATA+1];
50+
const char *out;
51+
char **hri;
52+
53+
if (len > MAX_DATA)
54+
return 0;
55+
56+
memcpy(in, buf, len);
57+
in[len] = '\0';
58+
59+
memcpy(pristine, in, len);
60+
61+
if (!gs1_encoder_setDataStr(ctx, in))
62+
return 0;
63+
64+
// Test that the input hasn't been corrupted
65+
if (memcmp(in, pristine, len) != 0) {
66+
printf("\n:IN %s\nPRISTINE: %s\n", in, pristine);
67+
abort();
68+
};
69+
70+
// Validate the round trip
71+
out = gs1_encoder_getDataStr(ctx);
72+
if (strcmp(in, out) != 0) {
73+
printf("\nIN: %s\nOUT: %s\n", in, out);
74+
abort();
75+
}
76+
77+
gs1_encoder_getHRI(ctx, &hri);
78+
79+
return 0;
80+
81+
}

0 commit comments

Comments
 (0)