minor problem in boolean cast

Started by Cary Huangabout 5 years ago2 messages
#1Cary Huang
cary.huang@highgo.ca
1 attachment(s)

Hi

I noticed that when casting a string to boolean value with input 'of' it still cast it to 'f'. I think with 'of', it should give an error because 'off' is the expected candidate. This may not be intended so I made a simple patch to address this. 

```

postgres=# select cast('of' as boolean);

bool

------

f

(1 row)

```

Cary Huang

-------------

HighGo Software Inc. (Canada)

mailto:cary.huang@highgo.ca

http://www.highgo.ca

Attachments:

0001-boolean-type-cast-fix.patchapplication/octet-stream; name=0001-boolean-type-cast-fix.patchDownload
diff --git a/src/backend/utils/adt/bool.c b/src/backend/utils/adt/bool.c
index 340607f936..26f9ea04b4 100644
--- a/src/backend/utils/adt/bool.c
+++ b/src/backend/utils/adt/bool.c
@@ -82,7 +82,7 @@ parse_bool_with_len(const char *value, size_t len, bool *result)
 					*result = true;
 				return true;
 			}
-			else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
+			else if (pg_strncasecmp(value, "off", (len > 3 ? len : 3)) == 0)
 			{
 				if (result)
 					*result = false;
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Cary Huang (#1)
Re: minor problem in boolean cast

Cary Huang <cary.huang@highgo.ca> writes:

I noticed that when casting a string to boolean value with input 'of' it still cast it to 'f'. I think with 'of', it should give an error because 'off' is the expected candidate. This may not be intended so I made a simple patch to address this. 

It's absolutely intended, and documented:

https://www.postgresql.org/docs/devel/datatype-boolean.html

Note the bit about "Unique prefixes of these strings are also accepted".

The code comment just above parse_bool() says the same.

regards, tom lane