Changeset 5ad2c6c7 for libcfa/src


Ignore:
Timestamp:
Aug 24, 2023, 11:37:16 AM (2 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
88001dd
Parents:
d0cfcbe1
Message:

update reading C strings with size check

Location:
libcfa/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    rd0cfcbe1 r5ad2c6c7  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug 14 23:07:20 2023
    13 // Update Count     : 1466
     12// Last Modified On : Thu Aug 24 10:25:20 2023
     13// Update Count     : 1516
    1414//
    1515
     
    970970                } // if
    971971
     972                int check = f.wd - 1;
     973
    972974                // getline
    973975                if ( f.flags.delimit ) {
    974                         enum { size = 16 };
     976                        enum { size = 32 };
    975977                        char fmtstr[size];
    976978                        snprintf( fmtstr, size, "%%%d[^%c]s", f.wd, f.delimit );
    977                         f.s[f.wd - 1] = '\0';                                           // insert sentinel
     979                        if ( ! f.flags.rwd ) f.s[check] = '\0';         // insert sentinel
    978980                        int len = fmt( is, fmtstr, f.s );                       // read upto delimiter
     981                        if ( ! f.flags.rwd && f.s[check] != '\0' )      // sentinel overwritten ?
     982                                throw (cstring_length){ &cstring_length_vt };
    979983                        if ( len == 0 ) f.s[0] = '\0';                          // empty read => argument unchanged => set empty
    980                         if ( f.s[f.wd - 1] != '\0' )                            // sentinel overwritten ?
    981                                 throw (cstring_length){ &cstring_length_vt };
     984//                      fprintf( stderr, "getline %s %s %d %d %d '%c'\n", fmtstr, f.s, len, f.wd, check, f.s[check] );
    982985                        if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
    983986                        return is;
     
    10071010                } // if
    10081011
    1009                 int check = f.wd - 1;
    1010                 if ( f.flags.rwd ) check += 1;                                  // provide place for sentinel
    1011                 f.s[check] = '\0';                                                              // insert sentinel
     1012                if ( ! f.flags.rwd ) f.s[check] = '\0';                 // insert sentinel
    10121013                fmt( is, fmtstr, f.s );
    1013                 if ( f.s[check] != '\0' )                                               // sentinel overwritten ?
     1014//              fprintf( stderr, "KK %s %d %c\n", fmtstr, check, f.s[check] );
     1015                if ( ! f.flags.rwd && f.s[check] != '\0' )              // sentinel overwritten ?
    10141016                        throw (cstring_length){ &cstring_length_vt };
    10151017                return is;
  • libcfa/src/iostream.hfa

    rd0cfcbe1 r5ad2c6c7  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Aug 18 10:44:50 2023
    13 // Update Count     : 512
     12// Last Modified On : Thu Aug 24 11:35:02 2023
     13// Update Count     : 530
    1414//
    1515
     
    1818#include "iterator.hfa"
    1919#include "Exception.hfa"
    20 
    2120
    2221// *********************************** ostream ***********************************
     
    432431
    433432static inline {
    434         _Istream_Cstr skip( const char scanset[] ) { return (_Istream_Cstr){ 0p, {scanset}, -1, {.all : 0} }; }
    435         _Istream_Cstr skip( unsigned int wd ) { return (_Istream_Cstr){ 0p, {0p}, wd, {.all : 0} }; }
    436         _Istream_Cstr & getline( _Istream_Cstr & fmt ) { fmt.delimit = '\n'; fmt.flags.delimit = true; return fmt; }
    437         _Istream_Cstr & getline( const char delimit, _Istream_Cstr & fmt ) { fmt.delimit = delimit; fmt.flags.delimit = true; return fmt; }
    438 //      _Istream_Cstr incl( const char scanset[], char s[] ) { return (_Istream_Cstr){ s, {scanset}, -1, { .flags.inex : false } }; }
    439 //      _Istream_Cstr incl( const char scanset[], unsigned int wd, char s[] ) { return (_Istream_Cstr){ s, {scanset}, wd, {.flags.inex : false} }; }
    440         _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
    441 //      _Istream_Cstr excl( const char scanset[], char s[] ) { return (_Istream_Cstr){ s, {scanset}, -1, { .flags.inex : true } }; }
    442 //      _Istream_Cstr excl( const char scanset[], unsigned int wd, char s[] ) { return (_Istream_Cstr){ s, {scanset}, wd, {.flags.inex : true} }; }
    443         _Istream_Cstr & excl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
    444         _Istream_Cstr ignore( char s[] ) { return (_Istream_Cstr)@{ s, {0p}, -1, {.flags.ignore : true} }; }
    445         _Istream_Cstr & ignore( _Istream_Cstr & fmt ) { fmt.flags.ignore = true; return fmt; }
     433        // width must include room for null terminator
    446434        _Istream_Cstr wdi( unsigned int wd, char s[] ) { return (_Istream_Cstr)@{ s, {0p}, wd, {.all : 0} }; }
     435        // read width does not include null terminator
    447436        _Istream_Cstr wdi( unsigned int wd, unsigned int rwd, char s[] ) {
    448437                if ( wd <= rwd ) throw (cstring_length){ &cstring_length_vt };
    449438                return (_Istream_Cstr)@{ s, {0p}, rwd, {.flags.rwd : true} };
    450439        }
    451 //      _Istream_Cstr & wdi( unsigned int wd, _Istream_Cstr & fmt ) { fmt.wd = wd; return fmt; }
     440        _Istream_Cstr skip( const char scanset[] ) { return (_Istream_Cstr){ 0p, {scanset}, -1, {.all : 0} }; }
     441        _Istream_Cstr skip( unsigned int wd ) { return (_Istream_Cstr){ 0p, {0p}, wd, {.all : 0} }; }
     442        _Istream_Cstr & getline( _Istream_Cstr & fmt, const char delimit = '\n' ) { fmt.delimit = delimit; fmt.flags.delimit = true; return fmt; }
     443        _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
     444        _Istream_Cstr & excl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
     445        _Istream_Cstr ignore( char s[] ) { return (_Istream_Cstr)@{ s, {0p}, -1, {.flags.ignore : true} }; }
     446        _Istream_Cstr & ignore( _Istream_Cstr & fmt ) { fmt.flags.ignore = true; return fmt; }
    452447} // distribution
    453448forall( istype & | basic_istream( istype ) ) {
Note: See TracChangeset for help on using the changeset viewer.