Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable cppcheck and fix related mostly false-positive warnings #496

Merged
merged 1 commit into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/PJ_healpix.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ static XY combine_caps(double x, double y, int north_square, int south_square,
int inverse) {
XY xy;
double v[2];
double a[2];
double c[2];
double vector[2];
double v_min_c[2];
Expand All @@ -467,8 +466,7 @@ static XY combine_caps(double x, double y, int north_square, int south_square,
if (inverse == 0) {
/* Rotate (x, y) about its polar cap tip and then translate it to
north_square or south_square. */
a[0] = -3*M_FORTPI + pole*M_HALFPI;
a[1] = M_HALFPI;

if (capmap.region == north) {
pole = north_square;
tmpRot = rot[get_rotate_index(capmap.cn - pole)];
Expand All @@ -479,8 +477,7 @@ static XY combine_caps(double x, double y, int north_square, int south_square,
} else {
/* Inverse function.
Unrotate (x, y) and then translate it back. */
a[0] = -3*M_FORTPI + capmap.cn*M_HALFPI;
a[1] = M_HALFPI;

/* disassemble */
if (capmap.region == north) {
pole = north_square;
Expand All @@ -493,7 +490,14 @@ static XY combine_caps(double x, double y, int north_square, int south_square,

vector_sub(v, c, v_min_c);
dot_product(tmpRot, v_min_c, ret_dot);
vector_add(ret_dot, a, vector);
{
double a[2];
/* Workaround cppcheck git issue */
double* pa = a;
pa[0] = -3*M_FORTPI + ((inverse == 0) ? 0 : capmap.cn) *M_HALFPI;
pa[1] = M_HALFPI;
vector_add(ret_dot, a, vector);
}

xy.x = vector[0];
xy.y = vector[1];
Expand Down
6 changes: 5 additions & 1 deletion src/PJ_labrd.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ static XY e_forward (LP lp, PJ *P) { /* Ellipsoidal, forward */
static LP e_inverse (XY xy, PJ *P) { /* Ellipsoidal, inverse */
LP lp = {0.0,0.0};
struct pj_opaque *Q = P->opaque;
double x2, y2, V1, V2, V3, V4, t, t2, ps, pe, tpe, s;
/* t = 0.0 optimization is to avoid a false positive cppcheck warning */
/* (cppcheck git beaf29c15867984aa3c2a15cf15bd7576ccde2b3). Might no */
/* longer be necessary with later versions. */
double x2, y2, V1, V2, V3, V4, t = 0.0, t2, ps, pe, tpe, s;
double I7, I8, I9, I10, I11, d, Re;
int i;

Expand All @@ -60,6 +63,7 @@ static LP e_inverse (XY xy, PJ *P) { /* Ellipsoidal, inverse */
xy.y += Q->Cb * V1 - Q->Ca * V2 - Q->Cd * V3 + Q->Cc * V4;
ps = Q->p0s + xy.y / Q->kRg;
pe = ps + P->phi0 - Q->p0s;

for ( i = 20; i; --i) {
V1 = Q->A * log(tan(M_FORTPI + .5 * pe));
tpe = P->e * sin(pe);
Expand Down
15 changes: 11 additions & 4 deletions src/PJ_lsat.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,30 @@ static XY e_forward (LP lp, PJ *P) { /* Ellipsoidal, forward */
struct pj_opaque *Q = P->opaque;
int l, nn;
double lamt = 0.0, xlam, sdsq, c, d, s, lamdp = 0.0, phidp, lampp, tanph;
double lamtp, cl, sd, sp, fac, sav, tanphi;
double lamtp, cl, sd, sp, sav, tanphi;

if (lp.phi > M_HALFPI)
lp.phi = M_HALFPI;
else if (lp.phi < -M_HALFPI)
lp.phi = -M_HALFPI;

lampp = lp.phi >= 0. ? M_HALFPI : M_PI_HALFPI;
if (lp.phi >= 0. )
lampp = M_HALFPI;
else
lampp = M_PI_HALFPI;
tanphi = tan(lp.phi);
for (nn = 0;;) {
double fac;
sav = lampp;
lamtp = lp.lam + Q->p22 * lampp;
cl = cos(lamtp);
if (fabs(cl) < TOL)
lamtp -= TOL;
fac = lampp - sin(lampp) * (cl < 0. ? -M_HALFPI : M_HALFPI);
for (l = 50; l; --l) {
if( cl < 0 )
fac = lampp + sin(lampp) * M_HALFPI;
else
fac = lampp - sin(lampp) * M_HALFPI;
for (l = 50; l >= 0; --l) {
lamt = lp.lam + Q->p22 * sav;
c = cos(lamt);
if (fabs(c) < TOL)
Expand Down
13 changes: 10 additions & 3 deletions src/PJ_misrsom.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,28 @@ static XY e_forward (LP lp, PJ *P) { /* Ellipsoidal, forward */
struct pj_opaque *Q = P->opaque;
int l, nn;
double lamt = 0.0, xlam, sdsq, c, d, s, lamdp = 0.0, phidp, lampp, tanph;
double lamtp, cl, sd, sp, fac, sav, tanphi;
double lamtp, cl, sd, sp, sav, tanphi;

if (lp.phi > M_HALFPI)
lp.phi = M_HALFPI;
else if (lp.phi < -M_HALFPI)
lp.phi = -M_HALFPI;
lampp = lp.phi >= 0. ? M_HALFPI : M_PI_HALFPI;
if (lp.phi >= 0. )
lampp = M_HALFPI;
else
lampp = M_PI_HALFPI;
tanphi = tan(lp.phi);
for (nn = 0;;) {
double fac;
sav = lampp;
lamtp = lp.lam + Q->p22 * lampp;
cl = cos(lamtp);
if (fabs(cl) < TOL)
lamtp -= TOL;
fac = lampp - sin(lampp) * (cl < 0. ? -M_HALFPI : M_HALFPI);
if( cl < 0 )
fac = lampp + sin(lampp) * M_HALFPI;
else
fac = lampp - sin(lampp) * M_HALFPI;
for (l = 50; l; --l) {
lamt = lp.lam + Q->p22 * sav;
if (fabs(c = cos(lamt)) < TOL)
Expand Down
3 changes: 2 additions & 1 deletion src/PJ_mod_ster.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static LP e_inverse (XY xy, PJ *P) { /* Ellipsoidal, inverse */
struct pj_opaque *Q = P->opaque;
int nn;
COMPLEX p, fxy, fpxy, dp;
double den, rh = 0.0, z, sinz = 0.0, cosz = 0.0, chi, phi = 0.0, dphi, esphi;
double den, rh = 0.0, z, sinz = 0.0, cosz = 0.0, chi, phi = 0.0, esphi;

p.r = xy.x;
p.i = xy.y;
Expand Down Expand Up @@ -79,6 +79,7 @@ static LP e_inverse (XY xy, PJ *P) { /* Ellipsoidal, inverse */
chi = aasin(P->ctx, cosz * Q->schio + p.i * sinz * Q->cchio / rh);
phi = chi;
for (nn = 20; nn ;--nn) {
double dphi;
esphi = P->e * sin(phi);
dphi = 2. * atan(tan((M_HALFPI + chi) * .5) *
pow((1. + esphi) / (1. - esphi), P->e * .5)) - M_HALFPI - phi;
Expand Down
2 changes: 2 additions & 0 deletions src/PJ_oea.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ PJ *PROJECTION(oea) {

if (((Q->n = pj_param(P->ctx, P->params, "dn").f) <= 0.) ||
((Q->m = pj_param(P->ctx, P->params, "dm").f) <= 0.))
{
E_ERROR(-39)
}
else {
Q->theta = pj_param(P->ctx, P->params, "rtheta").f;
Q->sp0 = sin(P->phi0);
Expand Down
15 changes: 10 additions & 5 deletions src/emess.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ emess(int code, char *fmt, ...) {
putc('\n', stderr);
/* if |code|==2, print errno code data */
if (code == 2 || code == -2)
(void)fprintf(stderr, "Sys errno: %d: %s\n",
errno,
{
int my_errno = errno;
#ifdef HAVE_STRERROR
strerror(errno));
#else
"<system mess. texts unavail.>");
const char* my_strerror = strerror(my_errno);
#endif
#ifndef HAVE_STRERROR
const char* my_strerror = "<system mess. texts unavail.>";
#endif
(void)fprintf(stderr, "Sys errno: %d: %s\n",
my_errno, my_strerror);
}

/* post remainder of call data */
(void)vfprintf(stderr,fmt,args);
va_end(args);
Expand Down
5 changes: 4 additions & 1 deletion src/geodesic.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ static void Init() {
tolb = tol0 * tol2;
xthresh = 1000 * tol2;
degree = pi/180;
NaN = sqrt(-1.0);
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be changed upstream?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cffk See above. One of the versions of cppcheck complains that sqrt(-1.0) is undefined, but later no longer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll backport these changes. Also I note that Visual Studio gives two warnings
that S12 "may" be uninitialized. It's wrong! but I initialize it to 0 in those places together
with a comment /* Initialize S12 to stop Visual Studio warning */

double minus1 = -1.0;
NaN = sqrt(minus1);
}
init = 1;
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/geodtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,13 @@ static int GeodSolve12() {

static int GeodSolve14() {
/* Check fix for inverse ignoring lon12 = nan */
double azi1, azi2, s12, nan = sqrt(-1.0);
double azi1, azi2, s12, nan;
struct geod_geodesic g;
int result = 0;
{
double minus1 = -1.0;
nan = sqrt(minus1);
}
geod_init(&g, wgs84_a, wgs84_f);
geod_inverse(&g, 0, 0, 1, nan, &s12, &azi1, &azi2);
result += azi1 == azi1 ? 1 : 0;
Expand Down Expand Up @@ -473,9 +477,13 @@ static int GeodSolve33() {
static int GeodSolve55() {
/* Check fix for nan + point on equator or pole not returning all nans in
* Geodesic::Inverse, found 2015-09-23. */
double azi1, azi2, s12, nan = sqrt(-1.0);
double azi1, azi2, s12, nan;
struct geod_geodesic g;
int result = 0;
{
double minus1 = -1.0;
nan = sqrt(minus1);
}
geod_init(&g, wgs84_a, wgs84_f);
geod_inverse(&g, nan, 0, 0, 90, &s12, &azi1, &azi2);
result += azi1 == azi1 ? 1 : 0;
Expand Down
5 changes: 4 additions & 1 deletion src/pj_datum_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ int pj_datum_set(projCtx ctx, paralist *pl, PJ *projdef)
char entry[100];

strcpy( entry, "ellps=" );
strncat( entry, pj_datums[i].ellipse_id, 80 );
strncpy( entry + strlen(entry), pj_datums[i].ellipse_id,
sizeof(entry) - 1 - strlen(entry) );
entry[ sizeof(entry) - 1 ] = '\0';

curr = curr->next = pj_mkparam(entry);
}

Expand Down
15 changes: 11 additions & 4 deletions src/pj_gc_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,21 @@ PJ_GridCatalog *pj_gc_readcatalog( projCtx ctx, const char *catalog_name )

if( catalog->entry_count == entry_max )
{
PJ_GridCatalogEntry* new_entries;
entry_max = entry_max * 2;
catalog->entries = (PJ_GridCatalogEntry *)
new_entries = (PJ_GridCatalogEntry *)
realloc(catalog->entries,
entry_max * sizeof(PJ_GridCatalogEntry));
if (catalog->entries == NULL )
if (new_entries == NULL )
{
int i;
for( i = 0; i < catalog->entry_count; i++ )
free( catalog->entries[i].definition );
free( catalog->catalog_name );
free( catalog );
return NULL;
}
catalog->entries = new_entries;
}
}

Expand Down Expand Up @@ -184,8 +193,6 @@ static int pj_gc_readentry(projCtx ctx, PAFile fid, PJ_GridCatalogEntry *entry)
}
else
{
memset( entry, 0, sizeof(PJ_GridCatalogEntry));

entry->definition = strdup( tokens[0] );
entry->region.ll_long = dmstor_ctx( ctx, tokens[1], NULL );
entry->region.ll_lat = dmstor_ctx( ctx, tokens[2], NULL );
Expand Down
3 changes: 2 additions & 1 deletion src/pj_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ get_init(projCtx ctx, paralist **start, paralist *next, char *name,
paralist *init_items = NULL;
const paralist *orig_next = next;

(void)strncpy(fname, name, MAX_PATH_FILENAME + ID_TAG_MAX + 1);
(void)strncpy(fname, name, sizeof(fname)-2);
fname[sizeof(fname)-2] = '\0';

/*
** Search for file/key pair in cache
Expand Down
10 changes: 7 additions & 3 deletions src/pj_phi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@

double
pj_phi2(projCtx ctx, double ts, double e) {
double eccnth, Phi, con, dphi;
double eccnth, Phi, con;
int i;

eccnth = .5 * e;
Phi = M_HALFPI - 2. * atan (ts);
i = N_ITER;
do {
for(;;) {
double dphi;
con = e * sin (Phi);
dphi = M_HALFPI - 2. * atan (ts * pow((1. - con) /
(1. + con), eccnth)) - Phi;
Phi += dphi;
} while ( fabs(dphi) > TOL && --i);
if( fabs(dphi) > TOL && --i )
continue;
break;
}
if (i <= 0)
pj_ctx_set_errno( ctx, -18 );
return Phi;
Expand Down
10 changes: 8 additions & 2 deletions src/proj_etmerc.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,21 @@ PJ *PROJECTION(utm) {
P->y0 = pj_param (P->ctx, P->params, "bsouth").i ? 10000000. : 0.;
P->x0 = 500000.;
if (pj_param (P->ctx, P->params, "tzone").i) /* zone input ? */
if ((zone = pj_param(P->ctx, P->params, "izone").i) > 0 && zone <= 60)
{
zone = pj_param(P->ctx, P->params, "izone").i;
if (zone > 0 && zone <= 60)
--zone;
else
E_ERROR(-35)
}
else /* nearest central meridian input */
if ((zone = (int)(floor ((adjlon (P->lam0) + M_PI) * 30. / M_PI))) < 0)
{
zone = (int)(floor ((adjlon (P->lam0) + M_PI) * 30. / M_PI));
if (zone < 0)
zone = 0;
else if (zone >= 60)
zone = 59;
}
P->lam0 = (zone + .5) * M_PI / 30. - M_PI;
P->k0 = 0.9996;
P->phi0 = 0.;
Expand Down
10 changes: 10 additions & 0 deletions travis/linux_gcc/before_install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#!/bin/bash

sudo apt-get install -y cppcheck

cppcheck --inline-suppr --template='{file}:{line},{severity},{id},{message}' --enable=all --inconclusive --std=posix -DPJ_SELFTEST=1 src/*.c 2>/tmp/cppcheck.txt

grep "error," /tmp/cppcheck.txt
if [[ $? -eq 0 ]] ; then
echo "cppcheck failed"
exit 1
fi

set -e

./travis/before_install.sh