Commit 4a5c30be authored by Yoni Fogel's avatar Yoni Fogel

Addresses #1508 Added test that shows snprintf problem

git-svn-id: file:///svn/toku/tokudb@9701 c7de825b-a66e-492c-adef-691d508d4ae1
parent bd0e1386
../../windows/tests/test-snprintf.c
\ No newline at end of file
/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved."
#define _GNU_SOURCE
#include "toku_portability.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "toku_os.h"
void
check_snprintf(int i) {
char buf_before[8];
char target[8];
char buf_after[8];
memset(target, 0xFF, sizeof(target));
memset(buf_before, 0xFF, sizeof(buf_before));
memset(buf_after, 0xFF, sizeof(buf_after));
int64_t n = 1;
int j;
for (j = 0; j < i; j++) n *= 10;
int bytes = snprintf(target, sizeof target, "%"PRId64, n);
assert(bytes==i+1);
if (bytes>=sizeof target) {
//Overflow prevented by snprintf
assert(target[sizeof target - 1] == '\0');
assert(strlen(target)==sizeof target-1);
}
else {
assert(target[bytes] == '\0');
assert(strlen(target)==bytes);
}
}
int main(void) {
int i;
for (i = 0; i < 8; i++) {
check_snprintf(i);
}
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment