Skip to content

Commit 93f51f4

Browse files
committed
Recognise bad input data
1 parent a199bbc commit 93f51f4

File tree

6 files changed

+71
-24
lines changed

6 files changed

+71
-24
lines changed

CHANGES.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11

22
History (newest at top)
33
=======================
4+
5+
Sep 2023 (v5.4.4)
6+
* Try to recognise some input file corruption and cleanly exit
7+
48
Jun 2023 (v5.4.3)
59
* Update for MQ V9.3.3
6-
* Many more QQST fields
10+
* Many more QQST fields
711
* New "-f jsoncompact" option for single-line JSON records
812
* Add Task_Index field to channel structures, to easier tie up with MP1B output
9-
* Correct a couple of column names in WQ structure
13+
* Correct a couple of column names in WQ structure
1014

1115
Feb 2023 (v5.4.2)
1216
* Update for MQ V9.3.2

src/M

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ fi
3333

3434
export PLATFLAGS=$flags CC=$cc VERS=$VERS
3535
make -e -f Makefile.unix $*
36+
rc=$?
37+
if [ $rc -ne 0 ]
38+
then
39+
exit $rc
40+
fi
3641

3742
rm -f $targdir/convH $targdir/mqsmfcsv
3843
cp convH mqsmfcsv $targdir

src/Makefile.unix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CC=cc
33
CFLAGS= -I. -O2
44
PLATFLAGS=
55
SRC = \
6+
mqsmf.c \
67
smfDDL.c \
78
smfPrint.c \
89
smfDate.c \
@@ -39,8 +40,7 @@ SRC = \
3940
t123/printDataV2.c \
4041
t123/printServerV1.c \
4142
t123/printServerV2.c \
42-
t180/printAMS.c \
43-
mqsmf.c
43+
t180/printAMS.c
4444

4545
HDR = mqsmfstrucU.h \
4646
t123/smf123.h \
@@ -62,7 +62,7 @@ shipTest: mqsmfcsv
6262
mqsmfcsv: $(SRC) $(HDR) Makefile.unix dummy
6363
$(CC) $(PLATFLAGS) -o $@ $(SRC) $(CFLAGS) -DCSQDSMF_VERSION=$(VERS)
6464

65-
mqsmfstrucU.h: convH dummy # csqdsmfc-$(VERS).h
65+
mqsmfstrucU.h: convH dummy # csqdsmfc-$(VERS).h
6666
(test -s csqdsmfc-$(VERS).h && convH $(VERS) < csqdsmfc-$(VERS).h > $@) || exit 0
6767

6868
convH: convH64.c dummy

src/mqsmf.c

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ static myoff_t pos;
161161
static int offsetCorrection = 0;
162162

163163
static BOOL warn115_240=FALSE;
164+
static BOOL corruptData = FALSE;
164165

165166
static time_t startTime = 0;
166167
static time_t endTime = 0;
@@ -243,6 +244,7 @@ int main( int argc, char *argv[] )
243244

244245
int sectionCount;
245246
int recordSubTypeVersion;
247+
int triplet1Offset;
246248

247249
infoStream=stdout;
248250

@@ -697,7 +699,18 @@ int main( int argc, char *argv[] )
697699
/*******************************************************************/
698700
offsetBlockStart = &pSMFMQRecord->s[0];
699701
offsetBlockType = BT_TRIPLET;
700-
p = &dataBuf[conv32(pSMFMQRecord->s[0].offset)];
702+
triplet1Offset = conv32(pSMFMQRecord->s[0].offset);
703+
p = &dataBuf[triplet1Offset];
704+
705+
/*
706+
debugf(2,"Current file offset: 0x%08X\n",currentOffset);
707+
printDEBUG("INPUT BUF",dataBuf,offset);
708+
debugf(2,"\n p=%p bytesread=%d offset=%d\n",p,bytesRead+4,offset);
709+
debugf(2," trip[0] tripOffset=%d [0x%08X] len=%d count=%d\n", triplet1Offset,triplet1Offset,
710+
conv16(pSMFMQRecord->s[0].l),
711+
conv16(pSMFMQRecord->s[0].n));
712+
*/
713+
701714
if (conv16(pSMFMQRecord->s[0].l) == 4) /* There is no QWHS */
702715
{
703716
sectionCount = 2; /* 1 extra triplet beyond the QWHS location */
@@ -708,7 +721,24 @@ int main( int argc, char *argv[] )
708721
else
709722
{
710723
char *dt[2];
724+
int qwshlen;
725+
711726
pqwhs = (qwhs *)p;
727+
qwshlen = conv16(pqwhs->qwhslen);
728+
if (debugLevel >= 2) {
729+
printDEBUG("QWHS",p,(qwshlen!=0)?qwshlen:0x30);
730+
}
731+
if (qwshlen <= 0)
732+
{
733+
FILE *dfp = printDEBUGStream();
734+
fprintf(stderr,"Error: Data at file offset 0x%08X appears corrupt. Exiting the formatter.\n",currentOffset);
735+
if (dfp) {
736+
fprintf(dfp, "Error: Data at file offset 0x%08X appears corrupt. Exiting the formatter.\n",currentOffset);
737+
}
738+
corruptData = TRUE;
739+
continue;
740+
}
741+
712742
sectionCount = pqwhs->qwhsnsda[0];
713743
convDate(pqwhs->qwhsstck,dt);
714744
strcpy(commonF.stckFormatDate,dt[0]);
@@ -736,7 +766,7 @@ int main( int argc, char *argv[] )
736766
}
737767
break;
738768

739-
case SMFTYPE_ZCEE:
769+
case SMFTYPE_ZCEE:
740770
recordSubTypeVersion = conv32(pSMFMQRecord->Header.u.v);
741771

742772
switch (recordSubTypeVersion)
@@ -773,8 +803,9 @@ int main( int argc, char *argv[] )
773803
}
774804
}
775805

