<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[mysql_real_escape_string für eine zusätzliche Variable einbauen]]></title><description><![CDATA[<p>Hallo community,</p>
<p>es geht darum, dass ich in einem Programm eine bereits vorhandene Variable zur Authentifizierung gegen eine MySQL-Datenbank nutzen möchte. Da diese durch Dritte beschrieben werden kann, muss diese natürlich escaped werden. Allerdings macht mir diese Aufgabe sehr zu schaffen, da ich ansonsten nur in Delphi, PHP und Perl unterwegs bin und mit dem Konzept von Pointern &amp; co leider nicht viel am Hut habe <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";-)"
      alt="😉"
    /><br />
Durchs Googlen habe ich bereits einen Ansatz gefunden, der compiler läuft durch, aber an der AUTH-Stelle bricht es dann einfach ab. Zuerst werde ich die relevanten Ausschnitte des Original-Quellcodes posten und danach das, was ich versucht habe (meine Aenderungen sind in fett im zweiten Teil hervorgehoben!).</p>
<p>Würde mich freuen, wenn jemand dort kurz drübergucken könnte <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Besten Dank,<br />
Error500</p>
<p><strong>ORIGINAL Sourcecode:</strong></p>
<pre><code>static char *parse_select_clause (const char *clause, const char *username,
				  const char *defdomain,
				  const char *service,
				  const char *pass) /* prampec@gmail.com */

{
	char *str;

	static struct var_data vd[]={
		{&quot;local_part&quot;,	NULL,	sizeof(&quot;local_part&quot;),	0},
		{&quot;domain&quot;,		NULL,	sizeof(&quot;domain&quot;),	0},
		{&quot;service&quot;,		NULL,	sizeof(&quot;service&quot;),	0},
		{&quot;password&quot;,	NULL,	sizeof(&quot;password&quot;),	0}, /* prampec@gmail.com */
		{NULL,		NULL,	0,			0}};

	char *l_part;
	char *d_part;

	if (clause == NULL || *clause == '\0' ||
	    !username || *username == '\0')
		return NULL;

	if (!local_and_domain_part_escaped(username, defdomain,
					   &amp;l_part, &amp;d_part))
		return NULL;

	vd[0].value=l_part;
	vd[1].value=d_part;
	vd[2].value     = service;
	vd[3].value     = pass; /* prampec@gmail.com */

	str=parse_string (clause, vd);
	free(l_part);
	free(d_part);
	return str;
}

static char *local_part_escaped(const char *username)
{
	const char *p=strchr(username, '@');
	size_t n=p ? p-username:strlen(username);
	char *buf=malloc(n*2+1);

	if (!buf)
	{
		perror(&quot;malloc&quot;);
		return NULL;
	}

	mysql_real_escape_string(mysql, buf, username, n);
	return buf;
}

static char *domain_part_escaped(const char *username,
				 const char *defdomain)
{
	const char *p=strchr(username, '@');
	size_t n;
	char *buf;

	if (p)
		++p;
	else
		p=defdomain;

	n=strlen(p);

	buf=malloc(n*2+1);

	if (!buf)
	{
		perror(&quot;malloc&quot;);
		return NULL;
	}

	mysql_real_escape_string(mysql, buf, p, n);
	return buf;
}

static int local_and_domain_part_escaped(const char *username,
					 const char *defdomain,
					 char **local_ret,
					 char **domain_ret)
{
	if ((*local_ret=local_part_escaped(username)) == NULL)
		return 0;

	if ((*domain_ret=domain_part_escaped(username, defdomain)) == NULL)
	{
		free(*local_ret);
		return 0;
	}

	return 1;
}
</code></pre>
<p><strong>Mein gepatchter Sourcecode:</strong></p>
<pre><code>static char *parse_select_clause (const char *clause, const char *username,
				  const char *defdomain,
				  const char *service,
				  const char *pass) /* prampec@gmail.com */

{
	char *str;

	static struct var_data vd[]={
		{&quot;local_part&quot;,	NULL,	sizeof(&quot;local_part&quot;),	0},
		{&quot;domain&quot;,		NULL,	sizeof(&quot;domain&quot;),	0},
		{&quot;service&quot;,		NULL,	sizeof(&quot;service&quot;),	0},
		{&quot;password&quot;,	NULL,	sizeof(&quot;password&quot;),	0}, /* prampec@gmail.com */
		{NULL,		NULL,	0,			0}};

	char *l_part;
	char *d_part;
	[B]char *pass_part;[/B]

	if (clause == NULL || *clause == '\0' ||
	    !username || *username == '\0' [B]||
            !pass || *pass == '\0') [/B]
		return NULL;

	if (!local_and_domain_part_escaped(username, 
					[B]pass, [/B]
					defdomain,
					&amp;l_part, &amp;d_part,
					[B]&amp;pass_part [/B]
					))
		return NULL;

	vd[0].value=l_part;
	vd[1].value=d_part;
	vd[2].value     = service;
	[B]vd[3].value     = pass_part;[/B]

	str=parse_string (clause, vd);
	free(l_part);
	free(d_part);
	[B]free(pass_part);[/B]
	return str;
}

static char *local_part_escaped(const char *username)
{
	const char *p=strchr(username, '@');
	size_t n=p ? p-username:strlen(username);
	char *buf=malloc(n*2+1);

	if (!buf)
	{
		perror(&quot;malloc&quot;);
		return NULL;
	}

	mysql_real_escape_string(mysql, buf, username, n);
	return buf;
}

static char *domain_part_escaped(const char *username,
				 const char *defdomain)
{
	const char *p=strchr(username, '@');
	size_t n;
	char *buf;

	if (p)
		++p;
	else
		p=defdomain;

	n=strlen(p);

	buf=malloc(n*2+1);

	if (!buf)
	{
		perror(&quot;malloc&quot;);
		return NULL;
	}

	mysql_real_escape_string(mysql, buf, p, n);
	return buf;
}

[B]
static char *pass_part_escaped(const char *pass)
{
        size_t n;
        char *buf;

        n=strlen(pass);

        buf=malloc(n*2+1);

        if (!buf)
        {
                perror(&quot;malloc&quot;);
                return NULL;
        }

        mysql_real_escape_string(mysql, buf, pass, n);
        return buf;
}
/* 
static char *pass_part_escaped(const char *pass)
{
	char *to = new char[(strlen(pass) * 2) + 1];
	mysql_real_escape_string(mysql, to, pass, strlen(pass));
	return to;

}
*/
[/B]
static int local_and_domain_part_escaped(const char *username,
					 [B]const char *pass,[/B]
					 const char *defdomain,
					 char **local_ret,
					 char **domain_ret,
					 [B]char **pass_ret [/B]
					 )
{
	if ((*local_ret=local_part_escaped(username)) == NULL)
		return 0;

[B]
	if ((*pass_ret=pass_part_escaped(pass)) == NULL)
	{
		free(*local_ret);
		return 0;
  	}
[/B]

	if ((*domain_ret=domain_part_escaped(username, defdomain)) == NULL)
	{
		free(*local_ret);
		[B]free(*pass_ret);[/B]
		return 0;
	}

	return 1;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/265984/mysql_real_escape_string-für-eine-zusätzliche-variable-einbauen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 03 Sep 2026 16:41:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/265984.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 02 May 2010 09:38:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to mysql_real_escape_string für eine zusätzliche Variable einbauen on Sun, 02 May 2010 09:42:28 GMT]]></title><description><![CDATA[<p>Hallo community,</p>
<p>es geht darum, dass ich in einem Programm eine bereits vorhandene Variable zur Authentifizierung gegen eine MySQL-Datenbank nutzen möchte. Da diese durch Dritte beschrieben werden kann, muss diese natürlich escaped werden. Allerdings macht mir diese Aufgabe sehr zu schaffen, da ich ansonsten nur in Delphi, PHP und Perl unterwegs bin und mit dem Konzept von Pointern &amp; co leider nicht viel am Hut habe <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";-)"
      alt="😉"
    /><br />
Durchs Googlen habe ich bereits einen Ansatz gefunden, der compiler läuft durch, aber an der AUTH-Stelle bricht es dann einfach ab. Zuerst werde ich die relevanten Ausschnitte des Original-Quellcodes posten und danach das, was ich versucht habe (meine Aenderungen sind in fett im zweiten Teil hervorgehoben!).</p>
<p>Würde mich freuen, wenn jemand dort kurz drübergucken könnte <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Besten Dank,<br />
Error500</p>
<p><strong>ORIGINAL Sourcecode:</strong></p>
<pre><code>static char *parse_select_clause (const char *clause, const char *username,
				  const char *defdomain,
				  const char *service,
				  const char *pass) /* prampec@gmail.com */

{
	char *str;

	static struct var_data vd[]={
		{&quot;local_part&quot;,	NULL,	sizeof(&quot;local_part&quot;),	0},
		{&quot;domain&quot;,		NULL,	sizeof(&quot;domain&quot;),	0},
		{&quot;service&quot;,		NULL,	sizeof(&quot;service&quot;),	0},
		{&quot;password&quot;,	NULL,	sizeof(&quot;password&quot;),	0}, /* prampec@gmail.com */
		{NULL,		NULL,	0,			0}};

	char *l_part;
	char *d_part;

	if (clause == NULL || *clause == '\0' ||
	    !username || *username == '\0')
		return NULL;

	if (!local_and_domain_part_escaped(username, defdomain,
					   &amp;l_part, &amp;d_part))
		return NULL;

	vd[0].value=l_part;
	vd[1].value=d_part;
	vd[2].value     = service;
	vd[3].value     = pass; /* prampec@gmail.com */

	str=parse_string (clause, vd);
	free(l_part);
	free(d_part);
	return str;
}

static char *local_part_escaped(const char *username)
{
	const char *p=strchr(username, '@');
	size_t n=p ? p-username:strlen(username);
	char *buf=malloc(n*2+1);

	if (!buf)
	{
		perror(&quot;malloc&quot;);
		return NULL;
	}

	mysql_real_escape_string(mysql, buf, username, n);
	return buf;
}

static char *domain_part_escaped(const char *username,
				 const char *defdomain)
{
	const char *p=strchr(username, '@');
	size_t n;
	char *buf;

	if (p)
		++p;
	else
		p=defdomain;

	n=strlen(p);

	buf=malloc(n*2+1);

	if (!buf)
	{
		perror(&quot;malloc&quot;);
		return NULL;
	}

	mysql_real_escape_string(mysql, buf, p, n);
	return buf;
}

static int local_and_domain_part_escaped(const char *username,
					 const char *defdomain,
					 char **local_ret,
					 char **domain_ret)
{
	if ((*local_ret=local_part_escaped(username)) == NULL)
		return 0;

	if ((*domain_ret=domain_part_escaped(username, defdomain)) == NULL)
	{
		free(*local_ret);
		return 0;
	}

	return 1;
}
</code></pre>
<p><strong>Mein gepatchter Sourcecode:</strong></p>
<pre><code>static char *parse_select_clause (const char *clause, const char *username,
				  const char *defdomain,
				  const char *service,
				  const char *pass) /* prampec@gmail.com */

{
	char *str;

	static struct var_data vd[]={
		{&quot;local_part&quot;,	NULL,	sizeof(&quot;local_part&quot;),	0},
		{&quot;domain&quot;,		NULL,	sizeof(&quot;domain&quot;),	0},
		{&quot;service&quot;,		NULL,	sizeof(&quot;service&quot;),	0},
		{&quot;password&quot;,	NULL,	sizeof(&quot;password&quot;),	0}, /* prampec@gmail.com */
		{NULL,		NULL,	0,			0}};

	char *l_part;
	char *d_part;
	[B]char *pass_part;[/B]

	if (clause == NULL || *clause == '\0' ||
	    !username || *username == '\0' [B]||
            !pass || *pass == '\0') [/B]
		return NULL;

	if (!local_and_domain_part_escaped(username, 
					[B]pass, [/B]
					defdomain,
					&amp;l_part, &amp;d_part,
					[B]&amp;pass_part [/B]
					))
		return NULL;

	vd[0].value=l_part;
	vd[1].value=d_part;
	vd[2].value     = service;
	[B]vd[3].value     = pass_part;[/B]

	str=parse_string (clause, vd);
	free(l_part);
	free(d_part);
	[B]free(pass_part);[/B]
	return str;
}

static char *local_part_escaped(const char *username)
{
	const char *p=strchr(username, '@');
	size_t n=p ? p-username:strlen(username);
	char *buf=malloc(n*2+1);

	if (!buf)
	{
		perror(&quot;malloc&quot;);
		return NULL;
	}

	mysql_real_escape_string(mysql, buf, username, n);
	return buf;
}

static char *domain_part_escaped(const char *username,
				 const char *defdomain)
{
	const char *p=strchr(username, '@');
	size_t n;
	char *buf;

	if (p)
		++p;
	else
		p=defdomain;

	n=strlen(p);

	buf=malloc(n*2+1);

	if (!buf)
	{
		perror(&quot;malloc&quot;);
		return NULL;
	}

	mysql_real_escape_string(mysql, buf, p, n);
	return buf;
}

[B]
static char *pass_part_escaped(const char *pass)
{
        size_t n;
        char *buf;

        n=strlen(pass);

        buf=malloc(n*2+1);

        if (!buf)
        {
                perror(&quot;malloc&quot;);
                return NULL;
        }

        mysql_real_escape_string(mysql, buf, pass, n);
        return buf;
}
/* 
static char *pass_part_escaped(const char *pass)
{
	char *to = new char[(strlen(pass) * 2) + 1];
	mysql_real_escape_string(mysql, to, pass, strlen(pass));
	return to;

}
*/
[/B]
static int local_and_domain_part_escaped(const char *username,
					 [B]const char *pass,[/B]
					 const char *defdomain,
					 char **local_ret,
					 char **domain_ret,
					 [B]char **pass_ret [/B]
					 )
{
	if ((*local_ret=local_part_escaped(username)) == NULL)
		return 0;

[B]
	if ((*pass_ret=pass_part_escaped(pass)) == NULL)
	{
		free(*local_ret);
		return 0;
  	}
[/B]

	if ((*domain_ret=domain_part_escaped(username, defdomain)) == NULL)
	{
		free(*local_ret);
		[B]free(*pass_ret);[/B]
		return 0;
	}

	return 1;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1891225</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891225</guid><dc:creator><![CDATA[Error500]]></dc:creator><pubDate>Sun, 02 May 2010 09:42:28 GMT</pubDate></item><item><title><![CDATA[Reply to mysql_real_escape_string für eine zusätzliche Variable einbauen on Sun, 02 May 2010 09:47:22 GMT]]></title><description><![CDATA[<p>Das ist C. Hier ist das C++-Forum.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891234</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891234</guid><dc:creator><![CDATA[Nukularfüsiker]]></dc:creator><pubDate>Sun, 02 May 2010 09:47:22 GMT</pubDate></item><item><title><![CDATA[Reply to mysql_real_escape_string für eine zusätzliche Variable einbauen on Sun, 02 May 2010 09:55:35 GMT]]></title><description><![CDATA[<p>Und ob er wirklich Hilfe bekommt, mit dieser nichts aussagenden Fehlerbeschreibung und dem ganzen Quellcode, daran wage ich mal zu zweifeln. Wenn zumindest die Fehlerbeschreibung etwas besser wäre, könnte man es sich ja noch überlegen.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891238</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891238</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Sun, 02 May 2010 09:55:35 GMT</pubDate></item><item><title><![CDATA[Reply to mysql_real_escape_string für eine zusätzliche Variable einbauen on Sun, 02 May 2010 10:04:21 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>hier nochmal eine ausführlichere Beschreibung <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Der Funktion parse_select_clause() wird jetzt eine zusätzliche Variable, pass, übergeben. Im ersten Teil (&quot;Originaler Sourcecode&quot;) wird diese unbehandelt verarbeitet und letztlich 1-zu-1 an MySQL übergeben (mit allen sich daraus ergebenden Risiken).</p>
<p>Daher möchte ich die Variable vorher mit mysql_real_escape_string() escapen. Dazu habe ich mir angeguckt, wie im zweiten Teil mit den Variablen username, d_part und l_part verfahren wurde und versucht, das ganze analog für die Variable pass nachzubauen.</p>
<p>In Zeile 25-30 übergebe ich die Variable pass dann an die Funktion local_and_domain_part_escaped() - dort wird dann in Zeile 128-132 die Funktion pass_part_escaped() aufgerufen, die dann eigentlich nur den Befehl mysql_real_escape_string() aufrufen soll.<br />
Helfen die zusätzlichen Erklärungen, nachzuvollziehen, worin das Problem genau besteht? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891244</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891244</guid><dc:creator><![CDATA[Error500]]></dc:creator><pubDate>Sun, 02 May 2010 10:04:21 GMT</pubDate></item><item><title><![CDATA[Reply to mysql_real_escape_string für eine zusätzliche Variable einbauen on Sun, 02 May 2010 11:03:00 GMT]]></title><description><![CDATA[<p>Schon ein wenig besser, insofern dass man schneller deine Änderungen wiederfindet. Aber die Fehlerbeschreibung ist immer noch nichtsaussagend:</p>
<p>Error500 schrieb:</p>
<blockquote>
<p>..., aber an der AUTH-Stelle bricht es dann einfach ab.</p>
</blockquote>
<p>Diesen Teilsatz solltest du noch erläutern. Was verstehst du unter AUTH-Stelle? Was heisst &quot;bricht ab&quot;? Stürzt das Programm ab? Wenn ja, mit welcher Fehlermeldung? Oder schliesst sich die Verbindung? Oder wird es gar nicht ausgeführt? usw. usf.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891269</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891269</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Sun, 02 May 2010 11:03:00 GMT</pubDate></item><item><title><![CDATA[Reply to mysql_real_escape_string für eine zusätzliche Variable einbauen on Sun, 02 May 2010 12:55:06 GMT]]></title><description><![CDATA[<p>Sooo, ich editiere meinen letzten Post jetzt einfach - habe das Problem jetzt selbst lösen können <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Die Funktion</p>
<pre><code>static char *pass_part_escaped(const char *pass)
{
        size_t n;
        char *buf;

        n=strlen(pass);

        buf=malloc(n*2+1);

        if (!buf)
        {
                perror(&quot;malloc&quot;);
                return NULL;
        }

        mysql_real_escape_string(mysql, buf, pass, n);
        return buf;
}
</code></pre>
<p>war soweit völlig ok - das Problem wurde vorher ausgelöst, nämlich genau hier:</p>
<pre><code>if (clause == NULL || *clause == '\0' ||
            !username || *username == '\0' ||
            [b]!pass || *pass == '\0')[/b]
                return NULL;
</code></pre>
<p>Es kann in dem Programm vorkommen, dass legitimerweise kein Passwort ankommt - nachdem ich die fettgedruckte Zeile rausgenommen habe, klappt nun alles.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891294</guid><dc:creator><![CDATA[Error500]]></dc:creator><pubDate>Sun, 02 May 2010 12:55:06 GMT</pubDate></item></channel></rss>