Fossil SCM

Fix memcpy() compiler warnings.

drh 2019-08-20 19:16 trunk
Commit 7ae4b1a719c4a175a47e135dc00c9a84065f36640e637a74abf60b0378f6f283
1 file changed +20 -12
+20 -12
--- src/th.c
+++ src/th.c
@@ -202,10 +202,18 @@
202202
};
203203
typedef struct Buffer Buffer;
204204
static int thBufferWrite(Th_Interp *interp, Buffer *, const char *, int);
205205
static void thBufferInit(Buffer *);
206206
static void thBufferFree(Th_Interp *interp, Buffer *);
207
+
208
+/*
209
+** This version of memcpy() allows the first are second argument to
210
+** be NULL as long as the number of bytes to copy is zero.
211
+*/
212
+static void *th_memcpy(void *dest, const void *src, size_t n){
213
+ return n>0 ? memcpy(dest,src,n) : dest;
214
+}
207215
208216
/*
209217
** Append nAdd bytes of content copied from zAdd to the end of buffer
210218
** pBuffer. If there is not enough space currently allocated, resize
211219
** the allocation to make space.
@@ -227,17 +235,17 @@
227235
char *zNew;
228236
int nNew;
229237
230238
nNew = nReq*2;
231239
zNew = (char *)Th_Malloc(interp, nNew);
232
- memcpy(zNew, pBuffer->zBuf, pBuffer->nBuf);
240
+ th_memcpy(zNew, pBuffer->zBuf, pBuffer->nBuf);
233241
Th_Free(interp, pBuffer->zBuf);
234242
pBuffer->nBufAlloc = nNew;
235243
pBuffer->zBuf = zNew;
236244
}
237245
238
- memcpy(&pBuffer->zBuf[pBuffer->nBuf], zAdd, nAdd);
246
+ th_memcpy(&pBuffer->zBuf[pBuffer->nBuf], zAdd, nAdd);
239247
pBuffer->nBuf += nAdd;
240248
pBuffer->zBuf[pBuffer->nBuf] = '\0';
241249
242250
return TH_OK;
243251
}
@@ -841,12 +849,12 @@
841849
sizeof(int) * nCount + /* anElem */
842850
strbuf.nBuf /* space for list element strings */
843851
);
844852
anElem = (int *)&azElem[nCount];
845853
zElem = (char *)&anElem[nCount];
846
- memcpy(anElem, lenbuf.zBuf, lenbuf.nBuf);
847
- memcpy(zElem, strbuf.zBuf, strbuf.nBuf);
854
+ th_memcpy(anElem, lenbuf.zBuf, lenbuf.nBuf);
855
+ th_memcpy(zElem, strbuf.zBuf, strbuf.nBuf);
848856
for(i=0; i<nCount;i++){
849857
azElem[i] = zElem;
850858
zElem += (anElem[i] + 1);
851859
}
852860
*pazElem = azElem;
@@ -1293,11 +1301,11 @@
12931301
}
12941302
12951303
assert(zValue || nValue==0);
12961304
pValue->zData = Th_Malloc(interp, nValue+1);
12971305
pValue->zData[nValue] = '\0';
1298
- memcpy(pValue->zData, zValue, nValue);
1306
+ th_memcpy(pValue->zData, zValue, nValue);
12991307
pValue->nData = nValue;
13001308
13011309
return TH_OK;
13021310
}
13031311
@@ -1412,11 +1420,11 @@
14121420
char *zRes;
14131421
if( n<0 ){
14141422
n = th_strlen(z);
14151423
}
14161424
zRes = Th_Malloc(interp, n+1);
1417
- memcpy(zRes, z, n);
1425
+ th_memcpy(zRes, z, n);
14181426
zRes[n] = '\0';
14191427
return zRes;
14201428
}
14211429
14221430
/*
@@ -1472,11 +1480,11 @@
14721480
}
14731481
14741482
if( z && n>0 ){
14751483
char *zResult;
14761484
zResult = Th_Malloc(pInterp, n+1);
1477
- memcpy(zResult, z, n);
1485
+ th_memcpy(zResult, z, n);
14781486
zResult[n] = '\0';
14791487
pInterp->zResult = zResult;
14801488
pInterp->nResult = n;
14811489
}
14821490
@@ -1775,12 +1783,12 @@
17751783
nElem = th_strlen(zElem);
17761784
}
17771785
17781786
nNew = *pnStr + nElem;
17791787
zNew = Th_Malloc(interp, nNew);
1780
- memcpy(zNew, *pzStr, *pnStr);
1781
- memcpy(&zNew[*pnStr], zElem, nElem);
1788
+ th_memcpy(zNew, *pzStr, *pnStr);
1789
+ th_memcpy(&zNew[*pnStr], zElem, nElem);
17821790
17831791
Th_Free(interp, *pzStr);
17841792
*pzStr = zNew;
17851793
*pnStr = nNew;
17861794
@@ -2335,18 +2343,18 @@
23352343
if( pNew->pOp || pNew->nValue ){
23362344
if( pNew->nValue ){
23372345
/* A terminal. Copy the string value. */
23382346
assert( !pNew->pOp );
23392347
pNew->zValue = Th_Malloc(interp, pNew->nValue);
2340
- memcpy(pNew->zValue, z, pNew->nValue);
2348
+ th_memcpy(pNew->zValue, z, pNew->nValue);
23412349
i += pNew->nValue;
23422350
}
23432351
if( (nToken%16)==0 ){
23442352
/* Grow the apToken array. */
23452353
Expr **apTokenOld = apToken;
23462354
apToken = Th_Malloc(interp, sizeof(Expr *)*(nToken+16));
2347
- memcpy(apToken, apTokenOld, sizeof(Expr *)*nToken);
2355
+ th_memcpy(apToken, apTokenOld, sizeof(Expr *)*nToken);
23482356
}
23492357
23502358
/* Put the new token at the end of the apToken array */
23512359
apToken[nToken] = pNew;
23522360
nToken++;
@@ -2513,11 +2521,11 @@
25132521
25142522
if( op>0 && !pRet ){
25152523
pRet = (Th_HashEntry *)Th_Malloc(interp, sizeof(Th_HashEntry) + nKey);
25162524
pRet->zKey = (char *)&pRet[1];
25172525
pRet->nKey = nKey;
2518
- memcpy(pRet->zKey, zKey, nKey);
2526
+ th_memcpy(pRet->zKey, zKey, nKey);
25192527
pRet->pNext = pHash->a[iKey];
25202528
pHash->a[iKey] = pRet;
25212529
}
25222530
25232531
return pRet;
25242532
--- src/th.c
+++ src/th.c
@@ -202,10 +202,18 @@
202 };
203 typedef struct Buffer Buffer;
204 static int thBufferWrite(Th_Interp *interp, Buffer *, const char *, int);
205 static void thBufferInit(Buffer *);
206 static void thBufferFree(Th_Interp *interp, Buffer *);
 
 
 
 
 
 
 
 
207
208 /*
209 ** Append nAdd bytes of content copied from zAdd to the end of buffer
210 ** pBuffer. If there is not enough space currently allocated, resize
211 ** the allocation to make space.
@@ -227,17 +235,17 @@
227 char *zNew;
228 int nNew;
229
230 nNew = nReq*2;
231 zNew = (char *)Th_Malloc(interp, nNew);
232 memcpy(zNew, pBuffer->zBuf, pBuffer->nBuf);
233 Th_Free(interp, pBuffer->zBuf);
234 pBuffer->nBufAlloc = nNew;
235 pBuffer->zBuf = zNew;
236 }
237
238 memcpy(&pBuffer->zBuf[pBuffer->nBuf], zAdd, nAdd);
239 pBuffer->nBuf += nAdd;
240 pBuffer->zBuf[pBuffer->nBuf] = '\0';
241
242 return TH_OK;
243 }
@@ -841,12 +849,12 @@
841 sizeof(int) * nCount + /* anElem */
842 strbuf.nBuf /* space for list element strings */
843 );
844 anElem = (int *)&azElem[nCount];
845 zElem = (char *)&anElem[nCount];
846 memcpy(anElem, lenbuf.zBuf, lenbuf.nBuf);
847 memcpy(zElem, strbuf.zBuf, strbuf.nBuf);
848 for(i=0; i<nCount;i++){
849 azElem[i] = zElem;
850 zElem += (anElem[i] + 1);
851 }
852 *pazElem = azElem;
@@ -1293,11 +1301,11 @@
1293 }
1294
1295 assert(zValue || nValue==0);
1296 pValue->zData = Th_Malloc(interp, nValue+1);
1297 pValue->zData[nValue] = '\0';
1298 memcpy(pValue->zData, zValue, nValue);
1299 pValue->nData = nValue;
1300
1301 return TH_OK;
1302 }
1303
@@ -1412,11 +1420,11 @@
1412 char *zRes;
1413 if( n<0 ){
1414 n = th_strlen(z);
1415 }
1416 zRes = Th_Malloc(interp, n+1);
1417 memcpy(zRes, z, n);
1418 zRes[n] = '\0';
1419 return zRes;
1420 }
1421
1422 /*
@@ -1472,11 +1480,11 @@
1472 }
1473
1474 if( z && n>0 ){
1475 char *zResult;
1476 zResult = Th_Malloc(pInterp, n+1);
1477 memcpy(zResult, z, n);
1478 zResult[n] = '\0';
1479 pInterp->zResult = zResult;
1480 pInterp->nResult = n;
1481 }
1482
@@ -1775,12 +1783,12 @@
1775 nElem = th_strlen(zElem);
1776 }
1777
1778 nNew = *pnStr + nElem;
1779 zNew = Th_Malloc(interp, nNew);
1780 memcpy(zNew, *pzStr, *pnStr);
1781 memcpy(&zNew[*pnStr], zElem, nElem);
1782
1783 Th_Free(interp, *pzStr);
1784 *pzStr = zNew;
1785 *pnStr = nNew;
1786
@@ -2335,18 +2343,18 @@
2335 if( pNew->pOp || pNew->nValue ){
2336 if( pNew->nValue ){
2337 /* A terminal. Copy the string value. */
2338 assert( !pNew->pOp );
2339 pNew->zValue = Th_Malloc(interp, pNew->nValue);
2340 memcpy(pNew->zValue, z, pNew->nValue);
2341 i += pNew->nValue;
2342 }
2343 if( (nToken%16)==0 ){
2344 /* Grow the apToken array. */
2345 Expr **apTokenOld = apToken;
2346 apToken = Th_Malloc(interp, sizeof(Expr *)*(nToken+16));
2347 memcpy(apToken, apTokenOld, sizeof(Expr *)*nToken);
2348 }
2349
2350 /* Put the new token at the end of the apToken array */
2351 apToken[nToken] = pNew;
2352 nToken++;
@@ -2513,11 +2521,11 @@
2513
2514 if( op>0 && !pRet ){
2515 pRet = (Th_HashEntry *)Th_Malloc(interp, sizeof(Th_HashEntry) + nKey);
2516 pRet->zKey = (char *)&pRet[1];
2517 pRet->nKey = nKey;
2518 memcpy(pRet->zKey, zKey, nKey);
2519 pRet->pNext = pHash->a[iKey];
2520 pHash->a[iKey] = pRet;
2521 }
2522
2523 return pRet;
2524
--- src/th.c
+++ src/th.c
@@ -202,10 +202,18 @@
202 };
203 typedef struct Buffer Buffer;
204 static int thBufferWrite(Th_Interp *interp, Buffer *, const char *, int);
205 static void thBufferInit(Buffer *);
206 static void thBufferFree(Th_Interp *interp, Buffer *);
207
208 /*
209 ** This version of memcpy() allows the first are second argument to
210 ** be NULL as long as the number of bytes to copy is zero.
211 */
212 static void *th_memcpy(void *dest, const void *src, size_t n){
213 return n>0 ? memcpy(dest,src,n) : dest;
214 }
215
216 /*
217 ** Append nAdd bytes of content copied from zAdd to the end of buffer
218 ** pBuffer. If there is not enough space currently allocated, resize
219 ** the allocation to make space.
@@ -227,17 +235,17 @@
235 char *zNew;
236 int nNew;
237
238 nNew = nReq*2;
239 zNew = (char *)Th_Malloc(interp, nNew);
240 th_memcpy(zNew, pBuffer->zBuf, pBuffer->nBuf);
241 Th_Free(interp, pBuffer->zBuf);
242 pBuffer->nBufAlloc = nNew;
243 pBuffer->zBuf = zNew;
244 }
245
246 th_memcpy(&pBuffer->zBuf[pBuffer->nBuf], zAdd, nAdd);
247 pBuffer->nBuf += nAdd;
248 pBuffer->zBuf[pBuffer->nBuf] = '\0';
249
250 return TH_OK;
251 }
@@ -841,12 +849,12 @@
849 sizeof(int) * nCount + /* anElem */
850 strbuf.nBuf /* space for list element strings */
851 );
852 anElem = (int *)&azElem[nCount];
853 zElem = (char *)&anElem[nCount];
854 th_memcpy(anElem, lenbuf.zBuf, lenbuf.nBuf);
855 th_memcpy(zElem, strbuf.zBuf, strbuf.nBuf);
856 for(i=0; i<nCount;i++){
857 azElem[i] = zElem;
858 zElem += (anElem[i] + 1);
859 }
860 *pazElem = azElem;
@@ -1293,11 +1301,11 @@
1301 }
1302
1303 assert(zValue || nValue==0);
1304 pValue->zData = Th_Malloc(interp, nValue+1);
1305 pValue->zData[nValue] = '\0';
1306 th_memcpy(pValue->zData, zValue, nValue);
1307 pValue->nData = nValue;
1308
1309 return TH_OK;
1310 }
1311
@@ -1412,11 +1420,11 @@
1420 char *zRes;
1421 if( n<0 ){
1422 n = th_strlen(z);
1423 }
1424 zRes = Th_Malloc(interp, n+1);
1425 th_memcpy(zRes, z, n);
1426 zRes[n] = '\0';
1427 return zRes;
1428 }
1429
1430 /*
@@ -1472,11 +1480,11 @@
1480 }
1481
1482 if( z && n>0 ){
1483 char *zResult;
1484 zResult = Th_Malloc(pInterp, n+1);
1485 th_memcpy(zResult, z, n);
1486 zResult[n] = '\0';
1487 pInterp->zResult = zResult;
1488 pInterp->nResult = n;
1489 }
1490
@@ -1775,12 +1783,12 @@
1783 nElem = th_strlen(zElem);
1784 }
1785
1786 nNew = *pnStr + nElem;
1787 zNew = Th_Malloc(interp, nNew);
1788 th_memcpy(zNew, *pzStr, *pnStr);
1789 th_memcpy(&zNew[*pnStr], zElem, nElem);
1790
1791 Th_Free(interp, *pzStr);
1792 *pzStr = zNew;
1793 *pnStr = nNew;
1794
@@ -2335,18 +2343,18 @@
2343 if( pNew->pOp || pNew->nValue ){
2344 if( pNew->nValue ){
2345 /* A terminal. Copy the string value. */
2346 assert( !pNew->pOp );
2347 pNew->zValue = Th_Malloc(interp, pNew->nValue);
2348 th_memcpy(pNew->zValue, z, pNew->nValue);
2349 i += pNew->nValue;
2350 }
2351 if( (nToken%16)==0 ){
2352 /* Grow the apToken array. */
2353 Expr **apTokenOld = apToken;
2354 apToken = Th_Malloc(interp, sizeof(Expr *)*(nToken+16));
2355 th_memcpy(apToken, apTokenOld, sizeof(Expr *)*nToken);
2356 }
2357
2358 /* Put the new token at the end of the apToken array */
2359 apToken[nToken] = pNew;
2360 nToken++;
@@ -2513,11 +2521,11 @@
2521
2522 if( op>0 && !pRet ){
2523 pRet = (Th_HashEntry *)Th_Malloc(interp, sizeof(Th_HashEntry) + nKey);
2524 pRet->zKey = (char *)&pRet[1];
2525 pRet->nKey = nKey;
2526 th_memcpy(pRet->zKey, zKey, nKey);
2527 pRet->pNext = pHash->a[iKey];
2528 pHash->a[iKey] = pRet;
2529 }
2530
2531 return pRet;
2532

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button