safec  2.1
Safe C Library - ISO TR24731 Bounds Checking Interface
snprintf_s.c
Go to the documentation of this file.
1 /* August 2017, Reini Urban
2  *
3  * Copyright (c) 2017 by Reini Urban
4  * All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or
11  * sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *------------------------------------------------------------------
27  */
28 
29 /* Need restrict */
30 #include "config.h"
31 #include "safe_str_lib.h"
32 #include "safe_str_constraint.h"
33 #include <stdarg.h>
34 
35 /* TODO: error when fmt contains %n, or encoding errors occur.
36  */
37 
74 int snprintf_s(char * restrict dest, rsize_t dmax, const char * restrict fmt, ...)
75 {
76  va_list ap;
77  int ret = -1;
78  if (dmax > RSIZE_MAX_STR) {
79  invoke_safe_str_constraint_handler("snprintf_s: dmax exceeds max",
80  NULL, ESLEMAX);
81  return RCNEGATE(ESLEMAX);
82  }
83 
84  if (dest == NULL) {
85  invoke_safe_str_constraint_handler("snprintf_s: dest is null",
86  NULL, ESNULLP);
87  return RCNEGATE(ESNULLP);
88  }
89 
90  if (fmt == NULL) {
91  invoke_safe_str_constraint_handler("snprintf_s: fmt is null",
92  NULL, ESNULLP);
93  return RCNEGATE(ESNULLP);
94  }
95 
96  if (dmax == 0) {
97  invoke_safe_str_constraint_handler("snprintf_s: dmax is 0",
98  NULL, ESZEROL);
99  return RCNEGATE(ESZEROL);
100  }
101 
102  va_start(ap, fmt);
103 
104  ret = vsnprintf(dest, (size_t)dmax, fmt, ap);
105 
106  /*if (ret >= (int)dmax) {
107  invoke_safe_str_constraint_handler("snprintf_s: len exceeds dmax",
108  NULL, ESNOSPC);
109  *dest = 0;
110  ret = RCNEGATE(ESNOSPC);
111  }*/
112 
113  va_end(ap);
114 
115  return ret;
116 }
int snprintf_s(char *restrict dest, rsize_t dmax, const char *restrict fmt,...)
The snprintf_s function composes a string with same test that would be printed if format was used on ...
Definition: snprintf_s.c:74
void invoke_safe_str_constraint_handler(const char *msg, void *ptr, errno_t error)
Invokes the currently set constraint handler or the default.