Integer types cleanup

Try to be a bit more strict about integer types, use off_t or int64_t for
file size and file offsets.

Replace safe_ato*() by safe_atoi32() and safe_atoi64(), fix integer types
at call sites to match.
This commit is contained in:
Julien BLACHE 2010-02-02 21:02:24 +01:00
parent 5d41d2d99c
commit 58faeaceca
10 changed files with 72 additions and 93 deletions

View File

@ -30,6 +30,7 @@ options {
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include "logger.h"
#include "db.h"
@ -370,7 +371,7 @@ datespec returns [ time_t date, int valid ]
}
| ^(o = dateop r = dateref m = INT i = dateintval)
{
int val;
int32_t val;
int ret;
if (!$r.valid || !$i.valid)
@ -379,7 +380,7 @@ datespec returns [ time_t date, int valid ]
goto datespec_valid_0; /* ABORT */
}
ret = safe_atoi((char *)$m->getText($m)->chars, &val);
ret = safe_atoi32((char *)$m->getText($m)->chars, &val);
if (ret < 0)
{
DPRINTF(E_LOG, L_RSP, "Could not convert '\%s' to integer\n", (char *)$m->getText($m));

View File

@ -33,6 +33,8 @@
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <inttypes.h>
#include <event.h>
#include "evhttp/evhttp.h"
@ -81,11 +83,11 @@ struct stream_ctx {
struct event ev;
int id;
int fd;
size_t size;
size_t stream_size;
size_t offset;
size_t start_offset;
size_t end_offset;
off_t size;
off_t stream_size;
off_t offset;
off_t start_offset;
off_t end_offset;
int marked;
struct transcode_ctx *xcode;
};
@ -302,8 +304,8 @@ httpd_stream_file(struct evhttp_request *req, int id)
const char *param;
const char *param_end;
char buf[64];
long offset;
long end_offset;
int64_t offset;
int64_t end_offset;
int transcode;
int ret;
@ -315,7 +317,7 @@ httpd_stream_file(struct evhttp_request *req, int id)
DPRINTF(E_DBG, L_HTTPD, "Found Range header: %s\n", param);
/* Start offset */
ret = safe_atol(param + strlen("bytes="), &offset);
ret = safe_atoi64(param + strlen("bytes="), &offset);
if (ret < 0)
{
DPRINTF(E_LOG, L_HTTPD, "Invalid start offset, will stream whole file (%s)\n", param);
@ -327,7 +329,7 @@ httpd_stream_file(struct evhttp_request *req, int id)
param_end = strchr(param, '-');
if (param_end)
{
ret = safe_atol(param_end + 1, &end_offset);
ret = safe_atoi64(param_end + 1, &end_offset);
if (ret < 0)
{
DPRINTF(E_LOG, L_HTTPD, "Invalid end offset, will stream to end of file (%s)\n", param);
@ -336,7 +338,7 @@ httpd_stream_file(struct evhttp_request *req, int id)
if (end_offset < offset)
{
DPRINTF(E_LOG, L_HTTPD, "End offset < start offset, will stream to end of file (%ld < %ld)\n", end_offset, offset);
DPRINTF(E_LOG, L_HTTPD, "End offset < start offset, will stream to end of file (%" PRIi64 " < %" PRIi64 ")\n", end_offset, offset);
end_offset = 0;
}
}
@ -543,7 +545,7 @@ httpd_stream_file(struct evhttp_request *req, int id)
*/
if (!st->xcode)
{
ret = snprintf(buf, sizeof(buf), "%ld", st->size);
ret = snprintf(buf, sizeof(buf), "%" PRIi64, (int64_t)st->size);
if ((ret < 0) || (ret >= sizeof(buf)))
DPRINTF(E_LOG, L_HTTPD, "Content-Length too large for buffer, dropping\n");
else
@ -559,16 +561,16 @@ httpd_stream_file(struct evhttp_request *req, int id)
if (end_offset > 0)
st->stream_size -= (st->size - end_offset);
DPRINTF(E_DBG, L_HTTPD, "Stream request with range %ld-%ld\n", offset, end_offset);
DPRINTF(E_DBG, L_HTTPD, "Stream request with range %" PRIi64 "-%" PRIi64 "\n", offset, end_offset);
ret = snprintf(buf, sizeof(buf), "bytes %ld-%ld/%ld",
offset, (end_offset) ? end_offset : (long)sb.st_size, (long)sb.st_size);
ret = snprintf(buf, sizeof(buf), "bytes %" PRIi64 "-%" PRIi64 "/%" PRIi64,
offset, (end_offset) ? end_offset : (int64_t)sb.st_size, (int64_t)sb.st_size);
if ((ret < 0) || (ret >= sizeof(buf)))
DPRINTF(E_LOG, L_HTTPD, "Content-Range too large for buffer, dropping\n");
else
evhttp_add_header(req->output_headers, "Content-Range", buf);
ret = snprintf(buf, sizeof(buf), "%ld", ((end_offset) ? end_offset + 1 : (long)sb.st_size) - offset);
ret = snprintf(buf, sizeof(buf), "%" PRIi64, ((end_offset) ? end_offset + 1 : (int64_t)sb.st_size) - offset);
if ((ret < 0) || (ret >= sizeof(buf)))
DPRINTF(E_LOG, L_HTTPD, "Content-Length too large for buffer, dropping\n");
else

View File

@ -408,7 +408,7 @@ daap_session_find(struct evhttp_request *req, struct evkeyvalq *query, struct ev
goto invalid;
}
ret = safe_atoi(param, &needle.id);
ret = safe_atoi32(param, &needle.id);
if (ret < 0)
goto invalid;
@ -492,14 +492,14 @@ dmap_find_field(uint32_t hash)
static void
dmap_add_field(struct evbuffer *evbuf, struct dmap_field_map *dfm, char *strval, int intval)
{
int val;
int32_t val;
int ret;
val = intval;
if ((dfm->type != DMAP_TYPE_STRING) && (val == 0) && strval)
{
ret = safe_atoi(strval, &val);
ret = safe_atoi32(strval, &val);
if (ret < 0)
val = 0;
}
@ -560,7 +560,7 @@ get_query_params(struct evkeyvalq *query, struct query_params *qp)
DPRINTF(E_LOG, L_DAAP, "Unsupported index range: %s\n", param);
else
{
ret = safe_atoi(param, &low);
ret = safe_atoi32(param, &low);
if (ret < 0)
DPRINTF(E_LOG, L_DAAP, "Could not parse index range: %s\n", param);
else
@ -573,7 +573,7 @@ get_query_params(struct evkeyvalq *query, struct query_params *qp)
ptr++;
if (*ptr != '\0') /* low-high */
{
ret = safe_atoi(ptr, &high);
ret = safe_atoi32(ptr, &high);
if (ret < 0)
DPRINTF(E_LOG, L_DAAP, "Could not parse high index in range: %s\n", param);
}
@ -885,7 +885,7 @@ daap_reply_update(struct evhttp_request *req, struct evbuffer *evbuf, char **uri
return;
}
ret = safe_atoi(param, &reqd_rev);
ret = safe_atoi32(param, &reqd_rev);
if (ret < 0)
{
DPRINTF(E_LOG, L_DAAP, "Parameter revision-number not an integer\n");
@ -1013,7 +1013,7 @@ daap_reply_songlist_generic(struct evhttp_request *req, struct evbuffer *evbuf,
int want_mikd;
int want_asdk;
int oom;
int val;
int32_t val;
int i;
int ret;
@ -1209,7 +1209,7 @@ daap_reply_songlist_generic(struct evhttp_request *req, struct evbuffer *evbuf,
case dbmfi_offsetof(bitrate):
val = 0;
ret = safe_atoi(dbmfi.samplerate, &val);
ret = safe_atoi32(dbmfi.samplerate, &val);
if ((ret < 0) || (val == 0))
val = 1411;
else
@ -1231,7 +1231,7 @@ daap_reply_songlist_generic(struct evhttp_request *req, struct evbuffer *evbuf,
if (*strval && (dfm->type != DMAP_TYPE_STRING))
{
ret = safe_atoi(*strval, &val);
ret = safe_atoi32(*strval, &val);
if (ret < 0)
val = 0;
}
@ -1255,14 +1255,14 @@ daap_reply_songlist_generic(struct evhttp_request *req, struct evbuffer *evbuf,
if (want_mikd)
{
/* dmap.itemkind must come first */
ret = safe_atoi(dbmfi.item_kind, &val);
ret = safe_atoi32(dbmfi.item_kind, &val);
if (ret < 0)
val = 2; /* music by default */
dmap_add_char(songlist, "mikd", val);
}
if (want_asdk)
{
ret = safe_atoi(dbmfi.data_kind, &val);
ret = safe_atoi32(dbmfi.data_kind, &val);
if (ret < 0)
val = 0;
dmap_add_char(songlist, "asdk", val);
@ -1365,7 +1365,7 @@ daap_reply_plsonglist(struct evhttp_request *req, struct evbuffer *evbuf, char *
if (!s)
return;
ret = safe_atoi(uri[3], &playlist);
ret = safe_atoi32(uri[3], &playlist);
if (ret < 0)
{
dmap_send_error(req, "apso", "Invalid playlist ID");
@ -1391,7 +1391,7 @@ daap_reply_playlists(struct evhttp_request *req, struct evbuffer *evbuf, char **
int nmeta;
int npls;
int oom;
int val;
int32_t val;
int i;
int ret;
@ -1491,17 +1491,17 @@ daap_reply_playlists(struct evhttp_request *req, struct evbuffer *evbuf, char **
if (meta[i] == 0x670fc55e)
{
val = 0;
ret = safe_atoi(dbpli.type, &val);
ret = safe_atoi32(dbpli.type, &val);
if ((ret == 0) && (val == PL_SMART))
{
val = 1;
ret = safe_atoi(dbpli.id, &val);
ret = safe_atoi32(dbpli.id, &val);
if ((ret == 0) && (val != 1))
{
int aePS = 0;
int32_t aePS = 0;
dmap_add_char(playlist, "aeSP", 1);
ret = safe_atoi(dbpli.special_id, &aePS);
ret = safe_atoi32(dbpli.special_id, &aePS);
if ((ret == 0) && (aePS > 0))
dmap_add_char(playlist, "aePS", aePS);
}
@ -1533,13 +1533,13 @@ daap_reply_playlists(struct evhttp_request *req, struct evbuffer *evbuf, char **
/* Item count (mimc) */
val = 0;
ret = safe_atoi(dbpli.items, &val);
ret = safe_atoi32(dbpli.items, &val);
if ((ret == 0) && (val > 0))
dmap_add_int(playlist, "mimc", val);
/* Base playlist (abpl), id = 1 */
val = 0;
ret = safe_atoi(dbpli.id, &val);
ret = safe_atoi32(dbpli.id, &val);
if ((ret == 0) && (val == 1))
dmap_add_char(playlist, "abpl", 1);
@ -1631,7 +1631,8 @@ daap_reply_groups(struct evhttp_request *req, struct evbuffer *evbuf, char **uri
int nmeta;
int ngrp;
int oom;
int val;
int32_t val;
int64_t val64;
int i;
int ret;
char *tag;
@ -1748,21 +1749,20 @@ daap_reply_groups(struct evhttp_request *req, struct evbuffer *evbuf, char **uri
continue;
/* Special handling for persistentid (mper)
* Correctly handle a ulonglong.
* Correctly handle a DMAP long value (64bit)
*/
if (strcmp(dfm->tag, "mper") == 0)
{
unsigned long long ull = 0;
if (*strval)
{
ret = safe_atoull(*strval, &ull);
ret = safe_atoi64(*strval, &val64);
if (ret < 0)
ull = 0;
val64 = 0;
}
dmap_add_long(group, dfm->tag, ull);
dmap_add_long(group, dfm->tag, val64);
DPRINTF(E_DBG, L_DAAP, "Done with ULL meta tag %s (%llu) \n", dfm->desc, ull);
DPRINTF(E_DBG, L_DAAP, "Done with LONG meta tag %s (%" PRIi64 ") \n", dfm->desc, val64);
continue;
}
@ -1773,7 +1773,7 @@ daap_reply_groups(struct evhttp_request *req, struct evbuffer *evbuf, char **uri
/* Item count, always added (mimc) */
val = 0;
ret = safe_atoi(dbgri.itemcount, &val);
ret = safe_atoi32(dbgri.itemcount, &val);
if ((ret == 0) && (val > 0))
dmap_add_int(group, "mimc", val);
@ -2012,7 +2012,7 @@ daap_stream(struct evhttp_request *req, struct evbuffer *evbuf, char **uri, stru
if (!s)
return;
ret = safe_atoi(uri[3], &id);
ret = safe_atoi32(uri[3], &id);
if (ret < 0)
evhttp_send_error(req, HTTP_BADREQUEST, "Bad Request");
else

View File

@ -262,7 +262,7 @@ dacp_reply_playstatusupdate(struct evhttp_request *req, struct evbuffer *evbuf,
return;
}
ret = safe_atoi(param, &reqd_rev);
ret = safe_atoi32(param, &reqd_rev);
if (ret < 0)
{
DPRINTF(E_LOG, L_DACP, "Parameter revision-number not an integer\n");

View File

@ -177,7 +177,7 @@ get_query_params(struct evhttp_request *req, struct evkeyvalq *query, struct que
param = evhttp_find_header(query, "offset");
if (param)
{
ret = safe_atoi(param, &qp->offset);
ret = safe_atoi32(param, &qp->offset);
if (ret < 0)
{
rsp_send_error(req, "Invalid offset");
@ -189,7 +189,7 @@ get_query_params(struct evhttp_request *req, struct evkeyvalq *query, struct que
param = evhttp_find_header(query, "limit");
if (param)
{
ret = safe_atoi(param, &qp->limit);
ret = safe_atoi32(param, &qp->limit);
if (ret < 0)
{
rsp_send_error(req, "Invalid limit");
@ -453,13 +453,13 @@ rsp_reply_playlist(struct evhttp_request *req, char **uri, struct evkeyvalq *que
int mode;
int records;
int transcode;
int bitrate;
int32_t bitrate;
int i;
int ret;
memset(&qp, 0, sizeof(struct query_params));
ret = safe_atoi(uri[2], &qp.pl_id);
ret = safe_atoi32(uri[2], &qp.pl_id);
if (ret < 0)
{
rsp_send_error(req, "Invalid playlist ID");
@ -564,7 +564,7 @@ rsp_reply_playlist(struct evhttp_request *req, char **uri, struct evkeyvalq *que
case dbmfi_offsetof(bitrate):
bitrate = 0;
ret = safe_atoi(dbmfi.samplerate, &bitrate);
ret = safe_atoi32(dbmfi.samplerate, &bitrate);
if ((ret < 0) || (bitrate == 0))
bitrate = 1411;
else
@ -663,7 +663,7 @@ rsp_reply_browse(struct evhttp_request *req, char **uri, struct evkeyvalq *query
return;
}
ret = safe_atoi(uri[2], &qp.pl_id);
ret = safe_atoi32(uri[2], &qp.pl_id);
if (ret < 0)
{
rsp_send_error(req, "Invalid playlist ID");
@ -768,7 +768,7 @@ rsp_stream(struct evhttp_request *req, char **uri, struct evkeyvalq *query)
int id;
int ret;
ret = safe_atoi(uri[2], &id);
ret = safe_atoi32(uri[2], &id);
if (ret < 0)
evhttp_send_error(req, HTTP_BADREQUEST, "Bad Request");
else

View File

@ -475,7 +475,7 @@ main(int argc, char **argv)
break;
case 'd':
ret = safe_atoi(optarg, &option);
ret = safe_atoi32(optarg, &option);
if (ret < 0)
fprintf(stderr, "Error: loglevel must be an integer in '-d %s'\n", optarg);
else

View File

@ -38,7 +38,7 @@
int
safe_atoi(const char *str, int *val)
safe_atoi32(const char *str, int32_t *val)
{
char *end;
long intval;
@ -61,28 +61,28 @@ safe_atoi(const char *str, int *val)
return -1;
}
if (intval > INT_MAX)
if (intval > INT32_MAX)
{
DPRINTF(E_DBG, L_MISC, "Integer value too large (%s)\n", str);
return -1;
}
*val = (int)intval;
*val = (int32_t)intval;
return 0;
}
int
safe_atol(const char *str, long *val)
safe_atoi64(const char *str, int64_t *val)
{
char *end;
long intval;
long long intval;
errno = 0;
intval = strtol(str, &end, 10);
intval = strtoll(str, &end, 10);
if (((errno == ERANGE) && ((intval == LONG_MAX) || (intval == LONG_MIN)))
if (((errno == ERANGE) && ((intval == LLONG_MAX) || (intval == LLONG_MIN)))
|| ((errno != 0) && (intval == 0)))
{
DPRINTF(E_DBG, L_MISC, "Invalid integer in string (%s): %s\n", str, strerror(errno));
@ -97,35 +97,14 @@ safe_atol(const char *str, long *val)
return -1;
}
*val = intval;
return 0;
}
int
safe_atoull(const char *str, unsigned long long *val)
{
char *end;
unsigned long long intval;
errno = 0;
intval = strtoull(str, &end, 10);
if (errno)
if (intval > INT64_MAX)
{
DPRINTF(E_DBG, L_MISC, "Invalid unsigned long long integer in string (%s): %s\n", str, strerror(errno));
DPRINTF(E_DBG, L_MISC, "Integer value too large (%s)\n", str);
return -1;
}
if (end == str)
{
DPRINTF(E_DBG, L_MISC, "No unsigned long long integer found in string (%s)\n", str);
return -1;
}
*val = intval;
*val = (int64_t)intval;
return 0;
}

View File

@ -5,13 +5,10 @@
#include <stdint.h>
int
safe_atoi(const char *str, int *val);
safe_atoi32(const char *str, int32_t *val);
int
safe_atol(const char *str, long *val);
int
safe_atoull(const char *str, unsigned long long *val);
safe_atoi64(const char *str, int64_t *val);
char *
m_realpath(const char *pathname);

View File

@ -135,7 +135,7 @@ add_le32(uint8_t *dst, uint32_t val)
}
static void
make_wav_header(struct transcode_ctx *ctx, size_t *est_size)
make_wav_header(struct transcode_ctx *ctx, off_t *est_size)
{
uint32_t samplerate;
uint32_t byte_rate;
@ -310,7 +310,7 @@ transcode(struct transcode_ctx *ctx, struct evbuffer *evbuf, int wanted)
}
struct transcode_ctx *
transcode_setup(struct media_file_info *mfi, size_t *est_size)
transcode_setup(struct media_file_info *mfi, off_t *est_size)
{
struct transcode_ctx *ctx;
int hdr_len;

View File

@ -10,7 +10,7 @@ int
transcode(struct transcode_ctx *ctx, struct evbuffer *evbuf, int wanted);
struct transcode_ctx *
transcode_setup(struct media_file_info *mfi, size_t *est_size);
transcode_setup(struct media_file_info *mfi, off_t *est_size);
void
transcode_cleanup(struct transcode_ctx *ctx);