Skip to content

Commit e681bf1

Browse files
committed
plusplus: fix bug in literal string processing
fix NULL pointer access issue when no literal string in module
1 parent a0137fb commit e681bf1

3 files changed

Lines changed: 57 additions & 48 deletions

File tree

bld/plusplus/c/stringl.c

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* Open Watcom Project
44
*
5-
* Copyright (c) 2002-2023 The Open Watcom Contributors. All Rights Reserved.
5+
* Copyright (c) 2002-2026 The Open Watcom Contributors. All Rights Reserved.
66
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
77
*
88
* ========================================================================
@@ -375,26 +375,27 @@ pch_status PCHInitStringPool( bool writing )
375375
STRING_CONSTANT curr;
376376
STRING_CONSTANT *p;
377377

378-
if( ! writing ) {
379-
return( PCHCB_OK );
380-
}
381-
stringTranslateTable = CMemAlloc( stringCount * sizeof( STRING_CONSTANT ) );
382-
p = stringTranslateTable;
383-
for( curr = uniqueStrings; curr != NULL; curr = curr->next ) {
384-
*p = curr;
385-
++p;
386-
}
387-
qsort( stringTranslateTable, stringCount, sizeof( STRING_CONSTANT ), cmpString );
378+
if( writing ) {
379+
if( stringCount > 0 ) {
380+
stringTranslateTable = CMemAlloc( stringCount * sizeof( STRING_CONSTANT ) );
381+
p = stringTranslateTable;
382+
for( curr = uniqueStrings; curr != NULL; curr = curr->next ) {
383+
*p = curr;
384+
++p;
385+
}
386+
qsort( stringTranslateTable, stringCount, sizeof( STRING_CONSTANT ), cmpString );
388387
#ifdef DEVBUILD
389-
{
390-
unsigned i;
391-
for( i = 1; i < stringCount; ++i ) {
392-
if( stringTranslateTable[i -1 ] == stringTranslateTable[i] ) {
393-
CFatal( "two identical strings in translation table" );
388+
{
389+
unsigned i;
390+
for( i = 1; i < stringCount; ++i ) {
391+
if( stringTranslateTable[i -1 ] == stringTranslateTable[i] ) {
392+
CFatal( "two identical strings in translation table" );
393+
}
394+
}
394395
}
396+
#endif
395397
}
396398
}
397-
#endif
398399
return( PCHCB_OK );
399400
}
400401

@@ -416,13 +417,16 @@ pch_status PCHReadStringPool( void )
416417
StringTrash( uniqueStrings );
417418
}
418419
stringCount = PCHReadUInt();
419-
stringTranslateTable = CMemAlloc( stringCount * sizeof( STRING_CONSTANT ) );
420-
p = stringTranslateTable;
421-
for( ; (str_len = PCHReadUInt()) != 0; ) {
422-
str = allocLiteral( str_len, PCHReadUInt() );
423-
PCHRead( str->string, str_len );
424-
stringAdd( str, &uniqueStrings );
425-
*p++ = str;
420+
if( stringCount > 0 ) {
421+
stringTranslateTable = CMemAlloc( stringCount * sizeof( STRING_CONSTANT ) );
422+
p = stringTranslateTable;
423+
for( i = 0; i < stringCount; ++i ) {
424+
str_len = PCHReadUInt();
425+
str = allocLiteral( str_len, PCHReadUInt() );
426+
PCHRead( str->string, str_len );
427+
stringAdd( str, &uniqueStrings );
428+
*p++ = str;
429+
}
426430
}
427431
return( PCHCB_OK );
428432
}
@@ -434,29 +438,30 @@ pch_status PCHWriteStringPool( void )
434438
STRING_CONSTANT *p;
435439

436440
PCHWriteUInt( stringCount );
437-
p = stringTranslateTable;
438-
for( i = 0; i < stringCount; ++i ) {
439-
str = p[i];
440-
PCHWriteUInt( str->len );
441-
PCHWriteUInt( str->flags );
442-
PCHWrite( str->string, str->len );
441+
if( stringCount > 0 ) {
442+
p = stringTranslateTable;
443+
for( i = 0; i < stringCount; ++i ) {
444+
str = p[i];
445+
PCHWriteUInt( str->len );
446+
PCHWriteUInt( str->flags );
447+
PCHWrite( str->string, str->len );
448+
}
443449
}
444-
PCHWriteUInt( 0 );
445450
return( PCHCB_OK );
446451
}
447452

448453
STRING_CONSTANT StringMapIndex( STRING_CONSTANT index )
449454
/*****************************************************/
450455
{
451-
if( PCHGetUInt( index ) < PCH_FIRST_INDEX ) {
452-
return( NULL );
453-
}
456+
if( PCHGetUInt( index ) >= PCH_FIRST_INDEX && stringCount > 0 ) {
454457
#ifdef DEVBUILD
455-
if( PCHGetUInt( index ) >= stringCount + PCH_FIRST_INDEX ) {
456-
CFatal( "invalid string index" );
457-
}
458+
if( PCHGetUInt( index ) >= stringCount + PCH_FIRST_INDEX ) {
459+
CFatal( "invalid string index" );
460+
}
458461
#endif
459-
return( stringTranslateTable[PCHGetUInt( index ) - PCH_FIRST_INDEX] );
462+
return( stringTranslateTable[PCHGetUInt( index ) - PCH_FIRST_INDEX] );
463+
}
464+
return( NULL );
460465
}
461466

462467
static int cmpFindString( const void *kp, const void *tp )
@@ -477,13 +482,13 @@ STRING_CONSTANT StringGetIndex( STRING_CONSTANT str )
477482
{
478483
STRING_CONSTANT *found;
479484

480-
if( str == NULL ) {
481-
return( PCHSetUInt( PCH_NULL_INDEX ) );
482-
}
483-
found = bsearch( &str, stringTranslateTable, stringCount, sizeof( STRING_CONSTANT ), cmpFindString );
484-
if( found == NULL ) {
485-
DbgStmt( CFatal( "invalid string passed to StringGetIndex" ) );
486-
return( PCHSetUInt( PCH_ERROR_INDEX ) );
485+
if( str != NULL && stringCount > 0 ) {
486+
found = bsearch( &str, stringTranslateTable, stringCount, sizeof( STRING_CONSTANT ), cmpFindString );
487+
if( found == NULL ) {
488+
DbgStmt( CFatal( "invalid string passed to StringGetIndex" ) );
489+
return( PCHSetUInt( PCH_ERROR_INDEX ) );
490+
}
491+
return( PCHSetUInt( ( found - stringTranslateTable ) + PCH_FIRST_INDEX ) );
487492
}
488-
return( PCHSetUInt( ( found - stringTranslateTable ) + PCH_FIRST_INDEX ) );
493+
return( PCHSetUInt( PCH_NULL_INDEX ) );
489494
}

bld/plusplus/h/pcheader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* Open Watcom Project
44
*
5-
* Copyright (c) 2002-2023 The Open Watcom Contributors. All Rights Reserved.
5+
* Copyright (c) 2002-2026 The Open Watcom Contributors. All Rights Reserved.
66
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
77
*
88
* ========================================================================
@@ -57,7 +57,7 @@ enum {
5757
#endif
5858

5959
#define PHH_MAJOR 0x03
60-
#define PHH_MINOR 0x34
60+
#define PHH_MINOR 0x35
6161

6262
#define TEXT_HEADER_SIZE 40
6363
#ifdef __UNIX__

bld/plusplus/h/pcregdef.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*
33
* Open Watcom Project
44
*
5+
* Copyright (c) 2026 The Open Watcom Contributors. All Rights Reserved.
56
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
67
*
78
* ========================================================================
@@ -64,6 +65,9 @@
6465
// - must be before CGIO because ->open_ins in BLK_INITs in LABEL
6566
// must be zapped when CGIO processes ICs (we don't want the CGIO
6667
// zaps to be ruined by reading in BLK_INITs)
68+
// - STRINGL
69+
// - must be before PTREE and TYPE because they can use literal
70+
// string for based pointers
6771
//
6872
PCH_EXEC( CMACADD, Macros )
6973
PCH_EXEC( NAME, Names )

0 commit comments

Comments
 (0)