776-
if (pqwhs != NULL)
777-
debugf(3,"Section count %d for %4.4s, qwhslen=%d\n",sectionCount,commonF.qMgr,conv16(pqwhs->qwhslen));
806+
if (pqwhs != NULL) {
807+
debugf(3,"Section count %d for %4.4s, qwhslen=%d\n",sectionCount,commonF.qMgr,conv16(pqwhs->qwhslen));
808+
}
778809

779810
/*********************************************************************/
780811
/* Once we know how many sections there are, copy the triplet values */
@@ -786,8 +817,9 @@ int main( int argc, char *argv[] )
786817
/*********************************************************************/
787818
recLength = getOffsets(sectionCount,offsetBlockStart, offsetBlockType, recordType,subTypesValid, currentOffset);
788819
currentOffset += recLength;
789-
if (debugLevel >= 2)
820+
if (debugLevel >= 2) {
790821
printDEBUG("FULL RECORD",dataBuf + offsetCorrection,recLength);
822+
}
791823

792824

793825
/*********************************************************************/
@@ -1185,7 +1217,7 @@ int main( int argc, char *argv[] )
11851217
exit(0);
11861218
}
11871219
}
1188-
} while (0 != bytesRead && totalRecords < maxRecords);
1220+
} while (0 != bytesRead && totalRecords < maxRecords && !corruptData);
11891221

11901222
/***********************************************************************/
11911223
/* Cleanup and exit. If we get here normally, then the checkpoint */
@@ -1740,26 +1772,20 @@ int getOffsets(int sectionCount,void *offsetBlockStart, int offsetBlockType, int
17401772
{
17411773
if (recordType == amsType)
17421774
{
1743-
fprintf(infoStream, "Highest doublet = %d RecLength = %d New Offset = %lld\n",
1775+
debugf(3, "Highest doublet = %d RecLength = %d New Offset = %lld\n",
17441776
h, recLength, currentOffset);
1745-
if (debugLevel >=4)
1777+
for (i=0;i<sectionCount;i++)
17461778
{
1747-
for (i=0;i<sectionCount;i++)
1748-
{
1749-
fprintf(infoStream,"Doublet %d - offset=%d len=%d \n",i,doublet[i].offset,doublet[i].l);
1750-
}
1779+
debugf(4,"Doublet %d - offset=%d len=%d \n",i,doublet[i].offset,doublet[i].l);
17511780
}
17521781
}
17531782
else
17541783
{
1755-
fprintf(infoStream, "Highest triple = %d RecLength = %d New Offset = %lld\n",
1784+
debugf(3, "Highest triple = %d RecLength = %d New Offset = %lld\n",
17561785
h, recLength, currentOffset);
1757-
if (debugLevel >=4)
1786+
for (i=0;i<sectionCount;i++)
17581787
{
1759-
for (i=0;i<sectionCount;i++)
1760-
{
1761-
fprintf(infoStream,"Triplet %d - offset=%d len=%d count=%d\n",i,triplet[i].offset,triplet[i].l,triplet[i].n);
1762-
}
1788+
debugf(4,"Triplet %d - offset=%d len=%d count=%d\n",i,triplet[i].offset,triplet[i].l,triplet[i].n);
17631789
}
17641790
}
17651791
}

src/mqsmf.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ typedef struct columnHeader_s {
180180
/* Formatting functions - one for each element type */
181181
/********************************************************************/
182182
extern void printDEBUG (char *title, void *,int);
183+
extern FILE *printDEBUGStream();
183184
extern void printQ5ST (q5st *);
184185
extern void printQCCT (qcct *);
185186
extern void printQCST (qcst *);
@@ -260,7 +261,13 @@ extern FILE * fopenext(const char *, const char *, BOOL *);
260261

261262
#if !defined(debugf)
262263
#define debugf(_level,_fmt,...) \
263-
{if (debugLevel >= _level) fprintf(infoStream,_fmt,__VA_ARGS__);}
264+
{if (debugLevel >= _level) { \
265+
FILE *dfp = printDEBUGStream(); \
266+
if (dfp) { \
267+
fprintf(dfp,_fmt,__VA_ARGS__); \
268+
} \
269+
fprintf(infoStream,_fmt,__VA_ARGS__);} \
270+
}
264271
#endif
265272
/*
266273
* Convert some MQI values into the corresponding string

src/printDEBUG.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,8 @@ void printDEBUG(char *title, void *buf,int length)
7272
}
7373
return;
7474
}
75+
76+
FILE *printDEBUGStream()
77+
{
78+
return fp;
79+
}

0 commit comments

Comments
 (0)