Beispiel #1
0
int vfwscanf_s(FILE* stream, const wchar_t* format, va_list arglist)
{
    int retval = 0;
    SEC_FILE_STREAM fStr = INIT_SEC_FILE_STREAM;

    if ((stream == NULL) ||  (format == NULL) )
    {
        SECUREC_ERROR_INVALID_PARAMTER("vfwscanf_s");
        return SCANF_EINVAL;
    }

    SECUREC_LOCK_FILE(stream);

    _VALIDATE_STREAM_ANSI_SETRET(stream, EINVAL, retval, EOF);
    fStr.pf = stream;
    fStr._flag = FILE_STREAM_FLAG;
    fStr.oriFilePos = UNINITIALIZED_FILE_POS;

    retval = securec_winput_s(&fStr, format,  arglist);
    SECUREC_UNLOCK_FILE(stream);
    if (retval < 0)
    {
        /*MaskCoverityID13633*/ /*coverity[RESOURCE_LEAK]*/
        SECUREC_ERROR_INVALID_PARAMTER("vfwscanf_s");
        /* coverity[leaked_storage] */
        return SCANF_EINVAL;
    }
    return retval;
}
int __cdecl fprintf (
        FILE *str,
        const char *format,
        ...
        )
/*
 * 'F'ile (stream) 'PRINT', 'F'ormatted
 */
{
    va_list(arglist);
    FILE *stream;
    int buffing;
    int retval=0;

    _VALIDATE_RETURN( (str != NULL), EINVAL, -1);
    _VALIDATE_RETURN( (format != NULL), EINVAL, -1);

    va_start(arglist, format);

    /* Init stream pointer */
    stream = str;

    _lock_str(stream);
    __try {
        _VALIDATE_STREAM_ANSI_SETRET(stream, EINVAL, retval, -1);

        if (retval==0)
        {
            buffing = _stbuf(stream);
            retval = _output_l(stream,format,NULL,arglist);
            _ftbuf(buffing, stream);
        }
    }
    __finally {
        _unlock_str(stream);
    }

    return(retval);
}
Beispiel #3
0
_TCHAR* __cdecl _getts_helper(
    _TCHAR* string,
    size_t bufferSize,
    int earlyOutIfEOFIsFirstChar
) {
    int ch = 0;
    _TCHAR* pointer = string;
    _TCHAR* retval = string;
    _VALIDATE_RETURN((string != NULL), EINVAL, NULL);
    _VALIDATE_RETURN((bufferSize > 0), EINVAL, NULL);
    /* The C Standard states the input buffer should remain
    unchanged if EOF is encountered immediately. Hence we
    do not blank out the input buffer here */
    _lock_str2(0, stdin);

    __try {
#ifndef _UNICODE
        _VALIDATE_STREAM_ANSI_SETRET(stdin, EINVAL, retval, NULL);

        if (retval == NULL) {
            goto done;
        }

#endif  /* _UNICODE */
        /* special case: check if the first char is EOF and treat it differently if the user requested so */
        ch = _getchar_helper();

        if (ch == _TEOF) {
            retval = NULL;

            if (earlyOutIfEOFIsFirstChar) {
                goto done;
            }
        }

        if (bufferSize == (size_t) - 1) {
            /* insecure case: no buffer size check, no debug filling */
            while (ch != _T('\n') && ch != _TEOF) {
                *pointer++ = (_TCHAR)ch;
                ch = _getchar_helper();
            }

            *pointer = 0;
        } else {
            /* secure case, check buffer size; if buffer overflow, keep on reading until /n or EOF */
            size_t available = bufferSize;

            while (ch != _T('\n') && ch != _TEOF) {
                if (available > 0) {
                    --available;
                    *pointer++ = (_TCHAR)ch;
                }

                ch = _getchar_helper();
            }

            if (available == 0) {
                _RESET_STRING(string, bufferSize);
                _RETURN_BUFFER_TOO_SMALL_ERROR(string, bufferSize, NULL);
            }

            *pointer = 0;
            _FILL_STRING(string, bufferSize, bufferSize - available + 1);
        }

        /* Common return */
    done:
        ;
    } __finally {
        _unlock_str2(0, stdin);
    }

    return retval;
}
_TSCHAR * __cdecl _fgetts (
        _TSCHAR *string,
        int count,
        FILE *str
        )
{
    FILE *stream;
    _TSCHAR *pointer = string;
    _TSCHAR *retval = string;
    int ch;

    _VALIDATE_RETURN(( string != NULL ) || ( count == 0 ), EINVAL, NULL);
    _VALIDATE_RETURN(( count >= 0 ), EINVAL, NULL);
    _VALIDATE_RETURN(( str != NULL ), EINVAL, NULL);

    if (count == 0)
    {
        return NULL;
    }

    /* The C Standard states the input buffer should remain
    unchanged if EOF is encountered immediately. Hence we
    do not blank out the input buffer here */

    /* Init stream pointer */
    stream = str;

    _lock_str(stream);
    __try {
#ifndef _UNICODE
        _VALIDATE_STREAM_ANSI_SETRET(stream, EINVAL, retval, NULL);
#endif  /* _UNICODE */
        if (retval!=NULL)
        {
            while (--count)
            {
                if ((ch = _fgettc_nolock(stream)) == _TEOF)
                {
                    if (pointer == string) {
                                    retval=NULL;
                                    goto done;
                    }

                    break;
                }

                if ((*pointer++ = (_TSCHAR)ch) == _T('\n'))
                    break;
            }

            *pointer = _T('\0');
        }


/* Common return */
done: ;
    }
    __finally {
        _unlock_str(stream);
    }

    return(retval);
}