Skip to content

Commit 2fa3302

Browse files
author
Thomas G. Lockhart
committed
Use limits.h for INT, SHRT, and SCHAR min and max values rather than
hardcoded values.
1 parent 174f984 commit 2fa3302

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/backend/utils/adt/numutils.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numutils.c,v 1.16 1997/09/18 20:22:15 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numutils.c,v 1.17 1997/11/17 16:26:27 thomas Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
1717
#include <stdio.h> /* for sprintf() */
1818
#include <errno.h>
1919
#include <math.h>
20+
#ifdef HAVE_LIMITS
21+
#include <limits.h>
22+
#endif
2023
#include "postgres.h"
2124
#include "utils/builtins.h" /* where the declarations go */
2225
#ifndef HAVE_MEMMOVE
@@ -26,6 +29,25 @@
2629
#endif
2730
#include <port-protos.h> /* ecvt(), fcvt() */
2831

32+
#ifndef INT_MAX
33+
#define INT_MAX (0x7FFFFFFFL)
34+
#endif
35+
#ifndef INT_MIN
36+
#define INT_MIN (-0x80000000L)
37+
#endif
38+
#ifndef SHRT_MAX
39+
#define SHRT_MAX (0x7FFF)
40+
#endif
41+
#ifndef SHRT_MIN
42+
#define SHRT_MIN (-0x8000)
43+
#endif
44+
#ifndef SCHAR_MAX
45+
#define SCHAR_MAX (0x7F)
46+
#endif
47+
#ifndef SCHAR_MIN
48+
#define SCHAR_MIN (-0x80)
49+
#endif
50+
2951
int32
3052
pg_atoi(char *s, int size, int c)
3153
{
@@ -46,37 +68,37 @@ pg_atoi(char *s, int size, int c)
4668
case sizeof(int32):
4769
#ifdef HAS_LONG_LONG
4870
/* won't get ERANGE on these with 64-bit longs... */
49-
if (l < -0x80000000L)
71+
if (l < INT_MIN)
5072
{
5173
errno = ERANGE;
5274
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
5375
}
54-
if (l > 0x7fffffffL)
76+
if (l > INT_MAX)
5577
{
5678
errno = ERANGE;
5779
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
5880
}
5981
#endif /* HAS_LONG_LONG */
6082
break;
6183
case sizeof(int16):
62-
if (l < -0x8000)
84+
if (l < SHRT_MIN)
6385
{
6486
errno = ERANGE;
6587
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
6688
}
67-
if (l > 0x7fff)
89+
if (l > SHRT_MAX)
6890
{
6991
errno = ERANGE;
7092
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
7193
}
7294
break;
7395
case sizeof(int8):
74-
if (l < -0x80)
96+
if (l < SCHAR_MIN)
7597
{
7698
errno = ERANGE;
7799
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
78100
}
79-
if (l > 0x7f)
101+
if (l > SCHAR_MAX)
80102
{
81103
errno = ERANGE;
82104
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy