Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6658509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:55:19+00:00 2026-05-26T01:55:19+00:00

I work on a project using libpcap for capturing IP packets. libpcap returns captured

  • 0

I work on a project using libpcap for capturing IP packets. libpcap returns captured data in a buffer, with an unsigned char * pointer and a buffer length. The data in the buffer is not null-terminated.

I do process the buffer data with library functions, e.g. string functions from the C standard library. These functions expect (signed) char * pointers, requiring casting the data between unsigned char * and char *.

I like the idea of assuming an unsigned char * buffer as not-null-terminated (accompanied by a buffer length) with potentially non-printable characters, as opposed to a char * buffer which holds a printable string literal. However, that forces me to cast the libpcap buffer for each string function call which makes the code ugly.

What would be your coding style preference in this case?

  • Keep the unsigned char * and cast when calling string functions.

  • Cast the libpcap buffer to char * immediatelly after receiving it from libpcap and differ between raw data and strings via variable naming conventions in the upstream code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:55:19+00:00Added an answer on May 26, 2026 at 1:55 am

    If you know that you are at a protocol level where there is supposed to be text,
    use the second approach, just keep a char* around and use that where needed. There’s no reason to cast it to a char* everywhere.

    However, be very, very, very careful about which string handling functions you use. You are capturing stuff off th wire, you could be getting anything. i.e. you have to respect the total length of the pcap supplied buffer everywhere – functions such as strlen, strcpy, etc. cannot be used unless you safely alter and nul terminate the buffer.
    (and you really have to make sanity checks, if e.g.you’re parsing the length of an UDP packet and the length says 130 bytes, doesn’t mean there actually is 130 bytes you can safely access)

    You also have to verify that what you’re parsing actually is text, you should not e.g. just print out a chunk of the payload assuming it is text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm starting work on a project using Rails, but I'm waiting for the 3rd
I recently started work on a personal coding project using C++ and KDevelop. Although
At work, we recently started a project using CouchDB (a document-oriented database). I've been
For a project at work, we're considering using the Maven plugin for Eclipse to
I'm investigating using hosted subversion for a new project at work and was wondering
We're using Trac as the task management tool at the project we work in.
I recently got involved in a Java project at work: we're using MyEclipse for
I just began using Visual Studio to work on a practice .NET project. I
On my current project, I am using FxCop to work through various code analysis
I'm trying to work on a new project parsing some JSON data for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.