<commit_before>/*
 * Copyright (c) 2017-2020 Red Hat.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 */
#include <assert.h>
#include <ctype.h>
#include "pmapi.h"
#include "libpcp.h"
#include "pmwebapi.h"
#include "private.h"
#include "sdsalloc.h"
#include "zmalloc.h"
#include "maps.h"
#include "util.h"
#include "sha1.h"

/* dynamic memory manipulation */
static void
default_oom(size_t size)
{
    fprintf(stderr, "Out of memory allocating %" FMT_UINT64 " bytes\n",
		(__uint64_t)size);
    fflush(stderr);
    abort();
}
static void (*oom_handler)(size_t) = default_oom;

void *
s_malloc(size_t size)
{
    void	*p;

    p = malloc(size);
    if (UNLIKELY(p == NULL))
	oom_handler(size);
    return p;
}

void *
s_realloc(void *ptr, size_t size)
{
    void	*p;

    if (ptr == NULL)
	return s_malloc(size);
    p = realloc(ptr, size);
    if (UNLIKELY(p == NULL))
	oom_handler(size);
    return p;
}

void
s_free(void *ptr)
{
    if (LIKELY(ptr != NULL))
	free(ptr);
}

void *
zmalloc(size_t size)
{
    void	*p;

    p = malloc(size);
    if (UNLIKELY(p == NULL))
	oom_handler(size);
    return p;
}

void *
zcalloc(size_t elem, size_t size)
{
    void	*p;

    p = calloc(elem, size);
    if (UNLIKELY(p == NULL))
	oom_handler(elem * size);
    return p;
}

void
zfree(void *ptr)
{
    if (LIKELY(ptr != NULL))
	free(ptr);
}

/* time structure manipulation */
int
tsub(struct timeval *a, struct timeval *b)
{
    if ((a == NULL) || (b == NULL))
	return -1;
    pmtimevalDec(a, b);
    if (a->tv_sec < 0) {
	/* clip negative values at zero */
	a->tv_sec = 0;
	a->tv_usec = 0;
    }
    return 0;
}

int
tadd(struct timeval *a, struct timeval *b)
{
    if ((a == NULL) || (b == NULL))
	return -1;
    pmtimevalInc(a, b);
    return 0;
}

/* convert into <milliseconds>-<nanoseconds> format for series streaming */
const char *
timeval_stream_str(struct timeval *stamp, char *buffer, int buflen)
{
    __uint64_t	millipart;
    __uint64_t	fractions;
    __uint64_t	crossover = stamp->tv_usec / 1000;

    millipart = stamp->tv_sec * 1000;
    millipart += crossover;
    fractions = stamp->tv_usec % 1000;
    pmsprintf(buffer, buflen, "%" FMT_UINT64 "-%"FMT_UINT64,
		(__uint64_t)millipart, (__uint64_t)fractions);
    return buffer;
}

/* convert timeval into human readable date/time format for logging */
const char *
timeval_str(struct timeval *tvp, char *buffer, int buflen)<commit_msg>libpcp_web: fix 32bit int overflow in timeval_stream_str()<commit_after>/*
 * Copyright (c) 2017-2020 Red Hat.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 */
#include <assert.h>
#include <ctype.h>
#include "pmapi.h"
#include "libpcp.h"
#include "pmwebapi.h"
#include "private.h"
#include "sdsalloc.h"
#include "zmalloc.h"
#include "maps.h"
#include "util.h"
#include "sha1.h"

/* dynamic memory manipulation */
static void
default_oom(size_t size)
{
    fprintf(stderr, "Out of memory allocating %" FMT_UINT64 " bytes\n",
		(__uint64_t)size);
    fflush(stderr);
    abort();
}
static void (*oom_handler)(size_t) = default_oom;

void *
s_malloc(size_t size)
{
    void	*p;

    p = malloc(size);
    if (UNLIKELY(p == NULL))
	oom_handler(size);
    return p;
}

void *
s_realloc(void *ptr, size_t size)
{
    void	*p;

    if (ptr == NULL)
	return s_malloc(size);
    p = realloc(ptr, size);
    if (UNLIKELY(p == NULL))
	oom_handler(size);
    return p;
}

void
s_free(void *ptr)
{
    if (LIKELY(ptr != NULL))
	free(ptr);
}

void *
zmalloc(size_t size)
{
    void	*p;

    p = malloc(size);
    if (UNLIKELY(p == NULL))
	oom_handler(size);
    return p;
}

void *
zcalloc(size_t elem, size_t size)
{
    void	*p;

    p = calloc(elem, size);
    if (UNLIKELY(p == NULL))
	oom_handler(elem * size);
    return p;
}

void
zfree(void *ptr)
{
    if (LIKELY(ptr != NULL))
	free(ptr);
}

/* time structure manipulation */
int
tsub(struct timeval *a, struct timeval *b)
{
    if ((a == NULL) || (b == NULL))
	return -1;
    pmtimevalDec(a, b);
    if (a->tv_sec < 0) {
	/* clip negative values at zero */
	a->tv_sec = 0;
	a->tv_usec = 0;
    }
    return 0;
}

int
tadd(struct timeval *a, struct timeval *b)
{
    if ((a == NULL) || (b == NULL))
	return -1;
    pmtimevalInc(a, b);
    return 0;
}

/* convert into <milliseconds>-<nanoseconds> format for series streaming */
const char *
timeval_stream_str(struct timeval *stamp, char *buffer, int buflen)
{
    __uint64_t	millipart;
    __uint64_t	fractions;
    __uint64_t	crossover = stamp->tv_usec / 1000;

    millipart = ((__uint64_t)stamp->tv_sec) * 1000;
    millipart += crossover;
    fractions = stamp->tv_usec % 1000;
    pmsprintf(buffer, buflen, "%" FMT_UINT64 "-%"FMT_UINT64,
		(__uint64_t)millipart, (__uint64_t)fractions);
    return buffer;
}

/* convert timeval into human readable date/time format for logging */
const char *
timeval_str(struct timeval *tvp, char *buffer, int buflen)
{'fraction_non_alphanumeric': 0.13270060396893874, 'fraction_numerical': 0.01725625539257981, 'mean_word_length': 3.5175370226032734, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '485037', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2134, 'n_words': 639}