• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "securecutil.h"
17 
18 /*******************************************************************************
19  * <FUNCTION DESCRIPTION>
20  *    The vsprintf_s function is equivalent to the vsprintf function
21  *    except for the parameter destMax and the explicit runtime-constraints violation
22  *    The vsprintf_s function takes a pointer to an argument list, and then formats
23  *    and writes the given data to the memory pointed to by strDest.
24  *    The function differ from the non-secure versions only in that the secure
25  *    versions support positional parameters.
26  *
27  * <INPUT PARAMETERS>
28  *    strDest                Storage location for the output.
29  *    destMax                Size of strDest
30  *    format                 Format specification.
31  *    arglist                   pointer to list of arguments
32  *
33  * <OUTPUT PARAMETERS>
34  *    strDest                is updated
35  *
36  * <RETURN VALUE>
37  *    return  the number of characters written, not including the terminating null character,
38  *    return -1  if an  error occurs.
39  *
40  * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
41  *******************************************************************************
42  */
43 #ifndef __MINGW32__
vsprintf_s(char * strDest,size_t destMax,const char * format,va_list arglist)44 int vsprintf_s(char *strDest, size_t destMax, const char *format, va_list arglist)
45 {
46     int retVal;               /* If initialization causes  e838 */
47 
48     if (format == NULL || strDest == NULL || destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
49         if (strDest != NULL && destMax > 0) {
50             strDest[0] = '\0';
51         }
52         SECUREC_ERROR_INVALID_PARAMTER("vsprintf_s");
53         return -1;
54     }
55 
56     retVal = SecVsnprintfImpl(strDest, destMax, format, arglist);
57 
58     if (retVal < 0) {
59         strDest[0] = '\0';
60         if (retVal == SECUREC_PRINTF_TRUNCATE) {
61             /* Buffer is too small */
62             SECUREC_ERROR_INVALID_RANGE("vsprintf_s");
63         }
64         SECUREC_ERROR_INVALID_PARAMTER("vsprintf_s");
65         return -1;
66     }
67 
68     return retVal;
69 }
70 #endif
71 
72