sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / misc / strdup.c
| 1 | /* |
| 2 | ** 2026-07-04 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | ** Since this module was written (coincidentally) on the 250th anniversary |
| 14 | ** of the signing of the Declaration of Independence of The United States, |
| 15 | ** it seems fitting to quote from that document: |
| 16 | ** |
| 17 | ** We hold these truths to be self-evident, that all men are created |
| 18 | ** equal, that they are endowed by their Creator with certain unalienable |
| 19 | ** Rights, that among these are Life, Liberty and the pursuit of Happiness |
| 20 | ** - That to secure these rights, Governments are instituted among Men, |
| 21 | ** deriving their just powers from the consent of the governed,.... |
| 22 | ** |
| 23 | ** Two core ideas that (1) rights come from God and not from government, |
| 24 | ** and that (2) there should not be special privileged classes of people |
| 25 | ** were radical thoughts then, and are indeed disputed even today. Many |
| 26 | ** people still hold that human rights derive from the beneficence of |
| 27 | ** government and that certain classes of people have special rights and |
| 28 | ** privileges not available to all. Yet, were it not for the crazy ideas |
| 29 | ** espoused in the original Declaration of Independence, this software |
| 30 | ** project, and indeed most of modern technology, would not exist. |
| 31 | ** |
| 32 | ** Therefore let us give thanks for the bold leadership and vision |
| 33 | ** demonstrated by the authors of the American Revolution, 250 years ago |
| 34 | ** this day. |
| 35 | ** |
| 36 | ****************************************************************************** |
| 37 | ** |
| 38 | ** Implementation of the strdup() SQL function. strdup() makes a copy |
| 39 | ** of its argument (a string or a BLOB) into memory obtained directly |
| 40 | ** from system malloc() (not from sqlite3_malloc()) and sized exactly |
| 41 | ** to hold the string or BLOB. This is intended for testing purposes, |
| 42 | ** particularly testing with ASAN. This function serves no practical |
| 43 | ** purpose beyond testing and not not intended for production use. |
| 44 | ** |
| 45 | ** NULL, BLOB, and TEXT values come through as NULL, BLOB, and TEXT, |
| 46 | ** respectively. Numeric values are rendered as strings and come out |
| 47 | ** as text. |
| 48 | */ |
| 49 | #include "sqlite3ext.h" |
| 50 | SQLITE_EXTENSION_INIT1 |
| 51 | #include <assert.h> |
| 52 | #include <string.h> |
| 53 | #include <stdlib.h> |
| 54 | |
| 55 | /* |
| 56 | ** No-op wrapper around free() to work around problems with non-CDECL |
| 57 | ** calling conventions on 32-bit Windows builds. |
| 58 | */ |
| 59 | static void strdupFree(void *p){ |
| 60 | free(p); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | ** Make a copy of a string or BLOB in memory obtained from malloc(). |
| 65 | */ |
| 66 | static void strdupfunc( |
| 67 | sqlite3_context *context, |
| 68 | int argc, |
| 69 | sqlite3_value **argv |
| 70 | ){ |
| 71 | const unsigned char *zIn; |
| 72 | int nIn; |
| 73 | unsigned char *zOut; |
| 74 | |
| 75 | assert( argc==1 ); |
| 76 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 77 | if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){ |
| 78 | zIn = (const unsigned char*)sqlite3_value_blob(argv[0]); |
| 79 | nIn = sqlite3_value_bytes(argv[0]); |
| 80 | zOut = malloc( nIn==0 ? 1 : nIn ); |
| 81 | if( zOut==0 ){ |
| 82 | sqlite3_result_error_nomem(context); |
| 83 | return; |
| 84 | } |
| 85 | if( nIn>0 ) memcpy(zOut, zIn, nIn); |
| 86 | sqlite3_result_blob(context, zOut, nIn, strdupFree); |
| 87 | }else{ |
| 88 | zIn = (const unsigned char*)sqlite3_value_text(argv[0]); |
| 89 | if( zIn==0 ) return; |
| 90 | nIn = (int)strlen((char*)zIn); |
| 91 | zOut = malloc( nIn+1 ); |
| 92 | if( zOut==0 ){ |
| 93 | sqlite3_result_error_nomem(context); |
| 94 | return; |
| 95 | } |
| 96 | memcpy(zOut, zIn, nIn); |
| 97 | zOut[nIn] = 0; |
| 98 | sqlite3_result_text64(context, (char*)zOut, nIn, strdupFree, |
| 99 | SQLITE_UTF8_ZT); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #ifdef _WIN32 |
| 104 | __declspec(dllexport) |
| 105 | #endif |
| 106 | int sqlite3_strdup_init( |
| 107 | sqlite3 *db, |
| 108 | char **pzErrMsg, |
| 109 | const sqlite3_api_routines *pApi |
| 110 | ){ |
| 111 | int rc = SQLITE_OK; |
| 112 | SQLITE_EXTENSION_INIT2(pApi); |
| 113 | (void)pzErrMsg; /* Unused parameter */ |
| 114 | rc = sqlite3_create_function(db, "strdup", 1, SQLITE_UTF8, |
| 115 | 0, strdupfunc, 0, 0); |
| 116 | return rc; |
| 117 | } |