Skip to content

Commit

Permalink
Constify argv, fix warnings. (#1242)
Browse files Browse the repository at this point in the history
* Fix perl warnings

 - cast POPs to void to avoid unused value warning
 - declare functions that don't set RETVAL as returning void

Signed-off-by: Michal Suchanek <msuchanek@suse.de>

* Constify argv

rrd has no business modifying the string pointed to by passed agrv, and
as far as gcc can see it does indeed not modify them because it compiles
with const argv.

This fixes warnings when passing const strings into rrd, and avoids the
need to duplicate all strings in the tcl bindings.

This fixes warnings like these:
[    3s] prog/sensord/rrd.c: In function 'rrdInit':
[    3s] prog/sensord/rrd.c:302:40: warning: cast discards 'const' qualifier from pointer target type [-Wcast-qual]
[    3s]   302 |                 ret = rrd_create(argc, (char**) argv);
[    3s]       |                                        ^
[    3s] prog/sensord/rrd.c: In function 'rrdUpdate':
[    3s] prog/sensord/rrd.c:458:42: warning: cast discards 'const' qualifier from pointer target type [-Wcast-qual]
[    3s]   458 |                 if ((ret = rrd_update(3, (char **) /* WEAK */ argv))) {
[    3s]       |                                          ^

Signed-off-by: Michal Suchanek <msuchanek@suse.de>

* tcl: Do not duplicate const strings

---------

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
  • Loading branch information
hramrach authored Jan 5, 2024
1 parent c1be8ba commit b76e3c5
Show file tree
Hide file tree
Showing 32 changed files with 168 additions and 209 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Features

* Add (remote) dump support to rrdcached <Tobias Hintze>

* Constify argv argument to library functions
Without this users of library function that want to pass in const strings need to duplicate them to avoid compiler warnings

RRDtool 1.8.0 - 2022-03-13
==========================

Expand Down
25 changes: 12 additions & 13 deletions bindings/lua/rrdlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
extern void rrd_freemem(void *mem);

extern int luaopen_rrd (lua_State * L);
typedef int (*RRD_FUNCTION)(int, char **);
typedef rrd_info_t *(RRD_FUNCTION_V)(int, char **);
typedef int (*RRD_FUNCTION)(int, const char **);
typedef rrd_info_t *(RRD_FUNCTION_V)(int, const char **);

/**********************************************************/

Expand All @@ -49,9 +49,9 @@ static void reset_rrd_state(void)
rrd_clear_error();
}

static char **make_argv(const char *cmd, lua_State * L)
static const char **make_argv(const char *cmd, lua_State * L)
{
char **argv;
const char **argv;
int i;
int argc = lua_gettop(L) + 1;

Expand All @@ -60,13 +60,12 @@ static char **make_argv(const char *cmd, lua_State * L)
luaL_error(L, "Can't allocate memory for arguments array", cmd);

/* fprintf(stderr, "Args:\n"); */
argv[0] = (char *) cmd; /* Dummy arg. Cast to (char *) because rrd */
/* functions don't expect (const * char) */
argv[0] = cmd;
/* fprintf(stderr, "%s\n", argv[0]); */
for (i=1; i<argc; i++) {
/* accepts string or number */
if (lua_isstring(L, i) || lua_isnumber(L, i)) {
if (!(argv[i] = (char *) lua_tostring (L, i))) {
if (!(argv[i] = lua_tostring (L, i))) {
/* raise an error and never return */
luaL_error(L, "%s - error duplicating string area for arg #%d",
cmd, i);
Expand All @@ -84,7 +83,7 @@ static char **make_argv(const char *cmd, lua_State * L)
static int
rrd_common_call (lua_State *L, const char *cmd, RRD_FUNCTION rrd_function)
{
char **argv;
const char **argv;
int argc = lua_gettop(L) + 1;

argv = make_argv(cmd, L);
Expand All @@ -99,7 +98,7 @@ rrd_common_call (lua_State *L, const char *cmd, RRD_FUNCTION rrd_function)
static int
lua_rrd_infocall(lua_State *L, const char *cmd, RRD_FUNCTION_V rrd_function)
{
char **argv;
const char **argv;
rrd_info_t *p, *data;
int argc = lua_gettop(L) + 1;

Expand Down Expand Up @@ -197,7 +196,7 @@ static int
lua_rrd_fetch (lua_State * L)
{
int argc = lua_gettop(L) + 1;
char **argv = make_argv("fetch", L);
const char **argv = make_argv("fetch", L);
unsigned long i, j, step, ds_cnt;
rrd_value_t *data, *p;
char **names;
Expand Down Expand Up @@ -246,7 +245,7 @@ lua_rrd_first (lua_State * L)
{
time_t first;
int argc = lua_gettop(L) + 1;
char **argv = make_argv("first", L);
const char **argv = make_argv("first", L);
reset_rrd_state();
first = rrd_first(argc, argv);
free(argv);
Expand All @@ -260,7 +259,7 @@ lua_rrd_last (lua_State * L)
{
time_t last;
int argc = lua_gettop(L) + 1;
char **argv = make_argv("last", L);
const char **argv = make_argv("last", L);
reset_rrd_state();
last = rrd_last(argc, argv);
free(argv);
Expand All @@ -275,7 +274,7 @@ static int
lua_rrd_graph (lua_State * L)
{
int argc = lua_gettop(L) + 1;
char **argv = make_argv("last", L);
const char **argv = make_argv("last", L);
char **calcpr;
int i, xsize, ysize;
double ymin, ymax;
Expand Down
36 changes: 18 additions & 18 deletions bindings/perl-shared/RRDs.xs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ extern "C" {
strcpy(argv[i+1],handle); \
} \
rrd_clear_error();\
RETVAL=name(items+1,argv); \
RETVAL = name(items + 1, (const char **)argv); \
for (i=0; i < items; i++) {\
free(argv[i+1]);\
free((void *)argv[i+1]);\
} \
free(argv);\
free((void *)argv);\
\
if (rrd_test_error()) XSRETURN_UNDEF;

Expand All @@ -67,11 +67,11 @@ extern "C" {
strcpy(argv[i+1],handle); \
} \
rrd_clear_error(); \
data=name(items+1, argv); \
data = name(items + 1, (const char **)argv); \
for (i=0; i < items; i++) { \
free(argv[i+1]); \
free((void *)argv[i+1]); \
} \
free(argv); \
free((void *)argv); \
if (rrd_test_error()) XSRETURN_UNDEF; \
hash = newHV(); \
save=data; \
Expand Down Expand Up @@ -175,7 +175,7 @@ static int rrd_fetch_cb_wrapper(
/* Check the eval first */
if (SvTRUE(ERRSV)) {
rrd_set_error("perl callback failed: %s",SvPV_nolen(ERRSV));
POPs; /* there is undef on top of the stack when there is an error
(void)POPs; /* there is undef on top of the stack when there is an error
and call_sv was initiated with G_EVAL|G_SCALER */
goto error_out;
}
Expand Down Expand Up @@ -383,7 +383,7 @@ rrd_tune(...)

#ifdef HAVE_RRD_GRAPH

SV *
void
rrd_graph(...)
PROTOTYPE: @
PREINIT:
Expand All @@ -404,7 +404,7 @@ rrd_graph(...)
strcpy(argv[i+1],handle);
}
rrd_clear_error();
rrd_graph(items+1,argv,&calcpr,&xsize,&ysize,NULL,&ymin,&ymax);
rrd_graph(items+1,(const char **)argv,&calcpr,&xsize,&ysize,NULL,&ymin,&ymax);
for (i=0; i < items; i++) {
free(argv[i+1]);
}
Expand Down Expand Up @@ -433,7 +433,7 @@ rrd_graph(...)

#endif /* HAVE_RRD_GRAPH */

SV *
void
rrd_fetch(...)
PROTOTYPE: @
PREINIT:
Expand All @@ -455,7 +455,7 @@ rrd_fetch(...)
strcpy(argv[i+1],handle);
}
rrd_clear_error();
rrd_fetch(items+1,argv,&start,&end,&step,&ds_cnt,&ds_namv,&data);
rrd_fetch(items+1,(const char **)argv,&start,&end,&step,&ds_cnt,&ds_namv,&data);
for (i=0; i < items; i++) {
free(argv[i+1]);
}
Expand Down Expand Up @@ -486,7 +486,7 @@ rrd_fetch(...)
PUSHs(sv_2mortal(newRV_noinc((SV*)names)));
PUSHs(sv_2mortal(newRV_noinc((SV*)retar)));

SV *
void
rrd_fetch_cb_register(cb)
SV * cb
CODE:
Expand All @@ -496,7 +496,7 @@ rrd_fetch_cb_register(cb)
SvSetSV(rrd_fetch_cb_svptr,cb);
rrd_fetch_cb_register(rrd_fetch_cb_wrapper);

SV *
void
rrd_times(start, end)
char *start
char *end
Expand All @@ -521,7 +521,7 @@ rrd_times(start, end)
PUSHs(sv_2mortal(newSVuv(start_tmp)));
PUSHs(sv_2mortal(newSVuv(end_tmp)));

int
void
rrd_xport(...)
PROTOTYPE: @
PREINIT:
Expand All @@ -543,7 +543,7 @@ rrd_xport(...)
strcpy(argv[i+1],handle);
}
rrd_clear_error();
rrd_xport(items+1,argv,&xsize,&start,&end,&step,&col_cnt,&legend_v,&data);
rrd_xport(items+1,(const char **)argv,&xsize,&start,&end,&step,&col_cnt,&legend_v,&data);
for (i=0; i < items; i++) {
free(argv[i+1]);
}
Expand Down Expand Up @@ -657,7 +657,7 @@ rrd_flushcached(...)
OUTPUT:
RETVAL

SV*
void
rrd_list(...)
PROTOTYPE: @
PREINIT:
Expand All @@ -667,7 +667,7 @@ rrd_list(...)
char **argv;
AV *list;
PPCODE:
argv = (char **) malloc((items+1)*sizeof(char *));
argv = malloc((items+1)*sizeof(char *));
argv[0] = "dummy";

for (i = 0; i < items; i++) {
Expand All @@ -681,7 +681,7 @@ rrd_list(...)

rrd_clear_error();

data = rrd_list(items+1, argv);
data = rrd_list(items+1, (const char **)argv);

for (i=0; i < items; i++) {
free(argv[i+1]);
Expand Down
Loading

0 comments on commit b76e3c5

Please sign in to comment.