Skip to content

Commit

Permalink
Replace magic numbers with {MIN,MAX}_AI_LEN
Browse files Browse the repository at this point in the history
  • Loading branch information
terryburton committed Dec 7, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 7b129d0 commit 7485b36
Showing 4 changed files with 20 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/c-lib/ai.c
Original file line number Diff line number Diff line change
@@ -249,11 +249,11 @@ const struct aiEntry* gs1_lookupAIentry(gs1_encoder* const ctx, const char *p, s

assert(ailen <= strlen(p));

if (ailen == 1 || ailen > 4) // AI length between 2 and 4, even for unknown AIs
if (ailen != 0 && (ailen < MIN_AI_LEN || ailen > MAX_AI_LEN)) // Even for unknown AIs
return NULL;

// Don't attempt to find a non-digit AI
if (!gs1_allDigits((uint8_t *)p, ailen != 0 ? ailen : 2))
if (!gs1_allDigits((uint8_t *)p, ailen != 0 ? ailen : MIN_AI_LEN))
return NULL;

/*
@@ -736,7 +736,7 @@ static bool validateAImutex(gs1_encoder* const ctx) {

for (token = strtok_r((char*)(token+3), ",", &saveptr2); token; token = strtok_r(NULL, ",", &saveptr2)) {

char matchedAI[5] = { 0 };
char matchedAI[MAX_AI_LEN+1] = { 0 };

if (aiExists(ctx, token, ai->ai, matchedAI)) {
snprintf(ctx->errMsg, sizeof(ctx->errMsg), "It is invalid to pair AI (%.*s) with AI (%s)", ai->ailen, ai->ai, matchedAI);
4 changes: 3 additions & 1 deletion src/c-lib/ai.h
Original file line number Diff line number Diff line change
@@ -28,6 +28,8 @@
#include "syntax/gs1syntaxdictionary.h"

#define MAX_AIS 64
#define MIN_AI_LEN 2
#define MAX_AI_LEN 4
#define MAX_AI_VALUE_LEN 90
#define MAX_AI_ATTR_LEN 64

@@ -63,7 +65,7 @@ struct aiComponent {


struct aiEntry {
char ai[5];
char ai[MAX_AI_LEN+1];
bool fnc1;
struct aiComponent parts[MAX_PARTS];
char* attrs;
24 changes: 12 additions & 12 deletions src/c-lib/dl.c
Original file line number Diff line number Diff line change
@@ -234,9 +234,9 @@ void gs1_freeDLkeyQualifiers(gs1_encoder* const ctx) {
* the position in the list or -1 if missing
*
*/
static int getDLpathAIseqEntry(gs1_encoder* const ctx, const char seq[MAX_AIS][5], const int len) {
static int getDLpathAIseqEntry(gs1_encoder* const ctx, const char seq[MAX_AIS][MAX_AI_LEN+1], const int len) {

char aiseq[5 * MAX_AIS] = { 0 };
char aiseq[(MAX_AI_LEN+1) * MAX_AIS] = { 0 };
char *p = aiseq;
int i;
size_t s = 0;
@@ -270,14 +270,14 @@ static int getDLpathAIseqEntry(gs1_encoder* const ctx, const char seq[MAX_AIS][5

}

static inline bool isValidDLpathAIseq(gs1_encoder* const ctx, const char seq[MAX_AIS][5], const int len) {
static inline bool isValidDLpathAIseq(gs1_encoder* const ctx, const char seq[MAX_AIS][MAX_AI_LEN+1], const int len) {
return getDLpathAIseqEntry(ctx, seq, len) != -1;
}

static inline bool isDLpkey(gs1_encoder* const ctx, const char* const p) {
char seq[MAX_AIS][5] = { 0 };
char seq[MAX_AIS][MAX_AI_LEN+1] = { 0 };
strcpy(seq[0], p);
return getDLpathAIseqEntry(ctx, (const char(*)[5])seq, 1) != -1;
return getDLpathAIseqEntry(ctx, (const char(*)[MAX_AI_LEN+1])seq, 1) != -1;
}


@@ -349,7 +349,7 @@ bool gs1_parseDLuri(gs1_encoder* const ctx, char* const dlData, char* const data
const char* dp = NULL; // DL path info
bool ret;
bool fnc1req = true;
char pathAIseq[MAX_AIS][5] = { 0 }; // Sequence of AIs extracted from the path info
char pathAIseq[MAX_AIS][MAX_AI_LEN+1] = { 0 }; // Sequence of AIs extracted from the path info
int numPathAIs, i;

assert(ctx);
@@ -638,7 +638,7 @@ bool gs1_parseDLuri(gs1_encoder* const ctx, char* const dlData, char* const data

// Validate that the AI sequence in the path info is a valid
// key-qualifier association
if (!isValidDLpathAIseq(ctx, (const char(*)[5])pathAIseq, numPathAIs)) {
if (!isValidDLpathAIseq(ctx, (const char(*)[MAX_AI_LEN+1])pathAIseq, numPathAIs)) {
strcpy(ctx->errMsg, "The AIs in the path are not a valid key-qualifier sequence for the key");
ctx->errFlag = true;
ret = false;
@@ -650,7 +650,7 @@ bool gs1_parseDLuri(gs1_encoder* const ctx, char* const dlData, char* const data
if (numPathAIs < MAX_AIS) {
for (i = 0; i < ctx->numAIs; i++) {

char seq[MAX_AIS][5] = { 0 };
char seq[MAX_AIS][MAX_AI_LEN+1] = { 0 };
const struct aiValue* const ai = &ctx->aiData[i];
int j;

@@ -668,7 +668,7 @@ bool gs1_parseDLuri(gs1_encoder* const ctx, char* const dlData, char* const data
strcpy(seq[j], ai->aiEntry->ai);
memcpy(&seq[j+1], &pathAIseq[j], (size_t)(numPathAIs-j) * sizeof(seq[0]));

if (getDLpathAIseqEntry(ctx, (const char(*)[5])seq, numPathAIs + 1) != -1) {
if (getDLpathAIseqEntry(ctx, (const char(*)[MAX_AI_LEN+1])seq, numPathAIs + 1) != -1) {
snprintf(ctx->errMsg, sizeof(ctx->errMsg), "AI (%s) from query params should be in the path info", seq[j]);
ctx->errFlag = true;
ret = false;
@@ -743,7 +743,7 @@ char* gs1_generateDLuri(gs1_encoder* const ctx, const char* const stem) {
*/
for (i = 0; i < ctx->numAIs; i++) {

char seq[MAX_AIS][5] = { 0 };
char seq[MAX_AIS][MAX_AI_LEN+1] = { 0 };
int ke;
const struct aiValue* const ai = &ctx->aiData[i];

@@ -753,7 +753,7 @@ char* gs1_generateDLuri(gs1_encoder* const ctx, const char* const stem) {
assert(ai->aiEntry);

strcpy(seq[0], ai->aiEntry->ai);
if ((ke = getDLpathAIseqEntry(ctx, (const char(*)[5])seq, 1)) != -1) {
if ((ke = getDLpathAIseqEntry(ctx, (const char(*)[MAX_AI_LEN+1])seq, 1)) != -1) {
keyEntry = ke;
key = ctx->dlKeyQualifiers[keyEntry];
break;
@@ -1368,7 +1368,7 @@ void test_dl_URIescape(void) {

void test_dl_testValidateDLpathAIseq(void) {

const char seq[][MAX_AIS][5] = {
const char seq[][MAX_AIS][MAX_AI_LEN+1] = {

// SSCC
{ "00" },
4 changes: 2 additions & 2 deletions src/c-lib/syn.c
Original file line number Diff line number Diff line change
@@ -163,7 +163,7 @@ int parseSyntaxDictionaryEntry(gs1_encoder* const ctx, const char* const line, c
len = strlen(token);
if ((p = strchr(token, '-')) != NULL) {

if (len < 5 || len > 9)
if (len < MIN_AI_LEN*2+1 || len > MAX_AI_LEN*2+1)
error("AI range has wrong width");

if ((len%2 != 1) || ((size_t)(p - token) != len/2))
@@ -187,7 +187,7 @@ int parseSyntaxDictionaryEntry(gs1_encoder* const ctx, const char* const line, c

} else {

if (len < 2 || len > 4)
if (len < MIN_AI_LEN || len > MAX_AI_LEN)
error("AI has wrong width");

if (strspn(token, "0123456789") != len)

0 comments on commit 7485b36

Please sign in to comment.