Commit Graph

20 Commits

Author SHA1 Message Date
Levi Neely 358ae595e9
Add memory accounting per subsystem (#371)
* Implement memory accounting infrastructure

* Migrate malloc/calloc to pmem_malloc with subsystem tracking

* Add overflow-safe pmem_malloc_array function

* Migrate pfstasks.c malloc/free to pmem_malloc/pmem_free

* Migrate pnetlibs.c malloc/free to pmem_malloc/pmem_free

* Fix multiplication patterns in pnetlibs.c with pmem_malloc_array

* Migrate ppagecache.c malloc/free to pmem_malloc/pmem_free

* Fix remaining multiplication pattern in ppagecache.c line 3239

* Migrate pssl.c malloc/free to pmem_malloc/pmem_free

* Migrate pcryptofolder.c malloc/free to pmem_malloc/pmem_free

* Migrate pfolder.c malloc/free to pmem_malloc/pmem_free

* Fix multiplication patterns in pfolder.c with overflow checks

* Migrate publiclinks.c malloc/free to pmem_malloc/pmem_free

* Migrate plocalscan.c malloc/free to pmem_malloc/pmem_free

* Migrate psql.c malloc/free to pmem_malloc/pmem_free

* Migrate putil.c malloc/free to pmem_malloc/pmem_free

* Fix multiplication patterns in putil.c encoding functions with overflow checks

* Migrate pfsupload.c malloc/free to pmem_malloc/pmem_free

* Migrate pdiff.c malloc/free to pmem_malloc/pmem_free

* Migrate pcrypto.c malloc/free to pmem_malloc/pmem_free

* Migrate pupload.c malloc/free to pmem_malloc/pmem_free

* Migrate psock.c malloc/free to pmem_malloc/pmem_free

* Fix multiplication pattern in psock.c with overflow checks

* Add panic() with backtrace to psignal module

* Register panic handlers early in main()

* Fix panic() infinite recursion by unregistering handlers before abort()

* Migrate remaining 34 files to pmem_malloc/pmem_free

* Migrate papi.c, ptools.c, pbusinessaccount.c to PMEM_SUBSYS_API

* Fix pcache.c compilation errors - replace he->pmem_free with pmem_free

* Add pmem.h include to pdbg.c

* Add pmem.h include to 10 files missing it

* Fix ptask.c compilation errors - replace s->pmem_free with s->free

* Fix ptask.c callback arguments - remove PMEM_SUBSYS_OTHER from s->free calls

* Fix pmem.c infinite recursion - use raw malloc/free internally

* Fix prand.c mixed allocation - use pmem_free for putil_strdup result

* Migrate psettings.c free() calls to pmem_free()

* Fix all remaining stray free() calls - migrate to pmem_free()

* Fix pnetlibs_debug.c free() call - migrate to pmem_free()

* Add missing pmem.h include to pdevice.c

* Migrate pclsync_lib.cpp free() calls to pmem_free()

* Fix pfolder.c psync_free_string_list to use pmem_free()

* Fix all psync_list_for_each_element_call to use pmem_free wrappers

* Fix all list free wrapper functions - add proper definitions

* Fix rpcclient.cpp mixed allocation - use pmem_free for prpc_sockpath

* Replace all realloc() calls with pmem_realloc()

* Replace all strdup() calls with putil_strdup()

* Fix rpcclient.cpp allocation boundary - use plain malloc/free for RPC responses

* Fix RPC response length calculation - include header size

* Fix control_tools.cpp mixed allocations - use pmem_free for pshm_read results

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-08 21:47:22 +01:00
Levi Neely d0c213f181
Add overflow-safe allocation wrapper pmem_calloc_safe() (#369)
* Add overflow-safe allocation wrappers to pmem module

* Replace malloc(nmemb*size) with pmem_calloc_safe in high-risk sites

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-08 14:55:21 +01:00
Levi Neely e37c7f5894
Fix pcl-dls: errPtr memory leak between ptools_backend_call() calls in ptools_set_backend_file_dates() (#358)
* Fix pcl-dls.1: free errPtr between calls in ptools_set_backend_file_dates()

char *errPtr was already used instead of char msgErr[1024], but was
not freed between the two ptools_backend_call() invocations. If the
first call allocated errPtr, the second would overwrite the pointer
without freeing it. Add free(errPtr); errPtr = NULL; between the two
calls to eliminate the leak.

Ref GH #194.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Add ptools_set_backend_file_dates() errPtr lifecycle tests (pcl-dls)

6 test cases using malloc/free wrapping covering both-succeed,
call1-error, call2-error, both-errors, pre-fix leak demonstration,
and no-double-free after mid-free NULLing.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 19:01:17 +01:00
Levi Neely 60e43edec4
Fix pcl-a1j: buffer overflow in ptools_create_backend_event() via unchecked sprintf and strcat (#355)
* Fix pcl-a1j.1: replace sprintf with snprintf and add strcat length check in ptools_create_backend_event()

Add paramname length check (> 254 bytes → skip with warning) before
snprintf into charBuff[i][258], and validate the snprintf return
value. Add explicit length check before strcat into keyParams to
prevent overflow when paramname exceeds remaining buffer space.
Eliminates buffer overflow from long paramname.

Ref GH #195.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix pcl-a1j.1: clamp pCnt to PTOOLS_MAX_PARAMS to prevent charBuff stack overflow

charBuff[30][258] is a fixed-size stack array but pCnt was unbounded,
allowing any caller with params->paramCnt > 30 to overflow the stack
via charBuff[i] access. Add PTOOLS_MAX_PARAMS (30) define, use it to
size charBuff, and clamp pCnt to PTOOLS_MAX_PARAMS with a warning log
before the loop.

Ref GH #195.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Add ptools_create_backend_event() validation tests (pcl-a1j)

11 test cases covering pCnt clamping, paramname length guards,
snprintf boundary, keyParams overflow check, and comma-prefix
for subsequent params.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 18:35:43 +01:00
Levi Neely 00b4824961
Replace sprintf with snprintf in ptools.c (#317)
First login tested, no crash
2026-03-03 15:24:10 +01:00
Levi Neely 838b826b90
Fix char[] passed as char** in ptools_set_backend_file_dates (#305)
msgErr was declared as char[1024] but cast to char** and passed to
ptools_backend_call(). The function writes a char* into *err,
corrupting the first sizeof(char*) bytes of the array.

Change to char *errPtr = NULL and free it after use.

Fixes GH #216

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-03 12:34:18 +01:00
Levi Neely 3c096565ea
Fix optional params always using index 0 in ptools_backend_call (#304)
Loop variable j was declared inside the loop and reset to 0 on every
iteration. All branches had early continue, so j++ never executed.

Change j to i - reqParCnt to correctly index into optionalParams array.
Also fix paramtype check to use j instead of i.

Fixes GH #215

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-03 12:31:01 +01:00
Levi Neely beaa76adfb
Fix uninitialized keyParams in ptools_create_backend_event (#299)
keyParams is only allocated when pCnt > 0 but freed unconditionally.
When pCnt == 0, free() is called on an uninitialized pointer (UB).

Initialize to NULL so free() is a no-op when allocation is skipped.

Fixes GH #214

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-03 12:10:44 +01:00
Levi Neely 4d64b645a1
Increase maintainability: namespace refactors for putil, pfs, and pfs sub-modules (#176)
* Fix #103: Refactor putil to use putil_ namespace and enforce encapsulation

All public functions in putil now use the putil_ prefix (e.g., putil_strdup,
putil_strcat, putil_time_format, putil_base64_encode, etc.). Internal
variables (normalize_table, base64_reverse_table) and the constructor remain
static. Updated all 34 call-site files across pclsync accordingly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix #96: Refactor pfs to use pfs_ namespace and enforce encapsulation

All public functions in the pfs module renamed from psync_fs_ to pfs_
(e.g., pfs_update_openfile, pfs_refresh, pfs_start, pfs_stop, etc.).
Extern vars psync_fake_prefix/psync_fake_prefix_len renamed to
pfs_fake_prefix/pfs_fake_prefix_len. Functions only used within pfs.c
(pfs_update_openfile_fileid_locked, pfs_chown) made static. Updated all
call sites across 17 files including pclsync_lib.cpp.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Refactor pfsxattr to use pfs_xattr_ namespace

All psync_fs_ prefixed functions in the pfsxattr module renamed to
pfs_xattr_ (e.g., pfs_xattr_set, pfs_xattr_get, pfs_xattr_list,
pfs_xattr_remove, pfs_xattr_file_deleted, pfs_xattr_task_to_file, etc.).
Local macro psync_fs_set_thread_name renamed to pfs_xattr_set_thread_name.
Internal static helpers (delete_object_id, update_object_id,
xattr_get_object_id_locked) remain static and unchanged.
Updated all call sites in pfs.c, pfsupload.c, and pdiff.c.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Rename pfs_xattr_ namespace to pfs_xatr_

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Refactor pfs sub-modules to use namespaced function prefixes

- pfscrypto_* → pfs_crpt_*
- psync_fsstatic_* → pfs_stat_*
- psync_fsupload_* → pfs_upld_*
- psync_fstask_* → pfs_task_* (functions only; typedefs unchanged)
- psync_fsfolder_*/psync_fsfolderid_*/psync_fsfolderflags_*/
  get_decname_for_folder/psync_get_folderid → pfs_fldr_*

Type names, macro constants, and typedef aliases are unchanged.
Updated all call sites across 17 files.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 22:51:25 +01:00
Levi Neely 6994a3cac9
102 create a psql namespace (#142)
* another stupid macro gone

* more stupid macros gone. moved util functions from plibs to putil

* remaining plist and ptask functions moved to appropriate namespaces

* dead code beleted!

* bugfixes

* move sql functions to psql.c

* move sort functions into ppagecache

* psql namespace

* change plibs.h include to pdbg.h

* cleanup includes
2025-03-18 19:52:15 +01:00
Levi Neely 58461232db
fix weird name (#140) 2025-03-13 20:44:42 +01:00
Levi Neely 9014edb99a
98 create a pdbg namespace (#139)
* pdbg split from plibs
* pdbg namespace
* add SSLDBGLVL build option
2025-03-13 20:28:09 +01:00
Levi Neely 9da561c767
131 remove custom memory management wrappers (#137)
* psync_free removed

* psync_malloc removed

* psync_realloc removed

* remove dead code

* remove pmemlock

* remove dead code
2025-03-11 22:27:30 +01:00
Levi Neely 8ad1fe6086
Code cleanup and readline for better CLI UX (#116)
* pcommands.h for command ids

* remove compiler compat junk

* transient and unnecessary includes resolved

* unnecessary goto

* function naming and whatnot

* use readline for better command line UX

* add readline to CI workflow
2025-03-09 09:14:12 +01:00
Levi Neely 1106028eba
WIP: Decompose namespaces and other cleanup (#94)
* papi namespace, remove some transient dependencies

* ptask namespace and cleanup

* pcache namespace and cleanup

* refactor pcallbacks into pqevent and ptevent

* pcryptofolder namespace and cleanup

* pcryptofolder missed a rename

* pdeflate namespace and cleanup

* pcrypto namespace

* pcrypto cleanup

* pcrypto consistency

* pcrypto more consistency

* pdevmon namespace and cleanup

* pdiff namespace

* pdownload namespace and cleanup

* pstatus namespace and cleanup

* pfileops namespace, fix header inlining abuse

* pfoldersync API, start chipping away at the psynclib monster

* pfscrypto namespace

* plocks namespace

* pnotify namespace

* pnotify formatting

* prpc namespace (got rid of confusing "overlay")

* pp2p namespace and cleanup

* ppagecache namespace

* ppassword namespace

* ppathstatus namespace

* prunratelimited => prun with throttling

* pscanner.c => psuggest namespace

* ptimer namespace

* ptools namespace (move to putil namespace someday)

* ptree namespace

* moved public link definitions into publiclinks.h, no clear namespace here though...

* pupload namespace

* formatting

* pcrc32c namespace
2025-03-01 22:21:54 +01:00
Levi Neely 2b6508a275
eliminate pcompat (#92)
* code cleanup and removal
- remove useless wrapper functions
- remove obfuscating typedefs
- code reorganization and reformatting
- function renames for clarification

* prepare pcompat.h for decomposition

* remove dead function psync_rebuild_icons, reorganize pfsstatic.c headers

* separate pdevice from pcompat

* reorg function prototypes, remove more compat stuff

* renamed mutex for brevity

* remove dead debug code; plibs version is more sophisticated

* remove dead code, stale comments

* moved psync_delete_cached_crypto_keys to psynclib

* forgot to remove the function prototype from pdiff.h

* reorganized functions by accessibility, inlined useless psync_cache_contacts function

* remove unused header

* ppath object decomposed from pcompat

* ppath namespace

* ppath struct renames

* ppath final cleanup

* refactor, remove compatibility code

* format

* prun decomposed from pcompat for thread mgmt

* slim down prun.c

* remove useless comments

* move pagesize to pmemlock, pmemlock namespace

* pmem decomposed from pcompat, pmem namespace

* decompose psys from pcompat

* fix psys function names, remove more compatibility junk

* prand decomposed from pcompat

* psock decomposed from pcompat

* types renamed for ns consistency, brevity

* cleanup psock, remove unused proxy code

* remove unused headers psock

* putil for utility macros and functions

* forgot to move some psock constants

* move time macro to psys

* eliminate pcompat completely; new pfile namespace

* cleanup
- moved thread name definition
- explicit externs in implementation instead of headers for now
- better extern name
2025-02-28 22:47:32 +01:00
Levi Neely c687484707
Implement add, remove, and list sync folder commands (#59)
* list_sync_folders seems to work

* list_sync_folders breaks with sync folders in db; message size problem?

* fixed sync folders list

* fix double free and buffer overflow

* wip: add and remove sync folder; need to fix request->value in instance_thread

* remove comment

* removed pclsync_lib_c.{h,cpp}

* cleanup get_answer_to_request

* update comment

* changed reply struct to type response containing payload and msg

* cleanup

* refactored overlay_client, and poverlay; added docs

* better naming

* sock path in poverlay_protocol.h

* clarify comment

* refactor psync_overlay_get_response

* clarifications

* response_messages work with and without payload

* removed commented code

* syncadd works, but i need to fix the error handling

* remove sync folder

* remove commented code, change printfs to debug or simply remove

* removed unnecessary print

* removed more prints

* removed dead var

* update error handling in add_sync_folder, fix memleak

* simplified finalize
2024-08-25 21:54:41 +02:00
Levi Neely ed06599b7d
Bug fixes, build containers, minor updates (#54)
* openssl is a dependency

* openssl is not a real dpendency

* static link by default

* wip: build containers

* rm comments

* build containers, fix DEADLYSIGNAL on run with no args

* debug tools in deb/ubuntu, fix #44

* no slack container :(

* ensure P_LINUX_OS always defined

* error check fuse unmount ops

* fsanitize=thread temporarily

* undo

* clang static analysis

* fix dead assignment

* makefile quality of life stuff

* make install and uninstall; ignore scan results

* addressed dead assignments from static analysis

* debug and release builds possible

* gcc still default compiler

* debug symbols on debug build only
2024-08-20 22:00:38 +02:00
Levi Neely 8d3c496754
memleak fixes (#40)
* fixed 2 of the 3 memory leaks

* fixed more memleaks

* more leaks

* another one

* BUGHUNT mode to look for more mem leaks
2024-08-15 23:06:55 +02:00
Levi Neely bec97722cd
reorg source tree, link system mbedtls (#38)
* reorg source tree, link system mbedtls

* remove cmakelists.txt

* update ci workflow

* -Werror to -Wall

* libpcloudcc_so in makefile

* single makefile works

* removed extra makefiles
2024-08-15 19:49:57 +02:00