<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>XKCD &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/xkcd/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Wed, 09 Dec 2020 00:01:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.7</generator>
	<item>
		<title>Avoiding SQL Injection</title>
		<link>https://blog.xojo.com/2016/02/18/avoiding-sql-injection/</link>
					<comments>https://blog.xojo.com/2016/02/18/avoiding-sql-injection/#comments</comments>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Thu, 18 Feb 2016 00:00:00 +0000</pubDate>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[XKCD]]></category>
		<guid isPermaLink="false">http://blogtemp.xojo.com/2016/02/18/avoiding-sql-injection/</guid>

					<description><![CDATA[An SQL injection can allow unintended database access - here's how a SQL Injection works and what you can do to avoid it.]]></description>
										<content:encoded><![CDATA[<p>Maybe you&#8217;ve heard the term <a href="https://en.wikipedia.org/wiki/SQL_injection" target="_blank" rel="noopener noreferrer">SQL injection</a> and how it can allow unintended database access. Here&#8217;s how a SQL Injection works and what you can do to avoid it.</p>
<p><span id="more-236"></span></p>
<p>Generally, you work with databases by sending SQL commands. In particular the SELECT command is used to get data from a database. These SELECT statements are simply built out of text. This is normally not a problem, but it can be when you start to concatenate text to build an SQL statement.</p>
<p>In particular, the risk occurs when you build an SQL statement with user-supplied input. For example, say you prompt the user for some information to filter (it doesn&#8217;t matter what) and then create an SQL statement by concatenating what they typed. You might have code that looks like this:</p>
<pre>Dim sql As String
sql = "SELECT * FROM Tasks WHERE UserName = 'Paul' AND Title = '" + userValue " + "'"</pre>
<p>If the user types &#8220;Mow&#8221; as the value then the SQL looks like this:</p>
<pre>SELECT * FROM Tasks WHERE UserName = 'Paul' AND Title = 'Mow'</pre>
<p>But what if the user types something a bit more nefarious, such as &#8220;Mow&#8217; OR 1;&#8221;? This now results in SQL that looks like this:</p>
<pre>SELECT * FROM Tasks WHERE UserName = 'Paul' AND Title = 'Mow' OR 1;'</pre>
<p>Now there are two SQL statements. The first one returns all the data in the table (because of the &#8220;OR 1&#8221;) and the second statement (just a &#8216;) is likely just ignored. So now your app is displaying lots of data that the user was not meant to see (perhaps data from other users in this case).</p>
<p>This is called SQL Injection because the user was able to inject their own SQL into your query, all because you were using concatenation.</p>
<p><a href="https://xkcd.com/license.html"><img decoding="async" class="aligncenter" title="" src="https://blog.xojo.com/wp-content/uploads/2016/02/exploits_of_a_mom.png" /></a></p>
<p>So how do you avoid this? Instead of concatenating, you use the databases built-in ability to create <a href="https://en.wikipedia.org/wiki/Prepared_statement">prepared statements</a> by binding values to parameters. For Xojo, you use the appropriate PreparedStatement class for your database. In the case of SQLite, you would use <a href="http://developer.xojo.com/sqlitepreparedstatement">SQLitePreparedStatement</a>.</p>
<p>To start you first declare the SQL to have parameters:</p>
<pre>Dim sql As String
sql = "SELECT * FROM Tasks WHERE UserName = 'Paul' AND TITLE = ?"</pre>
<p>Then you create a prepared statement, bind the value and send it to the database:</p>
<pre>Dim ps As SQLitePreparedStatement
ps = MyDB.Prepare(sql)
ps.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
ps.Bind(0, userValue)
Dim rs As RecordSet
rs = ps.SQLSelect</pre>
<p><strong>Update (October 10, 2019)</strong>:</p>
<p>If you&#8217;re using API 2.0 in 2019r2 or later, the above code is even simper:</p>
<pre>Var results As RowSet = MyDB.SelectSQL(sql, userValue)</pre>
<p>Because the value is treated as a parameter, if evildoers again try to inject their own SQL, the resulting SQL sent to the database is actually equivalent to this:</p>
<pre>SELECT * FROM Tasks WHERE UserName = 'Paul' AND TITLE = 'Mow'' OR 1;'"</pre>
<p>This is a valid SQL statement that will simply return no rows.</p>
<p>Any apps that embed user input into SQL statements should always use prepared SQL statements. Do not think you can just write your own code to sanitize user input as this still poses a risk.</p>
<p>And there are also reasons to use prepared statements even when SQL injection is not a concern: some databases cache the parsing of the database command, which can result in some performance improvements if the database command is used repeatedly in the app.</p>
<p>Reference: <a href="http://developer.xojo.com/userguide/sql-injection">SQL Injection in the Xojo User Guide</a></p>
<p><!--HubSpot Call-to-Action Code --> <span id="hs-cta-wrapper-de1ba131-3831-49df-90a1-b9efde8e17d2" class="hs-cta-wrapper"> <span id="hs-cta-de1ba131-3831-49df-90a1-b9efde8e17d2" class="hs-cta-node hs-cta-de1ba131-3831-49df-90a1-b9efde8e17d2"><br />
<!-- [if lte IE 8]>


<div id="hs-cta-ie-element"></div>


<![endif]--> <a href="http://blog.xojo.com/2015/12/21/speed-up-sqlite-with-write-ahead-logging/" target="_blank" rel="noopener noreferrer"><img decoding="async" id="hs-cta-img-de1ba131-3831-49df-90a1-b9efde8e17d2" class="hs-cta-img aligncenter" style="border-width: 0px; margin: 0 auto; display: block; margin-top: 20px; margin-bottom: 20px;" src="https://blog.xojo.com/wp-content/uploads/2016/01/de1ba131-3831-49df-90a1-b9efde8e17d2.png" alt="Code Tip: Speed up SQLite with Write-Ahead Logging" width="393" height="72" align="middle" /></a></span></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.xojo.com/2016/02/18/avoiding-sql-injection/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>But it&#8217;s not precise: A floating point values primer</title>
		<link>https://blog.xojo.com/2015/12/10/but-its-not-precise-a-floating-point-values-primer/</link>
		
		<dc:creator><![CDATA[Norman Palardy]]></dc:creator>
		<pubDate>Thu, 10 Dec 2015 00:00:00 +0000</pubDate>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[XKCD]]></category>
		<guid isPermaLink="false">http://blogtemp.xojo.com/2015/12/10/but-its-not-precise-a-floating-point-values-primer/</guid>

					<description><![CDATA[Because digital computers are based entirely on binary, they use powers of 2 and this is, by the very design of floating point values, not going to be precise on any CPU using the IEEE formats]]></description>
										<content:encoded><![CDATA[<p>From time to time we see the issue raised where floating point values are not exact like we can write in the code editor or on paper. Usually the confusion or complaint is worded like &#8220;I can&#8217;t get my double value to be precise like in the string&#8221; or &#8220;It&#8217;s not the same as I get doing it by hand&#8221;.</p>
<p><span id="more-257"></span></p>
<p>Unfortunately, because digital computers are based entirely on <a href="https://en.wikipedia.org/wiki/Binary_number">binary</a>, they use powers of 2 and this is, by the very design of floating point values, not going to be precise on <em>any</em> CPU using the <a href="https://en.wikipedia.org/wiki/IEEE_floating_point" target="_blank" rel="noopener noreferrer">IEEE formats</a>.</p>
<p>Let me explain: when you and I do math by hand we all have internalized the rules that decimals are stated as positional values:</p>
<p>1 = 10<sup>0</sup><br />
10 = 10<sup>1</sup></p>
<p>And that the fraction parts are also powers of 10:</p>
<p>.1 = 1 / 10 = 1 / (10<sup>1</sup>)<br />
.01 = 1 / 100 = 1 / (10<sup>2</sup>)<br />
.001 = 1 / 1000 = 1 / (10<sup>3</sup>)</p>
<p>We could write 138 as 1*(10<sup>2</sup>) + 3*(10<sup>1</sup>) + 8*(10<sup>0</sup>)</p>
<p>On our computers things are similar but instead of powers of 10, they use power of 2:</p>
<p>1 = 2<sup>0</sup> (or as a binary literal in Xojo as &amp;b01)<br />
2 = 2<sup>1</sup> (or as a binary literal in Xojo as &amp;b10)<br />
3 = 2<sup>0</sup> + 2<sup>1</sup> (or as a binary literal in Xojo as &amp;b11)</p>
<p>Floating point values are also the sum of powers of 2. They use the same powers of 2 for the whole number portion and, at their simplest, sums of powers of 2 that are increasingly small. There are some optimizations to this that CPU&#8217;s use to normalize things, but the basics are still based on this notation:</p>
<p>.5 = 1 / (2<sup>1</sup>)<br />
.25 = 1 / (2<sup>2</sup>)<br />
.125 = 1/ (2<sup>3</sup>)</p>
<p>So when you want 1.5 that&#8217;s easy: (2^0) + (1 / (2<sup>1</sup>)) or 1 + .5 = 1.5<br />
Since 0.5 can be represented exactly using binary there&#8217;s no issue. But trying to represent a number like 0.3 will result in what is a common complaint:</p>
<p>(1/ 2<sup>2</sup>) + (1 / 2<sup>4</sup>) + (1/2<sup>5</sup>) + (1/2<sup>8</sup>) + (1/2<sup>9</sup>) + (1/2<sup>12</sup>) + (1/2<sup>13</sup>)</p>
<p>This evaluates to:</p>
<p>.25 + .0125 + .03125 + .00390625 + .001953125 + .0002441402625 + 0.0001220703125 + 0.0000152587890625</p>
<p>The sum of which is:</p>
<p>0.29999084436406</p>
<p>And you can carry this on as long as you&#8217;d like and sum successive powers of 2, and while you can approximate 0.3 you never sum to exactly 0.3. There is no sum of successive powers of 2 that totals precisely to this fractional value, so it&#8217;s approximated as close as possible using those fractions.</p>
<p>Therein lies the problem. In a digital computer there are only so many bits used to represent a floating point values and they will be close but not precise.</p>
<p>There are <a href="http://www.amazon.ca/Numerical-Recipes-Art-Scientific-Computing/dp/818561816X/ref=sr_1_2?ie=UTF8&amp;qid=1449001137&amp;sr=8-2&amp;keywords=numerical+recipes+in+c" target="_blank" rel="noopener noreferrer">entire</a> <a href="http://www.amazon.com/Numerical-Recipes-Routines-Examples-Edition/dp/0521406897" target="_blank" rel="noopener noreferrer">books</a> dedicated to the subject, should you want to learn more.</p>
<p><a href="https://www.explainxkcd.com/wiki/index.php/217:_e_to_the_pi_Minus_pi"><img fetchpriority="high" decoding="async" class="aligncenter" title="" src="https://blog.xojo.com/wp-content/uploads/2015/12/e_to_the_pi_minus_pi.png" width="578" height="218" /></a></p>
<p><a href="https://xkcd.com/license.html">(xkcd license) </a></p>
<p>Want more geek talk? Subscribe to the XojoTalk Podcast!</p>
<p><!--HubSpot Call-to-Action Code --> <span id="hs-cta-wrapper-32eb9715-5c1c-45a4-9f37-717ffc906d8e" class="hs-cta-wrapper"> <span id="hs-cta-32eb9715-5c1c-45a4-9f37-717ffc906d8e" class="hs-cta-node hs-cta-32eb9715-5c1c-45a4-9f37-717ffc906d8e"><br />
<!-- [if lte IE 8]>


<div id="hs-cta-ie-element"></div>


<![endif]--> <a href="http://feeds.feedburner.com/xojotalk?" target="_blank" rel="noopener noreferrer"><img decoding="async" id="hs-cta-img-32eb9715-5c1c-45a4-9f37-717ffc906d8e" class="hs-cta-img aligncenter" style="border-width: 0px; margin: 0 auto; display: block; margin-top: 20px; margin-bottom: 20px;" src="https://blog.xojo.com/wp-content/uploads/2014/09/32eb9715-5c1c-45a4-9f37-717ffc906d8e.png" alt="Subscribe Podcast" width="622" height="107" align="middle" /></a> </span><script src="https://js.hscta.net/cta/current.js" charset="utf-8">// <![CDATA[
<script type="text/javascript"><![CDATA[ hbspt.cta.load(608515, '32eb9715-5c1c-45a4-9f37-717ffc906d8e', {}); // ]]&gt;</script></span><br />
<!-- end HubSpot Call-to-Action Code --></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
