Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Tor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
The Tor Project
Core
Tor
Commits
22dc19b5
Commit
22dc19b5
authored
20 years ago
by
Nick Mathewson
Browse files
Options
Downloads
Patches
Plain Diff
snprintf wrapper with consistant (though not C99) overflow behavior
svn:r2606
parent
06fa8fc0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/common/util.c
+36
-1
36 additions, 1 deletion
src/common/util.c
src/common/util.h
+12
-0
12 additions, 0 deletions
src/common/util.h
with
48 additions
and
1 deletion
src/common/util.c
+
36
−
1
View file @
22dc19b5
...
...
@@ -2185,7 +2185,7 @@ parse_addr_and_port_range(const char *s, uint32_t *addr_out,
* "mask" is the Mask|Maskbits part...
* and "port" is the *|port|min-max part.
*/
if
(
strcmp
(
address
,
"*"
)
==
0
)
{
*
addr_out
=
0
;
}
else
if
(
tor_inet_aton
(
address
,
&
in
)
!=
0
)
{
...
...
@@ -2318,6 +2318,41 @@ tor_parse_ulong(const char *s, int base, unsigned long min,
return
0
;
}
/** Replacement for snprintf. Differs from platform snprintf in two
* ways: First, always NUL-terminates its output. Second, always
* returns -1 if the result is truncated. (Note that this return
* behavior does <i>not</i> conform to C99; it just happens to be the
* easiest to emulate "return -1" with conformant implementations than
* it is to emulate "return number that would be written" with
* non-conformant implementations.) */
int
tor_snprintf
(
char
*
str
,
size_t
size
,
const
char
*
format
,
...)
{
va_list
ap
;
int
r
;
va_start
(
ap
,
format
);
r
=
tor_vsnprintf
(
str
,
size
,
format
,
ap
);
va_end
(
ap
);
return
r
;
}
/** Replacement for vsnpritnf; behavior differs as tor_snprintf differs from
* snprintf.
*/
int
tor_vsnprintf
(
char
*
str
,
size_t
size
,
const
char
*
format
,
va_list
args
)
{
int
r
;
#ifdef MS_WINDOWS
r
=
_vsnprintf
(
str
,
size
,
format
,
args
);
#else
r
=
vsnprintf
(
str
,
size
,
format
,
args
);
#endif
str
[
size
-
1
]
=
'\0'
;
if
(
r
<
0
||
r
>=
size
)
return
-
1
;
return
r
;
}
#ifndef MS_WINDOWS
struct
tor_mutex_t
{
};
...
...
This diff is collapsed.
Click to expand it.
src/common/util.h
+
12
−
0
View file @
22dc19b5
...
...
@@ -13,6 +13,7 @@
#include
"orconfig.h"
#include
"torint.h"
#include
<stdio.h>
#include
<stdarg.h>
#ifdef HAVE_SYS_TIME_H
#include
<sys/time.h>
#endif
...
...
@@ -106,7 +107,18 @@ long tor_parse_long(const char *s, int base, long min,
unsigned
long
tor_parse_ulong
(
const
char
*
s
,
int
base
,
unsigned
long
min
,
unsigned
long
max
,
int
*
ok
,
char
**
next
);
/* XXXX duplicated from log.h */
#ifdef __GNUC__
#define CHECK_PRINTF(formatIdx, firstArg) \
__attribute__ ((format (printf, formatIdx, firstArg)))
#else
#define CHECK_PRINTF(formatIdx, firstArg)
#endif
int
tor_snprintf
(
char
*
str
,
size_t
size
,
const
char
*
format
,
...)
CHECK_PRINTF
(
3
,
4
);
int
tor_vsnprintf
(
char
*
str
,
size_t
size
,
const
char
*
format
,
va_list
args
);
/* Some platforms segfault when you try to access a multi-byte type
* that isn't aligned to a word boundary. The macros and/or functions
* below can be used to access unaligned data on any platform.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment